Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
11

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

Community Beginner ,
Mar 17, 2024 Mar 17, 2024

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

TOPICS
JavaScript , PDF forms
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 19, 2024 Mar 19, 2024

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 

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2024 Mar 17, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 18, 2024 Mar 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 18, 2024 Mar 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 18, 2024 Mar 18, 2024

Hi Thom, thank you for responding....actually alot of your solutions have helped me get the rest of what I needed done, so am a bit of a fan haha!
That sounds like it will work, I will have a play with it, in the mean time I have uploaded a copy of the specific fields I had been looking at to get my solution, hopefully not necessary any longer! Thank you!

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 18, 2024 Mar 18, 2024

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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 19, 2024 Mar 19, 2024

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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 27, 2024 Mar 27, 2024
LATEST

My sincere apologies for the late reply to this (I was in hospital for a couple of days and have just got back to this) and thank you so much, this looks like it is working brilliantly!

 

Thank you!

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