# Simple If Rule

## Recommended Posts

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,"

##### Share on other sites

A couple of problems I notice right off the bat. First, in this 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:

```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:

```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:

```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:

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:

```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:

```//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:

```//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")  + ",";
```

##### Share on other sites

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!

##### Share on other sites

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?

##### Share on other sites

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:

```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.

##### Share on other sites

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?

##### Share on other sites

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:

```var WordsToMaintainCase = ["AW", "PTI", "MarcomCentral", "JavaScript"]; // etc., whatever you want here
for (var i in WordsToMaintainCase)
{
var TextToReplace = WordsToMaintainCase[i];
}```

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×

• #### FusionPro.com

• MarcomCentral
×
• Create New...