Populating Prices from Dropdown Box

New Here ,
Nov 30, 2017

Copy link to clipboard

Copied

Hi,

I have been trying to write a script that automatically populates a price in another field once an item in a dropdown box is selected. From looking at the tutorials, I have a good portion completed but I am running into the issue where every "cost" field is populated and I am not sure how to create a 1:1 selection with each row.

Example: desc1 populates for cost1

                desc2 populates for cost2

Code:

var cateringData = { "Large Cheese Board":{ cost: "165"},

                                 "Regular Cheese Board":{cost: "98"}};

function setFieldValues(cCateringName)

{

  this.getField("cost1").value = cateringData [cCateringName].cost;

  this.getField("cost2").value = cateringData [cCateringName].cost;

  this.getField("cost3").value = cateringData [cCateringName].cost;

  this.getField("cost4").value = cateringData [cCateringName].cost;

  this.getField("cost5").value = cateringData [cCateringName].cost;

  this.getField("cost6").value = cateringData [cCateringName].cost;

  this.getField("cost7").value = cateringData [cCateringName].cost;

  this.getField("cost8").value = cateringData [cCateringName].cost;

  this.getField("cost9").value = cateringData [cCateringName].cost;

  this.getField("cost10").value = cateringData [cCateringName].cost; }

Then this code is in the dropdown box field (desc1),

if( event.willCommit ) {

    if(event.value == "") this.resetForm(["cost1"]); else setFieldValues(event.value); }

Screen Shot 2017-11-30 at 5.03.33 PM.png

Any help is much appreciated!

Thanks!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

So you have one dropdown for each cost field? Then all you need is a simple way to specify the associated cost field, when a particular dropdown is selected.

Modify your script so the name of the associated field is passed into the function that modifies the field value

function setFieldValues(cCateringName, cCostFieldName)

{

  this.getField(cCostFieldName).value = cateringData [cCateringName].cost;

}

The dropdown code for the desc1 field includes the name of the field it will affect:

if( event.willCommit ) {

    if(event.value == "") this.resetForm(["cost1"]); else setFieldValues(event.value,"cost1"); }

I personally prefer self-aligning systems, where the target fields are derived from the name of the current field. For this you'd add code to the Dropdown keystroke code:

var cCostName = event.targetName.replace(/desc/,"cost");

This code automatically generates the name of the associated field.

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

213

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

Populating Prices from Dropdown Box

New Here ,
Nov 30, 2017

Copy link to clipboard

Copied

Hi,

I have been trying to write a script that automatically populates a price in another field once an item in a dropdown box is selected. From looking at the tutorials, I have a good portion completed but I am running into the issue where every "cost" field is populated and I am not sure how to create a 1:1 selection with each row.

Example: desc1 populates for cost1

                desc2 populates for cost2

Code:

var cateringData = { "Large Cheese Board":{ cost: "165"},

                                 "Regular Cheese Board":{cost: "98"}};

function setFieldValues(cCateringName)

{

  this.getField("cost1").value = cateringData [cCateringName].cost;

  this.getField("cost2").value = cateringData [cCateringName].cost;

  this.getField("cost3").value = cateringData [cCateringName].cost;

  this.getField("cost4").value = cateringData [cCateringName].cost;

  this.getField("cost5").value = cateringData [cCateringName].cost;

  this.getField("cost6").value = cateringData [cCateringName].cost;

  this.getField("cost7").value = cateringData [cCateringName].cost;

  this.getField("cost8").value = cateringData [cCateringName].cost;

  this.getField("cost9").value = cateringData [cCateringName].cost;

  this.getField("cost10").value = cateringData [cCateringName].cost; }

Then this code is in the dropdown box field (desc1),

if( event.willCommit ) {

    if(event.value == "") this.resetForm(["cost1"]); else setFieldValues(event.value); }

Screen Shot 2017-11-30 at 5.03.33 PM.png

Any help is much appreciated!

Thanks!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

So you have one dropdown for each cost field? Then all you need is a simple way to specify the associated cost field, when a particular dropdown is selected.

Modify your script so the name of the associated field is passed into the function that modifies the field value

