Jump to content

Is it possible to get the current rule name within the rule itself?


ScottHillock

Recommended Posts

We get requests to edit customer supplied data on the fly. There might be 5 or so fields that need the same edits needed to each. I know we can do a loop and AddVariable for each all in one rule, but if there's specific instructions for just 1 or 2 fields, it's easier to keep the rules separate and just name the rules the same as the field name. Just looking for a way to make it less error prone, so we don't have to update the Field we're referencing for each.
Link to comment
Share on other sites

We get requests to edit customer supplied data on the fly. There might be 5 or so fields that need the same edits needed to each. I know we can do a loop and AddVariable for each all in one rule, but if there's specific instructions for just 1 or 2 fields, it's easier to keep the rules separate and just name the rules the same as the field name. Just looking for a way to make it less error prone, so we don't have to update the Field we're referencing for each.

I think a concrete example would be helpful here. Let's say you have three fields like "Name", "Phone", and "Email". And you want to apply some kind of formatting to each, right? So then you would make three rules with those same names, where each is something like this:

return ToTitleCase(Field("Name"));

return FormatPhoneNumber(Field("Phone"));

return '<color name=Blue>' + CopyfitEmail(Field("Email"), "Arial", 10, 10000);

Right?

 

But how does knowing the rule name inside the rule help you? Are you saying you want to write each rule something like this?

return ToTitleCase(Field(this.callee.name)); // doesn't work

I suppose that could save you a bit of boilerplate in each rule, but each rule still has to have different formatting (what you call "edits") applied, right? And you would still need to change each rule name if a field name changes. So I don't really see how knowing the rule name inside the rule really helps much.

 

Let's go back to this:

I know we can do a loop and AddVariable for each all in one rule, but if there's specific instructions for just 1 or 2 fields, it's easier to keep the rules separate and just name the rules the same as the field name.

I have to disagree here. To me, having all of the different kinds of formatting applied to each field in one place is easier to manage. If you have to tweak the formatting, it's easier to just do it once, in one rule, than to apply the same change to multiple rules. (In programmer jargon, we call this the DRY principle: "Don't Repeat Yourself"; that is, write your code so that you don't have to apply the same change to multiple places.)

 

I presume that the loop you're talking about is doing something like this in OnRecordStart:

for (var field in FusionPro.Fields)
   FusionPro.Composition.AddVariable(field, ToUpper(Field(field)));

Right? But you only want to apply the formatting (uppercase or whatever) to one or two fields instead of all of them? That's not really hard; just loop through those specific fields instead of through all of them, like so:

var fieldsToModify = ["name", "email"];
for (var i in fieldsToModify)
{
   var field = fieldsToModify[i];
   FusionPro.Composition.AddVariable(field, ToUpper(Field(field)));
}

Or, if you want to make a list of certain fields to exclude from the formatting, well, that question has actually already been asked and answered here:

http://forums.pti.com/showpost.php?p=17236&postcount=5

 

That thread has other examples as well, including one showing how to apply different types of formatting to different fields.

Link to comment
Share on other sites

The only problem with the OnRecordStart way is I haven't looked to see how to get tagged text to return correctly.

 

var fieldsToModify = ["paragraphtext1", "paragraphtext2", "singlelinetext1"];
for (var i in fieldsToModify)
{
   var field = fieldsToModify[i];
   FusionPro.Composition.AddVariable(field, Field(field).replace(/(.*)(\s)(.+)$/, "$1 $3").replace(" ","<color name=White> </color>");
}

 

Maybe it's possible, but I haven't looked.

 

Also, the customer will likely have specific changes then for just singlelinetext1. Which is easy enough to add a for statement, but it also adds complexity to the code.

 

Which is my next point. I might not be the only one not working on a project, or it might be months before I work on the project again. And it's easier to see in the rule list window exactly what fields have modifications made to them, vs having to open a rule and then go through the programming to see what's being modified.

 

The core issue, and the reason for asking the initial question. Is being able to duplicate a rule, and just change the rule name to modify a new field. Which doesn't seem like a feature available. We can adjust our workflow to work around it.

Link to comment
Share on other sites

The only problem with the OnRecordStart way is I haven't looked to see how to get tagged text to return correctly.

You can call TaggedDataField instead of Field, and then add the third parameter of "true" to the FusionPro.Composition.AddVariable call:

for (var field in FusionPro.Fields)
   FusionPro.Composition.AddVariable(field, '<span color=Red>' + TaggedDataField(field) + '</span>', true);

Or you can do what the examples in that other thread show and call RawTextFromTagged on the tagged field value.

var fieldsToModify = ["paragraphtext1", "paragraphtext2", "singlelinetext1"];
for (var i in fieldsToModify)
{
   var field = fieldsToModify[i];
   FusionPro.Composition.AddVariable(field, Field(field).replace(/(.*)(\s)(.+)$/, "$1 $3").replace(" ","<color name=White> </color>");
}

 

Maybe it's possible, but I haven't looked.

The same thing should work for that logic too:

var fieldsToModify = ["paragraphtext1", "paragraphtext2", "singlelinetext1"];
for (var i in fieldsToModify)
{
   var field = fieldsToModify[i];
   FusionPro.Composition.AddVariable(field, TaggedDataField(field).replace(/(.*)(\s)(.+)$/, "$1 $3").replace(" ","<color name=White> </color>"), true);
}

Also, the customer will likely have specific changes then for just singlelinetext1. Which is easy enough to add a for statement, but it also adds complexity to the code.

Well, yeah, any time the requirements get more complex, the VDP template (which is essentially a program) gets more complex to meet them. But what you need isn't another for statement, it's a switch statement:

for (var field in FusionPro.Fields)
{
   var oldval = TaggedDataField(field);
   var newval = "";
   switch (ToLower(field))
   {
       case "paragraphtext1":
       case "paragraphtext2":
           newval = '<span color=Red>' + oldval + '</span>';
           break;
       case "singlelinetext1":
           newval = oldval.replace(/(.*)(\s)(.+)$/, "$1 $3").replace(" ","<color name=White> </color>"), 
           break;
       // etc.
   }

   if (newval)
       FusionPro.Composition.AddVariable(field, newval, true);
}

That code doesn't do anything for any field not specifically called out in a case statement.

Which is my next point. I might not be the only one not working on a project, or it might be months before I work on the project again. And it's easier to see in the rule list window exactly what fields have modifications made to them, vs having to open a rule and then go through the programming to see what's being modified.

Okay, if that's easier for you. The tradeoff is that making changes to the field formatting may be more work, because you may have to edit multiple rules instead of having the logic all in one place.

The core issue, and the reason for asking the initial question. Is being able to duplicate a rule, and just change the rule name to modify a new field. Which doesn't seem like a feature available. We can adjust our workflow to work around it.

I suppose that could be an enhancement.

 

I do appreciate you taking the time to explain the use case. This kind of feedback is invaluable to us. So thanks for the discussion.

Edited by Dan Korn
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...