Highlighted

Script to determine value of text box

New Here ,
Aug 18, 2020

Copy link to clipboard

Copied

Good day,

I am attempting to create an interactive PDF form, but I have absolutely no coding background whatsoever. That being said, what I would like to do is fairly simple.

 

I have set up a form with several of radio buttons with values associated. In each row, I have a text box (Text01, Text02, etc.) that displays the value selected by the corresponding radio button. At the bottom of the page, there is a text box that totals all of the text boxes and displays that number (TextTotal).

 

What I would like is for a separate text box (TextResult) to display a string of conditionally formatted text based on the value of TextTotal. For example

 

if TextTotal <= 5, then TextResult = "Green";

if TextTotal  >5 but <= 10, then TextResult = "Yellow";

if TextTotal <10, then TextResult = "Red"

 

How, and in what field would I program this??

 

Thanks so much!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

As the custom calculation script of TextResult enter the following code:

 

var v = Number(this.getField("TextTotal").valueAsString);
if (v<=5) {
	event.value = "Green";
	event.target.textColor = color.green;
} else if (v<=10) {
	event.value = "Yellow";
	event.target.textColor = color.yellow;
} else {
	event.value = "Red";
	event.target.textColor = color.red;
}

 

TOPICS
Create PDFs, PDF forms

Views

120

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Script to determine value of text box

New Here ,
Aug 18, 2020

Copy link to clipboard

Copied

Good day,

I am attempting to create an interactive PDF form, but I have absolutely no coding background whatsoever. That being said, what I would like to do is fairly simple.

 

I have set up a form with several of radio buttons with values associated. In each row, I have a text box (Text01, Text02, etc.) that displays the value selected by the corresponding radio button. At the bottom of the page, there is a text box that totals all of the text boxes and displays that number (TextTotal).

 

What I would like is for a separate text box (TextResult) to display a string of conditionally formatted text based on the value of TextTotal. For example

 

if TextTotal <= 5, then TextResult = "Green";

if TextTotal  >5 but <= 10, then TextResult = "Yellow";

if TextTotal <10, then TextResult = "Red"

 

How, and in what field would I program this??

 

Thanks so much!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

As the custom calculation script of TextResult enter the following code:

 

var v = Number(this.getField("TextTotal").valueAsString);
if (v<=5) {
	event.value = "Green";
	event.target.textColor = color.green;
} else if (v<=10) {
	event.value = "Yellow";
	event.target.textColor = color.yellow;
} else {
	event.value = "Red";
	event.target.textColor = color.red;
}

 

TOPICS
Create PDFs, PDF forms

Views

121

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 18, 2020 0
Most Valuable Participant ,
Aug 18, 2020

Copy link to clipboard

Copied

As the custom calculation script of TextResult enter the following code:

 

var v = Number(this.getField("TextTotal").valueAsString);
if (v<=5) {
	event.value = "Green";
	event.target.textColor = color.green;
} else if (v<=10) {
	event.value = "Yellow";
	event.target.textColor = color.yellow;
} else {
	event.value = "Red";
	event.target.textColor = color.red;
}

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 18, 2020 0
Contributor ,
Aug 18, 2020

Copy link to clipboard

Copied

And what if "TextTotal" is empty? 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 18, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

In the code above it will be treated the same as zero.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

Moved

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

Fantastic! Thanks so much. I do have one more question. 

 

I am making a risk/authorization matrix. Each radio button has a value of 1, 2, or 3 which corresponds to the the riskiness of the activity (3 being the most). The form accumulates these points and determines who the authority to approve based on the score (TextTotal). However, if more than 2 instances of the value 3 occurs, the total score is ignored and that will govern the authorization level outcome.

 

How can I incorporate that?

 

Thanks so much! This community is awesome.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

You need to create two counter variables in your code: One for the general total and one for the total of "3" options selected. At the end, if the value of the first one is above the threshold OR the value of the second one is above 2 then show the desired level outcome, otherwise proceed as usual. If you post your current code I can help you adjust it.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

Thank you so much. This is really helping me out!

 

var v = Number(this.getField("TotalScore").valueAsString);
if (v<=5) {
event.value = "FAO";
event.target.textColor = color.green;
} else if (v<=10) {
event.value = "COS Ops / SOA OC";
event.target.textColor = color.yellow;

} else if (v>10) {
event.value = "CO";
event.target.textColor = color.red;
} else {
event.value = "FAO";
event.target.textColor = color.green;
}

 

If the indents don't show up, please let me know what how to do that in future because I think I'll be leveraging the help of these forums more and more knowing how responsive they are. I realse what is above probably has some redundancy in it (the FAO return string), but I couldn't make it behave the way I wanted any other way.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

