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

Hi Cam,



I got it to work. The code was perfect. The variable set was the issue. I was testing on a catalog item that was not using the same variable set.


Sorry and thank you for your time.



Thank you


camdesilva
ServiceNow Employee
ServiceNow Employee

That's great!



You're very welcome


Vamsi Saladi
Tera Contributor

Create a Json variable in script include.
without that the Json parsing you are doing in client script wont work.


And


the condition If(gs.User()) this will always return true and so controller will enter inside if condition. So this is not achieving anything in specific.



After that change return true to return the json object you have created.