function setFieldValues(cCateringName, cCostFieldName)

{

  this.getField(cCostFieldName).value = cateringData [cCateringName].cost;

}

The dropdown code for the desc1 field includes the name of the field it will affect:

if( event.willCommit ) {

    if(event.value == "") this.resetForm(["cost1"]); else setFieldValues(event.value,"cost1"); }

I personally prefer self-aligning systems, where the target fields are derived from the name of the current field. For this you'd add code to the Dropdown keystroke code:

var cCostName = event.targetName.replace(/desc/,"cost");

This code automatically generates the name of the associated field.

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

214

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
Nov 30, 2017 0
Adobe Community Professional ,
Nov 30, 2017

Copy link to clipboard

Copied

So you have one dropdown for each cost field? Then all you need is a simple way to specify the associated cost field, when a particular dropdown is selected.

Modify your script so the name of the associated field is passed into the function that modifies the field value

function setFieldValues(cCateringName, cCostFieldName)

{

  this.getField(cCostFieldName).value = cateringData [cCateringName].cost;

}

The dropdown code for the desc1 field includes the name of the field it will affect:

if( event.willCommit ) {

    if(event.value == "") this.resetForm(["cost1"]); else setFieldValues(event.value,"cost1"); }

I personally prefer self-aligning systems, where the target fields are derived from the name of the current field. For this you'd add code to the Dropdown keystroke code:

var cCostName = event.targetName.replace(/desc/,"cost");

This code automatically generates the name of the associated field.

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...
Nov 30, 2017 0
New Here ,
Dec 01, 2017

Copy link to clipboard

Copied

Awesome! Thank you so much for helping me.

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...
Dec 01, 2017 1
New Here ,
Dec 11, 2017

Copy link to clipboard

Copied

Hi,

I ran into an issue where if I try to add custom text to the dropdown fields, I get this error.

setFieldValues, script Document-Level:setField Values

Exception in line 2 of function top_level, script AcroForm:desc2:Keystroke

TypeError: cateringData[cCateringName] is undefined

74:Document-Level:setField Values

I’ve been trying to write a regular expression in the cateringData object as a “catchall” but I can’t get it working. Is their an easier way to accomplish this?

Thanks Again

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...
Dec 11, 2017 0
Adobe Community Professional ,
Dec 11, 2017

Copy link to clipboard

Copied

The error is caused by an undefined value. Have you checked this? Do you know why it is undefined?

What do you mean by a regular expression as a catchall? How is this RegEx used?

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...
Dec 11, 2017 0
New Here ,
Dec 12, 2017

Copy link to clipboard

Copied

Hi,

It is undefined because it is not one of the items in cateringData. I am trying to set it up, so if the item is not in the list we can still add it manually.

I was trying to use the RegEx like this,

var cateringData = { "Add Gluten Free":{ cost: "2"},

                     "Bacon,Egg,& Cheese Biscuit":{ cost: "8"},

                     "Banana Bread Loaf, 8 slices":{ cost: "28"},

                     "/*/": { cost: "/*/"}};

Thanks

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...
Dec 12, 2017 0
Most Valuable Participant ,
Dec 12, 2017

Copy link to clipboard

Copied

You can't use regular expressions with this kind of literal object. You

would need a different approach to be able to do that.

On 12 December 2017 at 22:26, campbellc36855280 <forums_noreply@adobe.com>

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...
Dec 12, 2017 0
Adobe Community Professional ,
Dec 12, 2017

Copy link to clipboard

Copied

So as try states, you can't use the regEx in this way.  But you can catch the error and respond to it.

To do this you'll need to break up the code that uses this list into two parts: 1) acquire object from catering data, 2) check for a valid value before using it.

So here is a redo:

function setFieldValues(cCateringName, cCostFieldName)

{

  var oItemData = cateringData [cCateringName];

  if(oItemData)

      this.getField(cCostFieldName).value = oItemData.cost;

  else

  { ... do something else ... }

}

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...
Dec 12, 2017 0
New Here ,
Dec 16, 2017

Copy link to clipboard

Copied

Thanks Thom!

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...
Dec 16, 2017 0