Jump to content

Insert string based on combination of two fields


Recommended Posts

Searched the forum and didn't find an example of what I'm looking for and am having trouble on my own, so here's my problem:

 

One of three things needs to appear in a text body based on two fields (Field “A” and “B” for this example). If A is blank and B contains data, return sentence 1; if A contains data and B is blank, return sentence 2; if A and B both contain data, return sentence 3.

 

So this part is easy enough:

 

if (Field("A") == "")

return "";

else

return 'sentence 1 ' +Field("A");

 

The problem is sentence 3. If I use it as written above and make three rules, sentence 1 and 2 would return along with sentence 3. I’ve played with things like the below, which validates but doesn't work as intended:

 

 

if ((Field("totalkwh") == null) && (Field("totaltherm") != null))

return "";

else

return 'sentence 1 ' +Field("A");

 

 

Doing it as three rules also seems unnecessary, is there a way to write this into one rule?

 

 

I'm still learning JavaScript so forgive me if something simple is wrong. I've tried to solve this on my own and am just not sure why it isn't working, thanks for any assistance.

 

FusionPro 9.3.15

Acrobat 10.1.13

MacOS 10.10.2 Yosemite

Link to comment
Share on other sites

It sounds like you just need to use an 'else if' statement within your 'if' statement. I've assigned variables for you to edit with your sentences and field names. The first 'if' statement checks to see if 'a' and 'b' both have data, if it does, it sets the result to sentence3. If it doesn't it moves on to the 'else if' statement where it checks to see if 'b' has data and sets the result to sentence1. If 'b' doesn't have data, it finally checks to see if field 'a' has data and sets the result to sentence2. If they're all null, it returns sentence2 as a fallback. You could account for this by adding another 'else if'.

var a = "a"; // Your Field
var b = "b"; // Your Field
var sentence1 = "Sentence 1"; // Your sentence1
var sentence2 = "Sentence 2"; // Your sentence2
var sentence3 = "Sentence 3"; // Your sentence3

var result = "";
if (a && b){
   result = sentence3;
}
else if(b){
   result = sentence1;
}
else {
   result = sentence2;
}

return result;

 

Or more succinctly:

var a = "a"; // Your Field
var b = "b"; // Your Field
var sentence1 = "Sentence 1"; // Your sentence1
var sentence2 = "Sentence 2"; // Your sentence2
var sentence3 = "Sentence 3"; // Your sentence3

var result = (a && b) ? sentence3 : (b) ? sentence1 : sentence2;
return result;

Link to comment
Share on other sites

This worked for me. Replacing the necessary fields and sentences you plan to use.

 

Code:

//if A is blank and B is not, use sentence 1

//if B is blank and A is not, use sentence 2

//if A and B both contain data, return sentence 3

 

if (Field("A") == "" && Field("B") != "")

return "sentence 1"

else if (Field("B") =="" && Field("A") !="")

return "sentence 2"

else if (Field("B") !="" && Field("A") !="")

return "sentence 3"

else

return ""

 

Another way to skin a cat I guess...

Link to comment
Share on other sites

That's the tree I was trying to bark up! I wasn't too far off, just screwed up some syntax and needed to correctly use the else if statement. It's great to see it done both ways, I can compare that to the "succinct" version from step, really helpful. Thanks!
Link to comment
Share on other sites

That's the tree I was trying to bark up! I wasn't too far off, just screwed up some syntax and needed to correctly use the else if statement. It's great to see it done both ways, I can compare that to the "succinct" version from step, really helpful. Thanks!

Well, at the risk of muddying the waters, I feel compelled to point out that the "else if" chain isn't really needed there. Or at least the "else"s are redundant. This code works exactly the same way, with the "else"s either commented out or removed:

//if A is blank and B is not, use sentence 1
//if B is blank and A is not, use sentence 2
//if A and B both contain data, return sentence 3

if (Field("A") == "" && Field("B") != "")
   return "sentence 1";
//else
if (Field("B") =="" && Field("A") !="")
   return "sentence 2";
//else
if (Field("B") !="" && Field("A") !="")
   return "sentence 3";
//else
return "";

If you think about it, you can see why: Once we hit a "return" statement, we're done. But if we don't return, we just go to the next statement, whether there's an "else" or not.

 

This can be simplified a bit more, like so:

var A = Field("A");
var B = Field("B");
if (!A && B)
   return "sentence 1";
if (!B && A)
   return "sentence 2";
if (B && A)
   return "sentence 3";
return "";

Which can in turn be rearranged as:

var A = Field("A");
var B = Field("B");
if (A && B)
   return "sentence 3";
if (!A && B)
   return "sentence 1";
if (A && !B)
   return "sentence 2";
return "";

And further reduced to:

var A = Field("A");
var B = Field("B");
if (A && B)
   return "sentence 3";
if (B)
   return "sentence 1";
if (A)
   return "sentence 2";
return "";

This is very similar to step's code, which is essentially:

var A = Field("A");
var B = Field("B");
return A && B ? "sentence 3" : B ? "sentence 1" : "sentence 2";

Although step's code is not accounting for the case which you also don't account for in the original description of the problem, which is the fourth case, where both A and B are empty. To handle that, you need to modify step's code thusly:

var A = Field("A");
var B = Field("B");
return A && B ? "sentence 3" : B ? "sentence 1" : A ? "sentence 2" : "";

Which is equivalent to the last rule where the third line starts with "if", above.

 

WARNING! EXTREMELY NERDY COMPUTER SCIENCE AHEAD!

 

The ultimate reduction of this logic is a two-dimensional Boolean truth table, where the two Boolean (true/false, or 1/0) inputs combine to generate four possible output values, in a 2x2 matrix, or a two-dimensional array. That looks like this in JavaScript:

var A = Field("A");
var B = Field("B");
return [["","sentence 2"],["sentence 1","sentence 3"]][!!B*1][!!A*1];

But it's probably not worth spending too much effort trying to understand that. Step's solution with the chained ternary ? : operators is arguably more readable, if not quite as succinct.

Edited by Dan Korn
Link to comment
Share on other sites

WARNING! EXTREMELY NERDY COMPUTER SCIENCE AHEAD!

 

The ultimate reduction of this logic is a two-dimensional Boolean truth table, where the two Boolean (true/false, or 1/0) inputs combine to generate four possible output values, in a 2x2 matrix, or a two-dimensional array. That looks like this in JavaScript:

var A = Field("A");
var B = Field("B");
return [["","sentence 2"],["sentence 1","sentence 3"]][!!B*1][!!A*1];

 

Thanks for sharing this solution, Dan! Admittedly, it took me a few minutes to wrap my head around what was actually going on there but that's very clever. And since I'm itching to try out some of the logic you presented there, I'll offer up yet another way to skin the cat (this time taking into consideration the default null value when both fields are empty):

var a = Field("A");
var b = Field("B");

var sentence = [];
sentence[1] = "Sentence 1";
sentence[2] = "Sentence 2";
sentence[3] = "Sentence 3";

return sentence[(+!!a*2 + +!!b)] || '';

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