How to populate Mobile device details (IMEI & Serial Number) using a script include and catalog client script?

thiraj
Tera Contributor

Hi Team,

I have a reference variable (mobile_device) on the service portal and when a device is selected two fields appear (IMEI & Serial Number). It is hidden until mobile_device field is not empty. The mobile device details (u_imei and serial_number) are stored in the (u_cmdb_ci_mobile_device). I have written up a script include and called it from a catalog client script. It does not seem to work and i am not sure what the error is. I know it is something to do with the code but i am not too sure what is causing the issue. The screenshots are below.

find_real_file.png

find_real_file.png

All help is appreciated.

Thank you

1 ACCEPTED SOLUTION

camdesilva
ServiceNow Employee
ServiceNow Employee

Hi Thiraj,



Hope this helps:



Server Script:


var getDeviceCIDetails = Class.create();


getDeviceCIDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {


        getCIDevice: function() {


                  var obj = {};


                  var json = new JSON();


                  var mob = this.getParameter('sysparm_mob');


                  var mobDets = new GlideRecord('u_cmdb_ci_mobile_device');


                  mobDets.addQuery('sys_id', mob);


                  mobDets.query();


                  if(mobDets.next() && gs.getUser()) {


                              obj.imei = mobDets.u_imei + '';


                              obj.serial_number = mobDets.serial_number + '';


                              return json.encode(obj);


                  }        


        },


        type: 'getDeviceCIDetails'


}


);



Client Script:


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


        if(newValue == oldValue) {


                  return;


        }



        var gaj = new GlideAjax('getDeviceCIDetails');


        gaj.addParam('sysparm_name', 'getCIDevice');


        gaj.addParam('sysparm_mob', newValue);


        gaj.getXML(ajaxResponse);



        function ajaxResponse(response) {


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


                  //check that answer is not nil before continuing


                  if(answer) {


                            var answer_json = answer.evalJSON();


                            g_form.setValue('imei', answer_json.imei);


                            g_form.setValue('serial', answer_json.serial_number);


                  }


        }


}


View solution in original post

12 REPLIES 12

camdesilva
ServiceNow Employee
ServiceNow Employee

Hi Thiraj,



Line 18 to 24 of the "Populate Device Details" Catalog Client Script is not going to work because the "answer" attribute will only contain either "true" or "" as these are the only two possibilities that can be returned from the "getCIDevice" function in your Script Include.



The Script Include is not returning the 'u_cmdb_mobile_device' GlideRecord, it is only returning "true" or "", which means when you try to access the u_imei and serial_number attributes you won't be able to as these only exist on the "u_cmdb_mobile_device" GlideRecord object.



You will need to return these two attributes in your Script Include in order for them to be accessible via your Catalog Client Script.



These two pages explain how to return multiple attributes in a GlideAjax call. They may help you in achieving the outcome you're after.


http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0


https://fruitionpartners.eu/blog/2015/11/17/glideajax-return-multiple-values-using-json/


Hi Cam,



Thank you for the useful information. But i am not quiet clear with the syntax on how to return the two values (IMEI & Serial_number). Do you think you can help me out with the syntax.



Thank you


if(mobDets.next()){


var obj = {};


var json = new JSON();



obj.imei=mobDets.u_imei;


obj.serialNum=mobDets.serial_number;


var result = json.encode(obj);



return result;


}




in client script



function ajaxResponse(response)


{


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


mobValue= mobValue.evalJSON();


g_from.setValue('imei',mobValue.imei);


g_form.setValue('serial',mobValue.serialNum);


}


camdesilva
ServiceNow Employee
ServiceNow Employee

Hi Thiraj,



Hope this helps:



Server Script:


var getDeviceCIDetails = Class.create();


getDeviceCIDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {


        getCIDevice: function() {


                  var obj = {};


                  var json = new JSON();


                  var mob = this.getParameter('sysparm_mob');


                  var mobDets = new GlideRecord('u_cmdb_ci_mobile_device');


                  mobDets.addQuery('sys_id', mob);


                  mobDets.query();


                  if(mobDets.next() && gs.getUser()) {


                              obj.imei = mobDets.u_imei + '';


                              obj.serial_number = mobDets.serial_number + '';


                              return json.encode(obj);


                  }        


        },


        type: 'getDeviceCIDetails'


}


);



Client Script:


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


        if(newValue == oldValue) {


                  return;


        }



        var gaj = new GlideAjax('getDeviceCIDetails');


        gaj.addParam('sysparm_name', 'getCIDevice');


        gaj.addParam('sysparm_mob', newValue);


        gaj.getXML(ajaxResponse);



        function ajaxResponse(response) {


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


                  //check that answer is not nil before continuing


                  if(answer) {


                            var answer_json = answer.evalJSON();


                            g_form.setValue('imei', answer_json.imei);


                            g_form.setValue('serial', answer_json.serial_number);


                  }


        }


}