GlideAjax not working in service catalog

Nosazena
Giga Contributor

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
Giga Contributor

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

Yash Agrawal1
Tera Guru

Hello @nosazena,

Here in line 

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

 

you cannot use "sc_cat_item.u_oladuration",as dot walking is not a best praticse in servicenow client script.

you can pass the field value directly to scrilt include.and apply the filter and script there in script include.

From client script please pass the value entered in field directly.

Please keep posted for more help

Regards

Yash Agrawal

Willem
Giga Sage
Giga Sage

Make sure your catalog client script is set as UI Type All or Service portal/mobile.

 

Try change the Catalog client script to:

function onLoad() {

    var getOlaDuration = new GlideAjax('GetOladuration');

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

    getOlaDuration.getXML(populateOLAfield);

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

Nosazena
Giga Contributor

I tried this... It still the same result

Below is the catalog item and the oladuration

find_real_file.png

 

I expected the oladuration field in the service portal to be automatically set to 3days as in the record but it shows 1day

 

find_real_file.png

Willem
Giga Sage
Giga Sage

Can you share screenshot of your Catalog client script?

Few things to check:

  • Is it attached to the Catalog? If not attach it, or attach it to a Variable Set and attach that variable set to the Catalog items you want.
  • UI Type set to All or Service portal/mobile.
  • You can include logs and alerts to see if any value is retrieved.