How are you calculating the value of TotalScore, though?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

I am using the "Value is the [sum (+)] of the following fields" on the calculate tab and then I've just checked all the radio button groups.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

OK, then you'll either need to use a script for it, which will also set the value (and color) of this other field, or you could create a separate (hidden) field to count the number of times "3" was selected.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

Alternatively, I guess it can be done directly in this calculation script, without an extra field.

What are the names of the radio-button groups you're counting?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

I suspect it might be better to do it via a script, but as discussed, I am a pretty hopeless coder.

 

Anyway, here is a screen shot of my radio button groups.

 

https://drive.google.com/file/d/14M2lkF1_cgQ64fsnHe27I7ER0FWBH47H/view?usp=sharing

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

I will create an array and you'll need to fill them in manually. So what should be the result if more then two of those fields have the value "3" selected? Also, the last else-clause in your code is not necessary and can be removed, as the other conditions cover all possible values.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

Assuming it should be FAO, use this code:

 

var fields = ["HoursFlown", "OpTempChoice", ...];
var counter = 0;
for (var i in fields) {
	var f = this.getField(fields[i]);
	if (f.valueAsString=="3") counter++;
}
if (counter> 2 || v<=5) {
	event.value = "FAO";
	event.target.textColor = color.green;
} else if (v<=10) {
	event.value = "COS Ops / SOA OC";
	event.target.textColor = color.yellow;
} else if (v>10) {
	event.value = "CO";
	event.target.textColor = color.red;
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

Thanks for this! I’m away from my desk for the next few hours but I’ll try
it out when I get back.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

I'll try to work below with what you gave me, but the rules are basically as fllows.

 

If there are less than or equal to 2 instances of the "3" as a value, it should return "COS Ops / SOAC OC"

 

If there are greater than 2 instances of the "3" as a value, it should return "CO".

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

In that case the sum doesn't matter, as the final value is only determined by the number of 3's...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

Yeah correct.

 

For the purpose of this discussion, let the number of "3s" equal "red"

 

An "FAO" can authorize if the total score is <5 and/or 1 red.

The "COS Ops" can authorize if the total score is <10 and/or <= 2 red.

The "CO" must authorize if the total score is >10 and or >2 red.

 

And as an added curve ball, I'd like another rule... say I have a value of "4" is "black".

 

Regardless of the score, if there is any "4s", then the field would return "Outside CO ability to Authorize".

Here is the code I just put in - it seems to have broke it.

var fields = ["HoursFlown", "OpTempChoice", "CrewRestChoice", "FARScoreChoice", "ProfChoice", "Currency Choice", "VFRDayChoice", "VFRNightChoice", "IFRChoice", "TempDAChoice", "IllumChoice", "ProfileChoice", "CrewChoice", "NoticeChoice", "PerfChoice", "DefectsChoice"];
var counter = 0;
for (var i in fields) {
var f = this.getField(fields[i]);
if (f.valueAsString=="3") counter++;
}
if (v<=5) {
event.value = "FAO";
event.target.textColor = color.green;
} else if (counter= 2 || v<=10) {
event.value = "COS Ops / SOA OC";
event.target.textColor = color.yellow;
} else if (counter> 2 || v>10) {
event.value = "CO";
event.target.textColor = color.red;
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

Yeah absolutely - the 3 contributes to the total score, but a 3 is also the score for "high risk". So if there are more than one item that is high risk, then it jumps up the authority ladder.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

The comparison operator is "==", not "=".

Change:

} else if (counter= 2 || v<=10) {

To:

} else if (counter==2 || v<=10) {

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

I fixed that, however now the field isn't returning any value no matter the score.

This code I saved was getting me the "points" results I want, but not the counter results obviously.

 

var v = Number(this.getField("TotalScore").valueAsString);

if (v<=5) {

                event.value = "FAO";

                event.target.textColor = color.green;

} else if (v<=10) {

                event.value = "COS Ops / SOA OC";

                event.target.textColor = color.yellow;

} else if (v>10) {

                event.value = "CO";

                event.target.textColor = color.red;

} else {

                event.value = "FAO";

                event.target.textColor = color.green;

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
Most Valuable Participant ,
Aug 19, 2020

Copy link to clipboard

Copied

I'll need to see the actual file to be able to help you further with this.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0
New Here ,
Aug 19, 2020

Copy link to clipboard

Copied

Sure! Thanks so much for helping with this.

I just started a JavaScript course over at codeacademy so I can be less helpless.

 

You can find the file here

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 19, 2020 0