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.

GlideAjax not working in service catalog

Nosazena
Kilo Expert

Hi guys,

I'm a bit of a newbie with servicenow. I'm trying to get fields from the catalog item record automatically inserted in the service portal when the request is loaded.

each catalog item record has a field called oladuration with values ranging from 1hr, 4hrs,1day, 2days,1week

I did some research and it seems i need to use GlideAjax to accomplish this task. However, I'm not a developer so i'd appreciate any help with the "script include" code and the "catalog client script" code

This is what I was able to conjure up. It doesn't give any errors but the ola doesn't get populated correctly in the service portal.

Script Include

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

getOLA: function(){

var item = new GlideRecord("sc_cat_item");
item.get(this.getParameter('sysparm_oladuration'));
return sc_cat_item.u_oladuration;
},

type: 'GetOladuration'
});

Catalog Client script

function onLoad() {

var getOlaDuration = new GlideAjax('GetOladuration');

getOlaDuration.addParam('sysparm_name', 'getOLA');
getOlaDuration.addParam('sysparm_oladuration',g_form.getValue('sc_cat_item.u_oladuration'));

getOlaDuration.getXML(populateOLAfield);

function populateOLAfield(response){
OLAfield = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_oladuration', OLAfield);
}
}

 

 

1 ACCEPTED SOLUTION

Nosazena
Kilo Expert

Hi guys,

Thanks for all your help. I was able to resolve the issue. The script below worked for me. 

SCRIPT INCLUDE

var getOlaDetail = Class.create();

getOlaDetail.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getOLA: function() {

//Get the sys_ID for the call

var callSysID = this.getParameter('sysparm_callSysID');

var gr = new GlideRecord('sc_cat_item');
gr.get(callSysID);
var olaDetail = {};
olaDetail.olaDur = gr.getDisplayValue('u_oladuration');
var json = new JSON();
var data = json.encode(olaDetail);
return data;
},

type: 'getOlaDetail'

});

 

CLIENT SCRIPT

function onLoad() {

var callSysID = g_form.getUniqueValue();
var ga = new GlideAjax('getOlaDetail');

ga.addParam('sysparm_name','getOLA');
ga.addParam('sysparm_callSysID', callSysID);
ga.getXML(handleResponse);

function handleResponse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);
g_form.setValue("itd_duration",answer.olaDur);
}}}

View solution in original post

8 REPLIES 8

Hi Willem,

Thanks for your feedback

See screenshot below

find_real_file.png

Ok, looks fine. Try this:

Script include:

(I have changed the return to the u_oladuration field on the sc_cat_item table. Make sure that field exists in that table and has valid value)

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

    getOLA: function () {
        
        var item = new GlideRecord("sc_cat_item");
        item.get(this.getParameter('sysparm_oladuration'));
        gs.info('item=' + item.u_oladuration.toString())
        return item.u_oladuration.toString();
    },

    type: 'GetOladuration'
});

 

Catalog client script:

function onLoad() {

    var getOlaDuration = new GlideAjax('GetOladuration');

    getOlaDuration.addParam('sysparm_name', 'getOLA');
    getOlaDuration.addParam('sysparm_oladuration', g_form.getValue('u_oladuration'));
    alert("oladuration" + g_form.getValue('u_oladuration'));

    getOlaDuration.getXML(populateOLAfield);

    function populateOLAfield(response) {
        OLAfield = response.responseXML.documentElement.getAttribute("answer");
        alert("olafield=" + OLAfield);
        g_form.setValue('u_oladuration', OLAfield);
    }
}

 

It will alert in the browser and write to the ServiceNow System log. Can you check both and share results?

see logs below

find_real_file.png

see browser alert below

find_real_file.png

 

find_real_file.png

Nosazena
Kilo Expert

Hi guys,

Thanks for all your help. I was able to resolve the issue. The script below worked for me. 

SCRIPT INCLUDE

var getOlaDetail = Class.create();

getOlaDetail.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getOLA: function() {

//Get the sys_ID for the call

var callSysID = this.getParameter('sysparm_callSysID');

var gr = new GlideRecord('sc_cat_item');
gr.get(callSysID);
var olaDetail = {};
olaDetail.olaDur = gr.getDisplayValue('u_oladuration');
var json = new JSON();
var data = json.encode(olaDetail);
return data;
},

type: 'getOlaDetail'

});

 

CLIENT SCRIPT

function onLoad() {

var callSysID = g_form.getUniqueValue();
var ga = new GlideAjax('getOlaDetail');

ga.addParam('sysparm_name','getOLA');
ga.addParam('sysparm_callSysID', callSysID);
ga.getXML(handleResponse);

function handleResponse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);
g_form.setValue("itd_duration",answer.olaDur);
}}}