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 gave it a try but does not seem to work. The two fields (IMEI & Serial number) does not get populated. I rechecked if this particular device has a IMEI and Serial Number in the back end and it does. The table referenced is also correct. I just cannot seem to find what is causing this error. The code looks correct as well.




find_real_file.png



Thank you


camdesilva
ServiceNow Employee
ServiceNow Employee

Hi Thiraj,



Can you confirm that the variables in the Catalog Item are named "imei" and "serial"?



I think the next step would be to do some logging, place some gs.info() statements in your script include and check the logs in ServiceNow to see that it's running as expected and it's setting the values as expected.



Place some alert() statements in your client script and see where it is failing. E.g what does the answer variable look like after it has been set. What does it look like after it has been evaluated as a JSON object?



alert(answer.toString());


alert(answer_json.imei);


alert(answer_json.serial_number);


Hi Cam,



I tried using logs on the server side script but nothing gets logged. I am not sure what is causing this problem.


find_real_file.png


Thank you


camdesilva
ServiceNow Employee
ServiceNow Employee

Place an alert(newValue); on the client script before the GlideAjax initialisation.



Is this firing? Are you seeing a value resembling a sys_id?


Hi Cam,



It is not firing!



Thank you