Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Incorrect value passing form script include to client script

Rameshnathan
Tera Expert

Hi

i want to pass this tow value from script include to client script,

its displaying correct value in the script include but then i get this thought client script its displaying this error     ([object XMLHttpRequest])

Script Include   :-

if(grp.next()){

asgment.push(grp.getDisplayValue('assignment_group'));

priority.push(grp.getDisplayValue('priority'));

}

var result =[asgment,priority];

return result;

Client Script :-

function CallBack(response){

alert(response);

g_form.setValue('short_description', response);

}

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Ramesh,



When you return a payload from the server, it must be a string. I HIGHLY recommend using JSON to ensure arrays, or objects, are preserved properly and that commas or other special characters don't get in the way of a split command. Here's an example of how you can return the priority and assignment_group display value as a JSON object, stringify it on the server side and parse it on the client side, then create your short_description based on those two properties. Sorry for the crappy formatting in the community editor.



if(grp.next()){



var myAnswer = {


        "assignment_group" : grp.getDisplayValue('assignment_group'),


"priority" : grp.getDisplayValue('priority')


};



}




var result = JSON.stringify(myAnswer);



return result;




Client Script :-



function CallBack(response){


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


var myObj = JSON.parse(answer);


var myString = myObj.assignment_group + ' ' + myObj.priority;



g_form.setValue('short_description', myString);



}


View solution in original post

16 REPLIES 16

To do this from a business rule, I recommend building in another tier to your structure.Right   now you have,



Client script ---(calls)---> Client callable script include



Instead, change it to this:



Client script --(calls)---> Client callable script include --(calls)---> (Regular) script include w/logic to do your lookup and return



Then, the business rule can call that third "regular" script include.


Or can we can write Script Include in such a way that it accommodates the arguments if called on the server side...



var YourScriptInclude = Class.create();  


YourScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {  


yourFunction: function(parameter1,parameter2){  


var functionParameter1= this.getParameter('parameter1')||parameter1;


var functionParameter2 = this.getParameter('parameter2')||parameter2;


....




Client Side Script would be



var ajax = new GlideAjax('YourScriptInclude ');  


ajax.addParam('sysparm_name','yourFunction');  


ajax.addParam('parameter1', actualParameter1);  


ajax.addParam('parameter2', actualParameter2);  


ajax.getXML(doSomething);




Server Side Script to use the same SI would be:



var ajax =   new GlideAjax('YourScriptInclude ');  


ajax.yourfunction(paramter1,paramter2);




I think this should work for you.


Hi


I have tired this way b@@@ut i am not getting result, instead of getting field value im trying to getting Sys Id of the record from the sys id i will take the field value but its how failure please find the code beloe



======================================================================================



Script Include :-



var getGroupName = Class.create();


getGroupName.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getDetail: function(){



var caller = this.getParameter('sysparm_caller');


var comp =   this.getParameter('sysparm_company');


var sysid = [];


var grp = new GlideRecord('u_test_corp');


grp.addQuery('caller', caller);


grp.addQuery('company',comp);


grp.query();


if(grp.next()){


sysid.push(grp.getUniqueValue());


//var res = grp.getUniqueValue();


gs.log("Test Sys ID 11111" + sysid.toString());



return sysid.toString();


}


},


type: 'getGroupName'


});



The above Script include Rerun the particular record Sys id


Ex : 32fd6c81db010300a91fd001cf96194f



Now i want to get caller and company field value from Sys Id in   Business Rule and Client Script, please find the BR and Client Script with Error


======================================================================================


Client Script



Now im trying to get the caller and company field value from Sys Id,



Error : Its showing result as undefined



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


if (isLoading || newValue === '') {


return;


}


//Type appropriate comment here, and begin script below



var caller = g_form.getValue("caller");


var cmpny = g_form.getValue("company");



var gaa = new GlideAjax('getGroupName');


gaa.addParam('sysparm_name','getDetail');


gaa.addParam('sysparm_company', cmpny);


gaa.addParam('sysparm_caller', caller);


gaa.getXML(CallBack2);



function CallBack2(response)


{


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


var test = answer.toString();


g_form.setValue('short_description', test.u_city);


g_form.setValue('description', test.u_address);


}


}


Output form client script:


find_real_file.png


======================================================================================


Business Rule :-



In this BR im trying to get the caller and company field value from Sys Id,



(function executeRule(current, previous /*null when async*/) {



// Add your code here


var getGroup = new   getGroupName();


var result= getDetail.getGroup();



current.short_description = result.u_city;


current.description = result.u_address;


})(current, previous);




Error : its showing error in log


find_real_file.png


Phewww ... Please go through the AJAX documentation ...


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



your code must be something like this : (small tweaks will be needed for sure as I cannot test it on my instance )



//Script Include :-


var getGroupName = Class.create();


getGroupName.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getDetail: function(sysparm_caller,sysparm_company){


var caller = this.getParameter('sysparm_caller')||sysparm_caller;


var comp =   this.getParameter('sysparm_company')||sysparm_company;


var array = [];


var grp = new GlideRecord('u_test_corp');


grp.addQuery('caller', caller);


grp.addQuery('company',comp);


grp.query();


if(grp._next()) {


                      var object = {};


                      object.uniqueId = grp.getUniqueValue();


                      object.city = grp.getDisplayValue('u_city');


                      object.address = grp.getDisplayValue('u_address');


                      array.push(object);


              }


              var json = new JSON();


              var dataStr = JSON.stringify(array);


              return dataStr;


},


type: 'getGroupName'


});


//Client Script


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


if (isLoading || newValue === '') {


return;


}


//Type appropriate comment here, and begin script below


var caller = g_form.getValue("caller");


var cmpny = g_form.getValue("company");


var gaa = new GlideAjax('getGroupName');


gaa.addParam('sysparm_name','getDetail');


gaa.addParam('sysparm_company',cmpny);


gaa.addParam('sysparm_caller',caller);


gaa.getXML(CallBack2);


function CallBack2(response) {


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


      g_form.addInfoMessage(answer); //JSON String


      var json = new JSON;


      answer = json.parse(answer);


      g_form.addInfoMessage(answer);


      g_form.setValue('short_description', answer[0].city);


      g_form.setValue('description', answer[0].address);


      }


}


//Business Rule :-


(function executeRule(current, previous /*null when async*/) {


var getGroup = new   getGroupName();


var result = getGroup.getDetail(caller,cmpny);


var json = new JSON;


result = json.parse(result);


gs.setValue('short_description', result[0].city);


gs.setValue('description', result[0].address);


})(current, previous);



Hope this helps


I prefer to keep mine modular. Easier to build and unit test.