g_form.getReference not working for list reference field when dot walked

Imran1
Giga Guru

Hi All,

I have a list reference field (referencing to contract table) and I need to set values in another field called approvers basis on contract that someone selects; for example if a person selects contract a, contract b. I need to set the approver as x & y and if a person selects contract d, contract e. I need to set the approvers as A&B etc.

For doing this I am using an on-change client script and calling the getReference() method

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

var suppliers  = g_form.getReference('contract', doAlert);
}

function doAlert(caller) { // reference is passed into callback as first arguments
if (caller.supplier) // here I am dot walking to the supplier field within the contract table
g_form.setValue('internal_use', caller.supplier);
var rel = g_form.getValue('internal_use');
g_form.addErrorMessage(rel);
}

if (suppliers.indexOf('HAA') > -1) {
g_form.setValue('approvers', '10a694bfdbd85410d3edf5f51d9619ac,6043e934db429810aec35665dc96193f');
}
if (suppliers.indexOf('ABC') > -1 || suppliers.indexOf('Enter') > -1) {
g_form.setValue('approvers', '8126d0bfdbd85410d3edf5f51d96197b,494e240ddb0590101fedff461d961982');
}

This script doesn't seem to work even the addError message doesn't through any error

Not sure if I am doing rightly, basically I need to get the value of another field from the reference field table and basis on that I need to set the approvers

Any help is highly appreciated. Could you please try it in your instance within client script 

Regards,
Imran

11 REPLIES 11

Willem
Giga Sage
Giga Sage

Hi Imran,

 

getReference is not recommended and will not work in this way. Try replace is with a Glide Ajax call that calls a script include.

Sourav16
Kilo Guru

Hi Imran why don't you try GlideAjax instead of getReference.

Screenshot of the form would be helpful to better understand the scenario. 

asifnoor
Kilo Patron

Hi,

FEw things.

1. Firstly best way is to go with GlideAjax.

 

2. This is wrong. The suppliers is not declared where you are using it. get the suppliers first and then check it.

if (suppliers.indexOf('HAA') > -1) {

3. Not sure hwat you are doing here. you are getting the caller.supplier.. storing in internal_use and then again fetching value of internal_use and putthing that as error.

if (caller.supplier) // here I am dot walking to the supplier field within the contract table
g_form.setValue('internal_use', caller.supplier);
var rel = g_form.getValue('internal_use');
g_form.addErrorMessage(rel);

Change this to

if (caller.supplier!= '') {
g_form.setValue('internal_use', caller.supplier);
g_form.addErrorMessage(caller.supplier);

}

Hi Asif, 

I had the 'suppliers' variable declared on the top, basically I am trying to get the string value from the reference table. 

example the reference value I will get the sys id and if I dot walk into the reference table I have field called supplier which has string values

If the suppliers is any of the once that is starting with ABC, then I will set the approvers using the indexOf method

at present I made it work using the hard coded sys ids (below is my script) but would prefer to use the g_form.getReference() method which is part of service-now documentation

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var sup = g_form.getValue('contract_suppliers');

if(sup.indexOf('8336ea93db101410d3edf5f51d961987')>-1){// Enterprise
g_form.clearValue('approvers');
g_form.setValue('approvers', '8126d0bfdbd85410d3edf5f51d96197b,494e240ddb0590101fedff461d961982');
g_form.setDisplay("secondary_role",true);
g_form.setDisplay("library",false);
g_form.setDisplay("gl_contracts",false);
}

else if(sup.indexOf('ce15e10fdbf70010d3edf5f51d96196d')>-1){ //ABC
g_form.clearValue('approvers');
g_form.setValue('approvers', '8126d0bfdbd85410d3edf5f51d96197b,494e240ddb0590101fedff461d961982');
g_form.setDisplay("secondary_role",true);
g_form.setDisplay("library",false);
g_form.setDisplay("gl_contracts",false);

}
else{
g_form.clearValue('approvers');
g_form.setValue('approvers', '10a694bfdbd85410d3edf5f51d9619ac,6043e934db429810aec35665dc96193f');
g_form.setDisplay("secondary_role",false);
g_form.setDisplay("library",true);
g_form.setDisplay("gl_contracts",true);
}

}

 

Regards,
Imran