Unable to pass Json values

abjaffrey
Giga Guru

Hi Community

 

Coul you please help me with the below what went wrong.

The expected result is when an incident is loaded it should display all the incidents assigned to the caller.

However im getting only the first info msg and not the incident numbers.

Appreciate your help.

 

Script Include:

 

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

    getCallersIncs: function() {
        var incArray = [];
        var callersRecords = new GlideRecord('incident');
        callersRecord.addQuery('caller_id', this.getParameter('sysparm_callerID'));
                callersRecords.query();

            while(callersRecords.next()) {
                // var incDetails = {};
                // incDetails.number = callersRecords.number.toString();
                // incDetails.priority = callersRecords.priority.getDisplayValue();
                // incDetails.shortDesc = callersRecords.short_description.toString();
                incArray.push(callersRecords.number);

            }

                return JSON.stringify(incArray);
    },

    type: 'CallerIncidents'
});
 
Client Script:
 
function onLoad() {
   //Type appropriate comment here, and begin script below

var incGA = new GlideAjax('CallerIncidents');
incGA.addParam('sysparm_name','getCallersIncs');
incGA.addParam('sysparm_callerID',g_form.getValue('caller_id'));
incGA.getXMLAnswer(CallerIncidentsParse);

function CallerIncidentsParse(response) {
    var myObj = JSON.parse(response);
    g_form.addInfoMessage('User has raised the following incidents:');

    for (var i=0; i< myObj.length; i++) {
        g_form.addInfoMessage(myObj[i].number);
    }
}
   
}

 

3 ACCEPTED SOLUTIONS

guru4
Kilo Guru

Hi @abjaffrey 

In script include line number 7 you mistakenly used 

"callersRecord", it should be "callersRecords" you have missed the 's' which you have declared as variable :var callersRecords = new GlideRecord('incident'); Please make the changes it should work fine.
Mark my answer as correct and helpful if it solves your query

View solution in original post

Hey @guru4 

 

Thanks for the response, it worked.

 

Can you help why when i passed as JSON initially (if you see i initially wanted multiple return values as key value pairs in an array) i was unable to find the desired result.

View solution in original post

function onLoad() {
//Type appropriate comment here, and begin script below

 

//g_form.addInfoMessage('hello');
var incGA = new GlideAjax('CallerIncidents');
incGA.addParam('sysparm_name', 'getCallersIncs');
incGA.addParam('sysparm_callerID', g_form.getValue('caller_id'));
incGA.getXMLAnswer(CallerIncidentsParse);

 

function CallerIncidentsParse(response) {
var myObj = JSON.parse(response);
g_form.addInfoMessage('User has raised the following incidents:');

 

for (var i=0; i< myObj.length; i++) {
g_form.addInfoMessage(myObj[i]);
}
}
}
Just use g_form.addInfoMessage(myObj[i]); instead of g_form.addInfoMessage(myObj[i].number); Since that object only contains number inside it no need to mention it separately.
Mark my answer as correct and helpful if it solves your query
Regards,
Guru

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

@abjaffrey 

try this

don't use JSON as you just want numbers

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

    getCallersIncs: function() {
        var incArray = [];
        var callersRecords = new GlideRecord('incident');
        callersRecord.addQuery('caller_id', this.getParameter('sysparm_callerID'));
        callersRecords.query();
        while (callersRecords.next()) {
            incArray.push(callersRecords.number.toString());
        }
        return incArray.toString();
    },

    type: 'CallerIncidents'
});
function onLoad() {
    //Type appropriate comment here, and begin script below

    var incGA = new GlideAjax('CallerIncidents');
    incGA.addParam('sysparm_name', 'getCallersIncs');
    incGA.addParam('sysparm_callerID', g_form.getValue('caller_id'));
    incGA.getXMLAnswer(CallerIncidentsParse);

    function CallerIncidentsParse(response) {
        g_form.addInfoMessage(response.toString());
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

 

thanks for the response, but still its not working.

 

SI:

 

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

    getCallersIncs: function() {
        var incArray = [];
        var callersRecords = new GlideRecord('incident');
        callersRecord.addQuery('caller_id', this.getParameter('sysparm_callerID'));
                callersRecords.query();

            while(callersRecords.next()) {
                // var incDetails = {};
                // incDetails.number = callersRecords.number.toString();
                // incDetails.priority = callersRecords.priority.getDisplayValue();
                // incDetails.shortDesc = callersRecords.short_description.toString();
                incArray.push(callersRecords.number);

            }

                return incArray;
    },

    type: 'CallerIncidents'
});
 
CS:
 
function onLoad() {
   //Type appropriate comment here, and begin script below

var incGA = new GlideAjax('CallerIncidents');
incGA.addParam('sysparm_name','getCallersIncs');
incGA.addParam('sysparm_callerID',g_form.getValue('caller_id'));
incGA.getXMLAnswer(CallerIncidentsParse);

function CallerIncidentsParse(response) {
    var myObj = response;
    g_form.addInfoMessage('User has raised the following incidents:');

    for (var i=0; i< myObj.length; i++) {
        g_form.addInfoMessage(myObj[i].number);
    }
}
   
}

@abjaffrey 

what's not working?

Is script include client callable?

Did you add logs and see if the script include is getting called?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

hi 

 

yes it is client callable,

i don't know how to add logs and where to view them,

im in a learning phase