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

Help Setting Field Values from Array Values

Explorer ,
Sep 30, 2018 Sep 30, 2018

Copy link to clipboard

Copied

Hi all,

I am working on writing a doc-level function to be triggered when a particular page opens.

A salesperson enters information for warranties (up to 4) that a customer has purchased for their vehicle on the first page of the document. When the page for the Bill of Sale opens, I want the "Notes" section to populate with the warranty information (description and cost).

I don't want to take up more of the Notes section than necessary, so the function I'm writing will ultimately shift the line items up/down (i.e. if there is only one warranty package included, I want it to only show up on the last line). I haven't gotten that far yet as I keep getting a "field is null" error when I test this function.

It will work for one line and populate it with information, then error and stop. If I run it a second time, it populates the next line, then errors. I can keep running it and it will fill everything correctly, but I can't figure out where the error is coming from. Any help would be greatly appreciated!

function addWarranty() {

var WarrDesc1 = this.getField("WARR_DESC1").value;

var WarrDesc2 = this.getField("WARR_DESC2").value;

var WarrDesc3 = this.getField("WARR_DESC3").value;

var WarrDesc4 = this.getField("WARR_DESC4").value;

var WarrDescShift1 = this.getField("WARR_DESC_SHIFT1").value;

var WarrDescShift2 = this.getField("WARR_DESC_SHIFT2").value;

var WarrDescShift3 = this.getField("WARR_DESC_SHIFT3").value;

var WarrDescShift4 = this.getField("WARR_DESC_SHIFT4").value;

var WarrRetail1 = this.getField("WARR_RETAIL.0").value;

var WarrRetail2 = this.getField("WARR_RETAIL.1").value;

var WarrRetail3 = this.getField("WARR_RETAIL.2").value;

var WarrRetail4 = this.getField("WARR_RETAIL.3").value;

var WarrRetailShift1 = this.getField("WARR_RETAIL_SHIFT1").value;

var WarrRetailShift2 = this.getField("WARR_RETAIL_SHIFT2").value;

var WarrRetailShift3 = this.getField("WARR_RETAIL_SHIFT3").value;

var WarrRetailShift4 = this.getField("WARR_RETAIL_SHIFT4").value;

var arrWarrDesc = [WarrDesc1, WarrDesc2, WarrDesc3, WarrDesc4];

var arrWarrRetail = [WarrRetail1, WarrRetail2, WarrRetail3, WarrRetail4];

var arrWarrDescShift = [];

var arrWarrRetailShift = [];

for (i = 0; i < arrWarrDesc.length; i++) {

   if (arrWarrDesc !== "") {

    arrWarrDescShift.push(arrWarrDesc);

    arrWarrRetailShift.push(arrWarrRetail);

    console.println(arrWarrDescShift + ": " + arrWarrRetailShift);

    }

}

for (i = 0; i < arrWarrRetailShift.length; i++) {

    this.getField("WARR_DESC_SHIFT" + (i+1)).value = arrWarrDescShift;

    this.getField("WARR_RETAIL_SHIFT" + (i+1)).value = arrWarrRetailShift;

    }

}

TOPICS
Acrobat SDK and JavaScript

Views

1.2K

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
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 30, 2018 Sep 30, 2018

The "getField()" method does return an error condition by setting the return field object to "null", so it is possible to determine with JavaScript that a field does or does not exist.

Look at line 48 or earlier of you addWarranty function.  You are trying to compute 2 different field names starting at line 45 for the "WARR_DESC_SHIFT# and the WARR_RETAIL_SHIFT#. When the variable i has a value of 4 you are trying to access fields with a # value of 5. Do you have such a pair of fields in your for

...

Votes

Translate

Translate
LEGEND ,
Sep 30, 2018 Sep 30, 2018

Copy link to clipboard

Copied

I haven't looked at your code but I have a thought: if you use Page Open to populate data, consider what happens if the document is printed before or instead of going to that page.

Votes

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
community guidelines
Explorer ,
Sep 30, 2018 Sep 30, 2018

Copy link to clipboard

Copied

Thank you for the thought - the way the document is set up ensures that the page won't be able to be printed until it's looked at. However, it may make more sense to trigger the function with a page close action on page 1!

Votes

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
community guidelines
Community Expert ,
Sep 30, 2018 Sep 30, 2018

Copy link to clipboard

Copied

You say it errors. What is the exact text of the error message?

Votes

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
community guidelines
Explorer ,
Sep 30, 2018 Sep 30, 2018

Copy link to clipboard

Copied

Here is what the console reads...

MX-13 2 YEAR: 8000

CUMMINS 5 YR: 6250

TypeError: this.getField(...) is null

48:Document-Level:addWarranty

undefined

The first two lines are what values should be populating the fields marked with "_SHIFT" - printed to the console from the function

