Skip to main content
New Participant
March 18, 2024
Answered

Force a drop down field to commit values based on another field selection

  • March 18, 2024
  • 1 reply
  • 2263 views

Hi all, 

I have looked everywhere for a solution that I think should be easy but I dont think it is, so long time reader, 1st time poster!

 

I have been tasked with calculating a product price which depends on 5 variables (3 drop down fields and 2 calculated text fields)

 

I have a solution which may not be the greatest where:

 

  1. I have a hidden text field that combines the values of all 5 fields to get a unique identifier with a comination of letters and numbers. - Text9
  2. I have added these unique indetifiers (94 in total) to a hidden drop down field with the export value of the price - PricingExport
  3. The value of PricingExport drop down is selected based on a custom calculation getting the value from Text9
  4. I have another text field TotalPrice to get the export value of the PricingExport drop down.

 

This all works well except that the export value in TotalPrice only mirrors the unique Identifier, not the export value that I want - the export value only shows if I remove the calculation and manually make selctions in the drop down.

 

Is there a code I can add to the actions of the 5 required fields that will force the PricingExport field to commit its value, as if it was selected manually?

Or is there a better way to acheive this? I am happy to share the current codes I just thought that it may make it very messy!

Thank you in advance

This topic has been closed for replies.
Correct answer Nesa Nurani

Sorry, me again! Really appreciate your help, I am afraid I need more 😞

 

I have added two fields with your suggested scripts, and they dont seem to calculate, I am attaching the updated document here, am I putting them in the wrong spot? Thank you 


You didn't put quotes for 'sku' in aProducts list, but actually you can remove 'sku' because it is not needed.

You can use a list like this:

var aProducts = [ 
{rating:"fire",height:"2070",width:"830",style:"FastFix",assembly:"welded",price:"315"},
{rating:"fire",height:"2070",width:"1130",style:"FastFix",assembly:"welded",price:"315"},
{rating:"fire",height:"2070",width:"1430",style:"FastFix",assembly:"welded",price:"320"}];//...etc

and then add this script:

var fire = this.getField("FireRated").valueAsString;
var height = this.getField("HeightCalc").valueAsString;
var width = this.getField("WidthCalc").valueAsString;
var assemb = this.getField("Assembly").valueAsString;
var pType = this.getField("ProfileType").valueAsString;
event.value = "";
for(var i in aProducts){
 if(fire == aProducts[i].rating && height == aProducts[i].height && width == aProducts[i].width && assemb == aProducts[i].assembly && pType ==aProducts[i].style)
 event.value = aProducts[i].price;}

Here is your file with changes made:

https://drive.google.com/file/d/1NlexJk4PPP9BlsUICuwGftr6nnYxJUkX/view?usp=sharing 

1 reply

Nesa Nurani
Community Expert
March 18, 2024

There is an option in dropdown field under 'Options' tab to 'Commit selected value immediately' see if you checked that.

If you have multiple calculation scripts, check your field calculation order.

There is probably an easier way to achieve this, if you can show us an example we can help you.

New Participant
March 18, 2024

Thank you Nesa for looking at this. I have checked the 'Commit selected value immediately' and the calculation order and it apears to be correct unfortunately! I will spend some time this morning and put just the fields I need help with into its own file and attach for reference, I am sure you are right and there should be an easier way haha! Thanks again for your help

Thom Parker
Community Expert
March 18, 2024

It sounds like you are using the dropdown to map the "product variables" to a price value. There is a better way to do this, which is to use custom document level object to do the mapping.

 

For example, here's an array of objects that each provide a number of product properties.

 

var aProducts = [ {name:"prod1", size:"large", style:"clasic", color:"red", price:"40", skew:144},

                               {name:"prod1", size:"medium", style:"clasic", color:"blue", price:"38", skew:145},

                          .... 

                           ];

 

Now a product can be found from a set of input parameters by searching this array of objects. 

 

var cName = this.getField("ProdName").valueAsString;

var cSize = this.getField("ProdSize").valueAsString;

var cColor = this.getField("ProdColor").valueAsString;

var cStyle = this.getField("ProdStyle").valueAsString;

 

var oProd = aProducts.find(a=>a.name == cName && a.size == cSize && a.color==cColor && a.style==cStyle);

if(oProd)

    event.value = oProd.price;

else

   event.value = 0;

 

  

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often