How to get values dynamically using Get Method Rest API

Venkat141
Tera Contributor

On Catalog Item there is a variable called Requested For. whatever user is selected on Requested For Variable that value Department should need to get from other service-now Instance.

@Ankur Bawiskar 

@Peter Bodelier 

@AnveshKumar M 

@Vishal Birajdar 

@Sandeep Rajput

@Atulya - LNG 

@Saurav11

2 REPLIES 2

Jaspal Singh
Mega Patron
Mega Patron

Hi,

Is this more of ServiceNow to ServiceNow integration or some other system? Or is it that you want to pass RequestedFor.Department value to the other system.

What have you tried and where are you stuck?

Hi @Jaspal Singh,

@Ankur Bawiskar 

@Peter Bodelier 

@AnveshKumar M 

@Vishal Birajdar 

@Sandeep Rajput

@Atulya - LNG 

@Saurav11

Please find below code.

 

I wrote on Load client script and passing the Requested For user SysId to script include:

function onLoad() {
   //Type appropriate comment here, and begin script below
   var userID = g_form.getValue('requested_for');

   var ga = new GlideAjax('GetUserDetails');
   ga.addParam('sysparm_name', 'getUser');
   ga.addParam('sysparm_user_id', userID);
   ga.getXML(callback);

   function callback(response){

    var answer = response.responseXML.documentElement.getAttribute('answer');
    alert(answer);

   }
}
 
Here is the script include:
 
var GetUserDetails = Class.create();
GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    getUser: function() {

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', this.getParameter('sysparm_user_id'));
        gr.query();
        if (gr.next()) {

            var req = new sn_ws.RESTMessageV2('GetUserrecord', 'getDepartment'); // GetUserrecord is the Rest message name and GetDepartment is the get method name
            req.setStringParameterNoEscape('gr.sys_id');
            var response = req.execute();

            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();
            gs.log('responseBody = ', + responseBody);
            gs.log('statusCode = ' + httpStatus );
            var obj = JSON.parse(responseBody);
            var ans = obj.result.department;
            return ans;
        }

    },
    type: 'GetUserDetails'
});