When the function runs, it only populates in "WARR_DESC_SHIFT1" with "MX-13 2 YEAR", then throws "TypeError: this.getField(...) is null". If I run it again, it populates the "8000" in "WARR_RETAIL_SHIFT1", then throws the error. And so on.

Votes

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
community guidelines
Community Expert ,
Sep 30, 2018 Sep 30, 2018

Copy link to clipboard

Copied

One of the field names you're trying to populate doesn't exist. Double-check you've spelled the field names correctly.

You can also add a console.println() command to your loop to print out the value of i in each iteration, so you can find out the culprit more easily.

Votes

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
community guidelines
LEGEND ,
Sep 30, 2018 Sep 30, 2018

Copy link to clipboard

Copied

The "getField()" method does return an error condition by setting the return field object to "null", so it is possible to determine with JavaScript that a field does or does not exist.

Look at line 48 or earlier of you addWarranty function.  You are trying to compute 2 different field names starting at line 45 for the "WARR_DESC_SHIFT# and the WARR_RETAIL_SHIFT#. When the variable i has a value of 4 you are trying to access fields with a # value of 5. Do you have such a pair of fields in your form?

If you are shifting the fields, what happens to the last element of the array arrWarrRetailShift?

Is it dropped or sent the the first element of the array?

Your code for this section could be revised to

var oWarr_Desc_Shift;

var oWarr_Retail_Shift;

for (i = 0; i < arrWarrRetailShift.length; i++) {

    console.println("i: " + i + " i + 1: " (i + 1));

    oWar_Desc_Shift = this.getField("WARR_DESC_SHIFT" + (i+1))

    if(oWar_Desc_Shift == null ) {

    app.alert("Error accessing " +"WARR_DESC_SHIFT" + (i+1), 1, 0)

    }

    oWar_Desc_Shift.value = arrWarrDescShift;

    oWarr_Retail_Shift = this.getField("WARR_RETAIL_SHIFT" + (i+1));

    if(oWarr_Retail_Shift = null) {

    app.alert("Error accessing " +"WARR_RETAIL_SHIFT" + (i+1), 1, 0)

    }

    oWarr_Retail_Shift.value = arrWarrRetailShift;

    }

I have written a function that performs the getField object and test for the error condition.

Votes

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
community guidelines
Community Expert ,
Sep 30, 2018 Sep 30, 2018

Copy link to clipboard

Copied

There's a mistake in this line:

if(oWarr_Retail_Shift = null) {

It should be:

if(oWarr_Retail_Shift == null) {

Votes

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
community guidelines
Explorer ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

Thanks so much for this - I think maybe there was one other small error on line 4 -

console.println("i: " + i + " i + 1: " (i + 1));

should be

console.println("i: " + i + " i + 1: " + (i + 1));

As to this point...

Look at line 48 or earlier of you addWarranty function.  You are trying to compute 2 different field names starting at line 45 for the "WARR_DESC_SHIFT# and the WARR_RETAIL_SHIFT#. When the variable i has a value of 4 you are trying to access fields with a # value of 5. Do you have such a pair of fields in your form?

I actually did think of that and had added a hidden pair of fields called WARR_DESC_SHIFT5 and WARR_RETAIL_SHIFT5... but was still getting the error.

Either way, I ran the function you wrote and it revealed that i is not being treated as a number, rather it's concatenating "i+1" and turning it into "01", thus looking for the field "WARR_DESC_SHIFT01" instead of "WARR_DESC_SHIFT1"

I will play with this a little bit and post an update shortly!

Thanks again for all of your help!

Votes

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
community guidelines
Explorer ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

gkaiseriltry67

I think I've gotten over this hump thanks to you guys.

I took gkaiseril​'s function and added "Number()" around all of the "(I + 1)"s.

var oWarr_Desc_Shift;

var oWarr_Retail_Shift;

for (i = 0; i < arrWarrRetailShift.length; i++) {

    console.println("i: " + i + " i + 1: " + (i + 1));

    oWarr_Desc_Shift = this.getField("WARR_DESC_SHIFT" + Number((i+1)));

    if(oWar_Desc_Shift == null ) {

    app.alert("Error accessing " +"WARR_DESC_SHIFT" + Number((i+1)), 1, 0);

    }

    oWarr_Desc_Shift.value = arrWarrDescShift;

    oWarr_Retail_Shift = this.getField("WARR_RETAIL_SHIFT" + Number((i+1)));

    if(oWarr_Retail_Shift == null) {

    app.alert("Error accessing " +"WARR_RETAIL_SHIFT" + Number((i+1)), 1, 0);

    }

    oWarr_Retail_Shift.value = arrWarrRetailShift;

    }

This seems to have done the trick! Thanks again!!

Votes

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
community guidelines
Community Expert ,
Oct 01, 2018 Oct 01, 2018

Copy link to clipboard

Copied

LATEST

That shouldn't be necessary, but if it works, that's great!

I would add the reserved word "var" before i=0 in the for-loop, to make sure there are no scope issues.

Votes

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
community guidelines