glide ajax

sandeepgujj
Tera Contributor
  1. How to send more than one variable from server side script to client side while using GlideAjax?

 

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @sandeepgujj 

 

You can refer https://youtu.be/NOPYxzICZBc?feature=shared

 

"If you found my answer helpful, please like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

4 REPLIES 4

Runjay Patel
Giga Sage

Hi @sandeepgujj ,

 

Use below code to return multiple value from script in clude.

 

Server Side code

var AjaxUtils = Class.create();
AjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    returnSimpleObject: function() {
        var obj = {};
        var sysId = this.getParameter('sysparm_sys_id');
        var almAssetGr = new GlideRecord("alm_asset");
        almAssetGr.addQuery('sys_id', sysId);
        almAssetGr.query();
        if (almAssetGr.next()) {
            obj.model = almAssetGr.getDisplayValue('model');
            obj.model_category = almAssetGr.getDisplayValue('model_category');
            
            var json = new JSON();
            var data = json.encode(obj); //JSON formatted string
        }
        return data;
    },
    type: 'AjaxUtils'
});

 

Client side code

function onLoad() {
    var ga = new GlideAjax('AjaxUtils');
    ga.addParam('sysparm_name', 'returnSimpleObject');
    ga.addParam('sysparm_sys_id', g_form.getValue('sys_id'));
    ga.getXML(showMessage);
}
function showMessage(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.addInfoMessage('JSON String: '+answer); //JSON String
    answer = answer.evalJSON(); //Transform the JSON string to an object
    g_form.addInfoMessage('Object: '+ answer);
    g_form.addInfoMessage('Asset Model: '+answer.model); //Display Asset Model
    g_form.addInfoMessage('Asset Category: '+answer.model_category); //Display Asset Category
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Tai Vu
Kilo Patron
Kilo Patron

Hi @sandeepgujj 

 

You can initialize an object with key-value pairs in the Script Include, then use JSON.stringify(jsonObject) to return it as a string. On the Client Script, you can use JSON.parse(response) to convert the string back into a JSON object and access its data.

Sample.

Client Callable Script Include

 

getJsonData: function() {  
    var data = { key1: "value1", key2: "value2" };  
    return JSON.stringify(data);  
}  

 

Client Script:

 

var ga = new GlideAjax('YourScriptIncludeName');  
ga.addParam('sysparm_name', 'getJsonData');  
ga.getXMLAnswer(function(response) {  
    var jsonData = JSON.parse(response);  
    console.log(jsonData.key1); // Output: value1  
    alert(jsonData.key1); // Output: value1  
});  

 

 

Cheers,

Tai Vu

it will work in the PDI also right

Juhi Poddar
Kilo Patron

Hello @sandeepgujj 

 

You can refer https://youtu.be/NOPYxzICZBc?feature=shared

 

"If you found my answer helpful, please like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar