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

Function to retrieve and assign values to a number of form fields using a multi-dimensional array

Engaged ,
Feb 12, 2022 Feb 12, 2022

I believe I need another set of eyes to review the script (provided below) for a function (script) intended to assign values to a number of form fields that rely solely upon the use of a multi-dimensional array. For some odd reason, the form fields only display the initialized values assigned to the form fields by the variables created/associated inside the script. Other than that, I am not getting any error messages regarding syntax. For whatever reason, I just can't seem to identify the problem with the script provided below. Any comments, suggestions. regarding the script are most appreciated. Thank you ahead of time.

 

// function to calculate /assign form field values using a multi-dimensional array
function calculatePrices() {
var systemType = getField("systemType."+i);
var quantity = this.getField("quantity."+i);
var agreeType = getField("agreeType."+i);
var agreeYear = getField("agreeYear."+i);

//create and initialize variables for form fields
var year1 = 0;
var year2 = 0;
var year3 = 0;
var year4 = 0;
var year5 = 0;
var year6 = 0;
var inspectionCharge = 0;
var total = 0;

if(systemType.value > -1 && agreeType.value > -1 && agreeYear.value > -1) {
if(agreeType.value==0){
year1 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][0];
year2 = aSystemsPricing[systemType.value][agreeType.value][agreetYear.value][1];
year3 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][2];
year4 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][3];
year5 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][4];
year6 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][5];
total = quantity.value * (year1 + year2 + year3 + year4 + year5 + year6);
}
if(agreeType.value==1){
year1 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][0];
year2 = aSystemsPricing[systemType.value][agreeType.value][agreetYear.value][1];
year3 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][2];
year4 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][3];
year5 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][4];
total = quantity.value * (year1 + year2 + year3 + year4 + year5 + inspectionCharge);
}
if(agreeType.value==2){
year1 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][0];
year2 = aSystemsPricing[systemType.value][agreeType.value][agreetYear.value][1];
year3 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][2];
year4 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][3];
year5 = aSystemsPricing[systemType.value][agreeType.value][agreeYear.value][4];
total = quantity.value * (year1 + year2 + year3 + year4 + year5);
}
}

//assign pricing to calculated form fields
getField("year1."+i).value = year1;
getField("year2."+i).value = year2;
getField("year3."+i).value = year3;
getField("year4."+i).value = year4;
getField("year5."+i).value = year5;
getField("year6."+i).value = year6;
getField("inspect."+i).value = inspectionCharge;
getField("total."+i).value = total;
}

TOPICS
General troubleshooting , How to , JavaScript , PDF forms
6.5K
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
Engaged ,
Feb 12, 2022 Feb 12, 2022

Sorry, but I neglected to add that the function takes the form calculatePrices(i) called from a number of form fields where the variable 'i' contains one of three digits, namely 0, 1, or 2 to identify the associated form field, i.e., systemType.0, systemType.1, systemType.2, etc.

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 ,
Feb 13, 2022 Feb 13, 2022

You're missing the declaration of the "i" parameter in the function definition, though.

It should be:

 

