MarcomCentral (PTI) and FusionPro User Communities Simple If Rule
 Register FAQ Calendar Search Today's Posts Mark Forums Read

#1
June 4th, 2019, 06:09 AM
 AWhite2504 Junior Community Member Join Date: May 2019 Posts: 29
Simple If Rule

Trying to create a rule that substitutes different salutations,

If Salutation is Present, it should be Salutation + Last name.
If Salutation is not present it should default to First Name.
If no first name is present it should default to "Friend"

The first two functions work, but the "Friend" line does not. Just returns a blank value.

if(Field("SALU") =="")
return Field("First Name")+(",")
else
return Field("SALU" )+"" + Field("Last Name")+(",")
if (Field ("SALU") & Field ("Last Name") & Field ("First Name") =="")
return "Friend,"
#2
June 4th, 2019, 12:38 PM
 Dan Korn FusionPro Senior Engineer / Forum Moderator Join Date: Aug 2008 Location: Chicago, IL Posts: 4,592
Re: Simple If Rule

A couple of problems I notice right off the bat. First, in this code:
Code:
`if (Field ("SALU") & Field ("Last Name") & Field ("First Name") =="")`
You need to use a double ampersand && for a logical "and". (A single ampersand is a "bitwise and," which is a math thing.)

Also, in these four lines of code:
Code:
```if(Field("SALU") =="")
return Field("First Name")+(",")
else
return Field("SALU" )+"" + Field("Last Name")+(",")```
You have only two possible outcomes (branches), both of which return (exit) from the rule. So no code at all after those four lines will ever be executed.

Though the "else" in there also has no effect at all, since the first "if" either results in a return (exit) from the rule, or its continuation to the next statement. Therefore this is equivalent:
Code:
```if(Field("SALU") =="")
return Field("First Name")+(",")

return Field("SALU" )+"" + Field("Last Name")+(",")```
This pattern of "if a condition is met, then return, (else) continue" can be repeated as many times as needed to yield as many possible outcomes as desired. So if you want three possible outcomes, it would look something like this:
Code:
```if (condition A)
return A;

if (condition B)
return B;

if (condition C)
return C;
// ...
return D; // (default)```
Now, what I like to do for rules like this is first describe the requirements in pseudo-code, which really means to just talk about them in English. You've mostly done that already with this description:
Quote:
 Originally Posted by AWhite2504 If Salutation is Present, it should be Salutation + Last name. If Salutation is not present it should default to First Name. If no first name is present it should default to "Friend"
I would rearrange that just a bit:
Code:
```If Salutation is Present:
it should be Salutation + Last name.
OTHERWISE (i.e. If Salutation is not present):
If first name is present,
it should default to  First Name.
OTHERWISE:
it should default to "Friend"```
Now that we have this well-defined in pseudo-code, the actual code follows directly from it:
Code:
```//If Salutation is Present, it should be Salutation + Last name.
if (Field("SALU"))
return Field("SALU") + " " + Field ("Last Name") + ",";

//If first name is present, it should default to  First Name.
if (Field("First Name"))
return Field("First Name") + ",";

// Otherwise (If no first name is present), it should default to "Friend"
return "Friend,";```
That, I believe, should do what you want.

I'll add that, if you want to be slightly more succinct, you could do this:
Code:
```//If Salutation is Present, it should be Salutation + Last name.
if (Field("SALU"))
return Field("SALU") + " " + Field ("Last Name") + ",";

//If first name is present, it should default to  First Name.
// Otherwise (If no first name is present), it should default to "Friend"
return (Field("First Name") || "Friend")  + ",";```
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
#3
June 4th, 2019, 01:18 PM
 AWhite2504 Junior Community Member Join Date: May 2019 Posts: 29
Re: Simple If Rule

That worked!!

Thank you for the in-depth breakdown. I am new to the forum, Javascript and FusionPro. This forum has REALLY helped me with the transition!
#4
June 12th, 2019, 06:56 AM
 AWhite2504 Junior Community Member Join Date: May 2019 Posts: 29
Re: Simple If Rule

Just realized this rule will need an add on for Salutations that do not have the appropriate punctuation.

For Example: Mrs->Mrs. etc...

Have this as a simple if rule:
var Mrs = Field("SALU");
if (Mrs == "") return "Mrs.";
//else
return " " + Mrs;

Will I need to do this for every salutation within my data file?
#5
June 12th, 2019, 03:13 PM
 Dan Korn FusionPro Senior Engineer / Forum Moderator Join Date: Aug 2008 Location: Chicago, IL Posts: 4,592
Re: Simple If Rule

Quote:
 Originally Posted by AWhite2504 Will I need to do this for every salutation within my data file?
I haven't seen your data file, so I can't say whether what you have there is correct, nor whether you would need it for (some unknown set of) other values. These specific questions are hard to answer without seeing the template, or at least the data.

But I think you might want something like this:
Code:
```var salutation = Field("SALU");

// If no salutation:
// If first name is present, it should default to  First Name.
// Otherwise (If no first name is present), it should default to "Friend"
if (!salutation)
return (Field("First Name") || "Friend") + ',';

// Make sure salutation has a trailing dot.
if (Right(salutation, 1) != '.')
salutation += '.';

//If Salutation is Present, it should be Salutation + Last name.
return salutation + ' ' + Field ("Last Name") + ',';```
I would also advise you to examine your assumptions about how salutations work. For instance, some honorifics come after the last name, such as Esq. or DDS, and not all have trailing dots. You're getting into territory where it's hard to write a computer algorithm to account for all the possibilities.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
#6
June 13th, 2019, 08:48 AM
 AWhite2504 Junior Community Member Join Date: May 2019 Posts: 29
Re: Simple If Rule

Thanks Dan!!!!

I have some names that are like "AW Cleaning".Is there a rule that will keep that capitalized instead of returning Aw Cleaning?
#7
June 13th, 2019, 02:11 PM
 Dan Korn FusionPro Senior Engineer / Forum Moderator Join Date: Aug 2008 Location: Chicago, IL Posts: 4,592
Re: Simple If Rule

Quote:
 Originally Posted by AWhite2504 I have some names that are like "AW Cleaning".Is there a rule that will keep that capitalized instead of returning Aw Cleaning?
Well, this is a different question now. I assume you're asking about the ToTitleCase function, and how to program exceptions into it for things like acronyms, or names like "McDonald", or Roman numerals, which should not be subject to the usual title case logic of "the first letter of each word is upper-case, and the rest are lower-case." This has been discussed many, many times on this forum, in many threads, including these:

As those threads show, while you can certainly program a finite list of exceptions to title casing, there is no way to program a computer algorithm to completely emulate the often arbitrary logic of capitalization to handle all possible eventualities of names that you might encounter in the future. The "rules" for capitalization of names are based more on convention and preference than on logic. If you already have data which is in all upper-case (such as from a postal sort), there's no 100 percent reliable way to convert it back to the original casing. In other words, converting text from mixed-case to all upper-case is a lossy conversion, and there's no way to get back the information that was lost in that process. Your best bet, as always, is to get the data in its original format, before any conversions have been done.

All that being said, in FusionPro 10.1, you can use the new Text Replacement feature to force all instances of a particular word or phrase to be set in a particular case (or apply any transformation you want). From the New Rule dialog, you can select the "Preserve Case of Word or Phrase" rule, and add whatever you want in there. If you have multiple of these, you can make multiple copies of the rule, but you may find it easier to just add them to an array, like so, in OnJobStart:
Code:
```var WordsToMaintainCase = ["AW", "PTI", "MarcomCentral", "JavaScript"]; // etc., whatever you want here
for (var i in WordsToMaintainCase)
{
var TextToReplace = WordsToMaintainCase[i];
}```
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});

 Tags simple if