Client Script - setValue not working as expected

Sue Frost
Giga Guru

I have an OnLoad client script to set a choice list field and then set the actual value of the field.

The choices are dynamic and are first looked up (when another reference field is entered) and then stored in a field to feed the choice list.

The choice values are being correctly returned and the options in the choice list are being set. However, if a value in the field exists, when reloading the form, only the value is shown, not the display value:

9-7-2017 1-17-44 PM.png

Here's my client script:

function onLoad() {

//Type appropriate comment here, and begin script below

//Build Coverage Item Description Code choice list

var covCodes = g_form.getValue('coverage_code_options');

var covNum;

var currValue = g_form.getValue('coverage_item_description_code');

g_form.clearValue('coverage_item_description_code');

alert('currValue - ' + currValue);

if(covCodes == ''){

return;

}

var codeChoices = covCodes.split(',');

var i = 0;

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

covNum = codeChoices[i].split(' - ');

g_form.addOption('coverage_item_description_code', covNum[0], codeChoices[i].toString(), covNum[0]);

if(currValue == covNum[0]){

alert("into IF - " + covNum[0] + " - " + codeChoices[i].toString());

g_form.setValue('coverage_item_description_code', covNum[0], codeChoices[i].toString());

}

}

}

The alerts show me that I AM getting the correct values (5 and 5 - TOOLS in my example) but I can't get the field to show "5 - TOOLS" That should, as far as I can tell, be what line 27 does. What am I doing wrong?

TIA!

1 ACCEPTED SOLUTION

saprem_d
Giga Guru

Hi,



Can you try updating line   29 as below



g_form.setValue('coverage_item_description_code', codeChoices[i].toString());


View solution in original post

6 REPLIES 6

nishailame
ServiceNow Employee
ServiceNow Employee

covNum[0] - should be the value and codeChoices[i].toString() should be the display value.


Open the choice list of coverage item description code and check the value of "5- tools" ( that should go in second place).



Thanks.


PS: Hit like, Helpful, Correct, if it answers your question.


saprem_d
Giga Guru

Hi,



Can you try updating line   29 as below



g_form.setValue('coverage_item_description_code', codeChoices[i].toString());


That worked!


I don't understand exactly why but I'm glad it did.



Now the only thing I'm seeing is that the 'old' value is still showing in spite of the line to remove all existing values (line 09).


9-7-2017 2-03-05 PM.png


Hi,



Thats good news. Can you please mark the answer as correct.