Skip to main content
Alliosaaa
Inspiring
September 30, 2018
Answered

Help Setting Field Values from Array Values

  • September 30, 2018
  • 3 replies
  • 2161 views

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;

    }

}

This topic has been closed for replies.
Correct answer gkaiseril

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.

3 replies

Alliosaaa
AlliosaaaAuthor
Inspiring
October 1, 2018

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!!

try67
Community Expert
Community Expert
October 1, 2018

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.

try67
Community Expert
Community Expert
September 30, 2018

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

Alliosaaa
AlliosaaaAuthor
Inspiring
September 30, 2018

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.

try67
Community Expert
Community Expert
September 30, 2018

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.

Legend
September 30, 2018

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.

Alliosaaa
AlliosaaaAuthor
Inspiring
September 30, 2018

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!