function calculatePrices(i) {

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
Engaged ,
Feb 13, 2022 Feb 13, 2022

Sorry, while I neglected to show the paramter "i" in my initial post, I do have it entered as function calculatePrices(i) so this isn't the issue. In reality, if the parameter were missing, this would have no doubt resolved my issue. Unfortunately, while I was hoping this to be the case, it apparently isn't. I even reviewed the actions where the function is called from inside the PDF form and the syntax is correct where calculatePrices(0), calculatePrices(1), and calculatePrices(2) are used. As such, my issue appears to be do to something else and I am losing my mind trying to resolve it. Thank you for the input, since your reply was intuitive based on the missing paramter provided in my initial 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 ,
Feb 13, 2022 Feb 13, 2022

I think we'll need to see the file in order to be able to help you further with this.

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 ,
Feb 12, 2022 Feb 12, 2022

What are the differences between the values of agreeType?

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
Engaged ,
Feb 13, 2022 Feb 13, 2022

agreeType values as they appear in the drop down including texport values (in array format) are as follows: [[Select Type],[Annual Service],[Late Annual Service],[Renewal Pricing]]. Refer to image file attachment to see what the user sees from inside the PDF form.

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
Engaged ,
Feb 13, 2022 Feb 13, 2022

Given my last reply, I neglected to provide the export values for each item in the agreeType drop down box. It should have read [["Select Type",-1],["Annual Service",0],["Late Annual Service",1],["Renewal Pricing",2]]. Sorry about that. 

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 ,
Feb 13, 2022 Feb 13, 2022

To debug the script add cmds like this:

console.println(agreeType.value);

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
Engaged ,
Feb 14, 2022 Feb 14, 2022

For whatever reason, I have still been unable to resolve the script issue I have been experiencing as there do not appear to be any issues using the debugger provided by Acrobat. I am now wondering if the PDF form initially provided has anything to do with the beheavior of the scripts I have implemented or modified reason being the PDF was created using Acrobat PDF Maker 11 for Word back in 2016 and not created using Acrobat Pro to begin with. While I have tried removing all form fields and scripts and then exporting the PDF to a postscript (ps) file to create a new PDF from the ps file (in an effort to save time and prevent from having to redo the entire original 5-page form design/layout in a word processor) using Acrobat, this doesn't appear to have changed a thing. On another note, given all the complaints/issues I have read online to date regarding MS PDF Maker, I am wondering if there exists something inherent in a PDF created using MS PDF Maker that acts to interfere with the intended performance of any scripts to be modified/added to the PDF using Acrobat Pro 2017. To be reiterate, the PDF I am using was initially created using MS Word and converted to PDF fomat using Acrobat PDF Maker 11 for Word back ni 2016. Any thoughts, comments at this time, regarding this subject matter are most appreciated.

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
Engaged ,
Feb 14, 2022 Feb 14, 2022

Upon further investigation and testing, it has come to my attention that the original interactive PDF form created using Acrobat PDF Maker 11 in Word appears to have nothing to due with the main issue I have been experiencing as descibed in my previous comments. To simplify things, I thought it might be beneficial to create a short consolidated version of the PDF form (attached hereto) to provide a better understanding of the form and the behavior exhibited for those interested. In brief, the end user clicks to select a system, enters a quantity, then selects an agreement type that serves to populate a number of agreement years, i.e., 5-6 years, depending on the agreement type selected. One will also note that if the end user ends up selecting 'Late Annual Service', this particular selection adds another field titled 'Re-Certification Inspection Charge' to be completed later by an aurhorized company rep. If this value is not selected, the additional field is not provided. In taking the form for a test drive, the main issue appears to be one where for whatever reason the function 'calculatePrices' stops immediately after a number of variables are created and initialized to '0' as indicated by each year and total displaying '0' on the form whereby the script fails to calculate and populate the years with actual prices provided by a number of multi-dmensional arrays that can be easily viewed in opening the script titled 'initialize'. Anyone interested in taking a brief look at the attached PDF 1-page form and offering their comments, suggestions is most appreciated as I am fast appoaching my wits end. Thank you all ahead of time.

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 ,
Feb 15, 2022 Feb 15, 2022

Check errors in the Javascript console. There are undefined variables.

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
Engaged ,
Feb 15, 2022 Feb 15, 2022

Thank you for the response but for whatever reason, I am not seeing any undefined variables listed in the JS onsole. Can you by chance post an image file attachment of what you observed or provide the names of the undefined variables you refer to? Thank you ahead of time. Oh, I should also add that I presently commented out a script that appears to be responsible for unpredictable behavior I've been experiencing otherwise in the form that no longer occur with the script being disabled.

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 ,
Feb 15, 2022 Feb 15, 2022

I see this:

 

 TypeError: (intermediate value)[(intermediate value)] is undefined
118:Document-Level:initialize
ReferenceError: agreetype is not defined
45:Document-Level:calculatePrices
ReferenceError: populateAgreeYear is not defined
1:AcroForm:agreeYear.0:Annot1:OnFocus:Action1
TypeError: aSystemPrices is undefined
20:Document-Level:calculatePrices

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
Engaged ,
Feb 15, 2022 Feb 15, 2022

Sorry but in reviewing the code, I fail to see/understand the error:

TypeError: (intermediate value)[(intermediate value)] is undefined
118:Document-Level:initialize

I believe this is line 118 displaying a line in the aSystemsPricing array inside 'Initialize' that reads:

[1850.00, 1850.00, 1850.00, 0.00, 0.00],// 3 year service agreement

Boy, I am really at a loss for this one since there are commas placed between each and every item inside the array and between left-hand and right-hand brackets of each array on the same level as normally required. What say 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 ,
Feb 15, 2022 Feb 15, 2022

In the preferences of Acrobat you should enable the entry "Show console on errors and messages".

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 ,
Feb 15, 2022 Feb 15, 2022

There are errors in your code. You must place a comma between all items (at the same level) in an array, which you're not doing in the aSystemPrices variable under "initialize".

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 ,
Feb 15, 2022 Feb 15, 2022

Plus, you have a typo in line #45 of calculatePrices. You wrote:

 

if(agreeType.value==0 || agreetype.value==2)

 

But it should be:

 

if(agreeType.value==0 || agreeType.value==2)

 

Remember that JS is case-sensitive... There might be other issues, too.

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 ,
Feb 15, 2022 Feb 15, 2022

PS. It will save you a lot of code if you changed your variables to point to the values directly, instead of the fields themselves. That way you won't have to repeat the ".value" part each time you refer to them...

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
Engaged ,
Feb 15, 2022 Feb 15, 2022

While I want to thank everyone who responded and offered their comments, suggestions regarding this matter, want to apologize for some of the errors like the typo in the abbreviated PDF I posted in an effort to provide the JS on my main PDF form. However I should state that this was not all copy and paste whereby I should have taken the necessary time reviewing the PDF for typo errors etc. b4 posting it as I have a physical hand impairment (even when typing this comment) whereby I often touch the wrong key on the keyboard or touch it too lightly whereby the intended letter, capitalization, etc. results in error. Once again sorry 'bout that. 

 

In response to Bernd's comment 'show console on errors and messages', I must admit I didn't have this checked under Edit > Preferences > Javascript. Thank you, done.

In response to ReferenceError: agreetype is not defined, this once again is my typing hand and haste to post the abbreviated PDF without checking for typos beforehand. Thank you anyway for pointing this out. I should also add that I am only too well aware that Javascript is case sensitive.

In regard to populateAgreeYear is not defined, this again was an inadvertent oversight on my part as I neglected to delete or comment out the function call as I should have when making code changes to the form. Thank you once again.

As for TypeError: aSystemPrices is undefined, this was due to copy and paste as the script was intentionally temporarily disabled on my main form in attempting to troublshoot unexpected behavior exhibited in other specific locations on the form.

In response to 'if(agreeType.value==0 || agreetype.value==2)', another typo I did overlook that appears on my main form. Thank you. A good example that two eyes are better than one and realizing that a lack of sleep and constantly looking at this stuff is a recipe for disaster. Thanks again.

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
Engaged ,
Feb 15, 2022 Feb 15, 2022

To begin, I want to thank you also for taking the time to review and comment on the abbreviated rendition of the code I posted on the forum.

In reponse to 'if(agreeType.value==0 || agreetype.value==2)', I must admit that this was an inadvertent oversight on my part as I am only too well aware of JS case sensitivity but for whatever reason I obviously overlooked this typo. In response to 'You must place a comma between all items (at the same level) in an array, which you're not doing in the aSystemPrices variable under "initialize". Correct me if I am wrong but I was under the impression rhat I did since I did not place a comma between the right-hand bracket (following the last element inside a lower level array) and left-hand bracket that begins an upper level array whereby these are the only locations in the multi-dimensional array that omit the comma?

Also, can you elaborate or provide an example given your last comment regarding changing variables to point to values directly instead of the fields themselves? Once again, thank you for your input it's greatly appreciated!

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 ,
Feb 15, 2022 Feb 15, 2022

- You're missing commas between Group 0 and Group 1 (line #5), and Group 1 and Group 2 (line #85).

- If you changed this line at the top of the code:

 

var systemType = getField("systemType."+i);

 

To this:

 

var systemType = getField("systemType."+i).value;

 

Then you could change all instances of systemType.value to just systemType later on.

Same with agreeType.value and agreeYear.value. This will make your code much easier to read.

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
Engaged ,
Feb 15, 2022 Feb 15, 2022

Just wanted to let both you and Bernd know that I have finally resolved all issues with the scripts to get my interactive PDF form to finally perform as intended. Needless to say I am estatic! Words cannot begin to convey my deep appreciation for both you and Bernd taking the time to field my many questions along with the feedback you both provided. You both must think I'm crazy but in reality you both are amazing. Thanks again for all your help regarding this matter. All I want to ask is, apart from being a little rusty, given as detailed and analytic a person as I am, am I alone when it comes to missing simple typos and specific syntax rules in reviewing code I have authored? As I have been away from JS for more than the last 10 years, aside from certain physical impairments suffered from a stroke back in 2016, and more than likely a lack of sleep, I'm not sure if I need a new set of eyes, glasses, or additional brain cells, lol!

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
Engaged ,
Feb 15, 2022 Feb 15, 2022

After everything I have done to address/correct the errors in the code, it appears that I have one last error (see below) to address/correct displayed by the console as follows:

TypeError: aSystemPrices[systemType.value][agreeType.value][agreeYear.value] is undefined
29:Document-Level:calculatePrices

for which I have atached the PDF for one last look. Sorry to be a PITA but I fail to see what's causing this issue, since it appeared all was resolved until I started testing the demo form once again and for whatever reason, I ventually received the error posted above. Once again, any comments, suggestions to address/correct this issue are most appreciated. Thank you ahead of time. 

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
Engaged ,
Feb 15, 2022 Feb 15, 2022

Oh, sorry, I forgot to attach the updated pilot-test-form.pdf. Here it is.

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