How to update catalog item fields via client script using getReference

Michael M1
Giga Expert

I have a field on the catalog item called requested_for which is a sys_user reference field.

I want to update the location field on the catalog item to the location of the requested for user - which is not necessarily the current logged in user. I have a catalog client script like below but the req4 is 'undefined' when I print to screen.

How can this be accomplished?

 

function onChange(control, oldValue, newValue, isLoading) {
if (newValue) {
 var req4 = g_form.getReference('requested_for',mycallback);
}
 return;
}

function mycallback(req4){
 if(!isLoading && newValue != oldValue){
 g_form.setValue('requested_for_location', req4.location);
 }


}

15 REPLIES 15

quick question "requested_for_location" is reference field? if not make it either reference

or you can use glide ajax to solve this requirement because requested for location is reference field and it will give you the sys id of location.

 

When I try the DisplayBox version you have provided I get:

onChange script error: TypeError: Cannot read property 'value' of null function () { [native code] }

Client script :

 

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

if (isLoading || newValue === '') {

return;

}

var reqFor = g_form.getValue('requested_for');

var graj=new GlideAjax('test');

graj.addParam('sysparm_name', 'getDept');

graj.addParam('sysparm_id', reqFor);

graj.getXML(HelloWorldParse);


function HelloWorldParse(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

g_form.setValue('requested_for_location',answer);

}

}

 

Script Include: Make sure you checked client callable check box.


var test = Class.create();

test.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getDept :function()



   {

   var name='';

   var id = this.getParameter('sysparm_id');

   var dep=new GlideRecord('sys_user');

   dep.addQuery('sys_id', id);

   dep.query();

   if(dep.next())

   {

   name=dep.location.getDisplayValue();

   }

   gs.log('here is:'+name);

   return name;

   },



       type: 'test'

});

Thanks I will try this.

FYI: All fields are reference fields (requested_for_manager, requested_for_location, requested_for).

 

In the first line of your above script:

var reqFor = g_form.getValue('requested_for');

I add this immediately after:
g_form.addInfoMessage("value = "+reqFor);

 

Value is null. It is not capturing the user in requested_for. 

Note: addInfoMessage works on server side and you are using client script so that will not work.

 

refer the doc link below.

https://docs.servicenow.com/bundle/london-application-development/page/script/general-scripting/refe...