The CreatorCon Call for Content is officially open! Get started here.

Need help in JSON parsing

abjaffrey
Giga Guru

Hey Folks,

 

I'm just learning and practising development, I'm unable to complete the Script include & client script, need your help.

Can someone help with what went wrong

 

Script Include : 

 

var getIncDetails = Class.create();
getIncDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getINC:function(){
        var incArray = [];
        var gr = new GlideRecord('incident');
        gr.addQuery('caller_id',this.getParameter(sysparm_user_id));
        gr.query();

        while(gr.next()){
            var incDetails = {};
            incDetails.number=gr.number.toString();
            incDetails.priority=gr.priority.getDisplayValue();
            incDetails.short_desc=gr.short_description.toString();
            incArray.push(incDetails);
        }
    return JSON.stringify(incArray);
    },

    type: 'getIncDetails'
});
 
Client Script: 
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var finalArray = [];
   var ga = new GlideAjax('getIncDetails');
   ga.addParam = ('sysparm_name','getINC');
   ga.addParam = ('sysparm_user_id',newValue);
   ga.getXMLAnswer(IncDetail);

   function IncDetail(response){
    var obj = JSON.parse(response);

    for(var i =0;i<obj.length;i++){
        finalArray.push('Incident Number : '+ obj[i].number +'\n'+'Priority : ' + obj[i].priority +'\n'+ 'Short Description : ' + obj[i].short_desc +'\n\n');
    }
    // g_form.setValue('description',finalArray);
   g_form.setValue('description',finalArray);
   }

   //Type appropriate comment here, and begin script below
   
}
1 ACCEPTED SOLUTION

ga.addParam = ('sysparm_name','getINC');
ga.addParam = ('sysparm_user_id',g_form.getValue('caller_id'));
Change these two lines as below in client script
ga.addParam('sysparm_name','getINC');
ga.addParam('sysparm_user_id',g_form.getValue('caller_id'));

View solution in original post

12 REPLIES 12

Gustav Aldenbra
Kilo Sage

Hi @abjaffrey 

In your script include it doesn't look like you have sysparm_user_id in quotes in your GlideRecord query. can you change that and see if it works? I would put the user_id that you pass in to the script in variable like this: var userId = this.getParameter('sysparm_user_id'); then add the userId variable in the query instead, it makes the script a bit easier to read.

It is also a good idea to log out data both in script include and client script when it does not work as intended.

Hi @Gustav Aldenbra 

 

Thanks for the response, i've tried changing the code as you suggested. But still it doesn't work.

 

Script Include:

 

var getIncDetails = Class.create();
getIncDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getINC:function(){
        var incArray = [];
        var gr = new GlideRecord('incident');
        var userID = this.getParameter('sysparm_user_id');
        gr.addQuery('caller_id',userID);
        gr.query();

        while(gr.next()){
            var incDetails = {};
            incDetails.number=gr.number.toString();
            incDetails.priority=gr.priority.getDisplayValue();
            incDetails.short_desc=gr.short_description.toString();
            incArray.push(incDetails);
        }
    return JSON.stringify(incArray);
    },

    type: 'getIncDetails'
});
 
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var finalArray = [];
   var ga = new GlideAjax('getIncDetails');
   ga.addParam = ('sysparm_name','getINC');
   ga.addParam = ('sysparm_user_id',g_form.getValue('caller_id'));
   ga.getXMLAnswer(IncDetail);

   function IncDetail(response){
    var obj = JSON.parse(response);

    for(var i =0;i<obj.length;i++){
        finalArray.push('Incident Number : '+ obj[i].number +'\n'+'Priority : ' + obj[i].priority +'\n'+ 'Short Description : ' + obj[i].short_desc +'\n\n');
    }
    // g_form.setValue('description',finalArray);
   g_form.setValue('description',finalArray);
   }

   //Type appropriate comment here, and begin script below
   
}
 

 

thomaskennedy
Tera Guru

You didn't say what happens when you run this, and how that is different from the result you expected.

the expected result is that when i change the caller field in incident form the description should change as per the script where we are returning an array

 

but im getting no change in description