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.

g_scratchpad not working for client script in Agent Workspace Incident creation form

Pratik13
Giga Contributor

Hi,

I am trying to populate sub category list in agent workspace incident creation.

find_real_file.png

On selecting the category the sub-category L1 list doesn't populate.

This is the client script:

 

function onLoad() {

//alert ("CS running now");

//Type appropriate comment here, and begin script below
var categoriesTree = g_scratchpad.atosChoices;
var renderFields = g_scratchpad.choicesFields;
var setFields = g_scratchpad.setFields;

for (var rF = 0; rF < renderFields.length; rF++) {
g_form.clearOptions(renderFields[rF]);
g_form.addOption(renderFields[rF], '', '-- None --', 0);
}

var _dependentValue = {}; // Stash for dependent values

// Process categories tree
for (var cat = 0; cat < categoriesTree.length; cat++) {
// At first add categories which don't depend on any other values
if (categoriesTree[cat].dependent == "") {
g_form.addOption(categoriesTree[cat].name, categoriesTree[cat].value, categoriesTree[cat].label);
}

// If there is already value set for field being processed then save it in the stash..
if (setFields[categoriesTree[cat].name] == categoriesTree[cat].value) {
_dependentValue[categoriesTree[cat].value] = categoriesTree[cat].value;
g_form.setValue(categoriesTree[cat].name, categoriesTree[cat].value); //..and set correct value on the form

//jslog("vals: " + categoriesTree[cat].name + " - " + categoriesTree[cat].value + " setFields: " + setFields[categoriesTree[cat].name]);
}

// If currently processed category is already set (and exists in stash) add
// options to slave list/category
if (_dependentValue[categoriesTree[cat].dependent]) {
if (categoriesTree[cat].value != setFields[categoriesTree[cat].name]) {
g_form.addOption(categoriesTree[cat].name, categoriesTree[cat].value, categoriesTree[cat].label);

// console.log("vals2: " + categoriesTree[cat].name + " - " + categoriesTree[cat].value );
}
}
}
}

 

Any help is appreciated.

10 REPLIES 10

Pratik13
Giga Contributor

Yes, renderField value is showing undefined in agent workspace but working properly elsewhere.

I guess g_scratchpad is not able to get values.

Any alternative for this?

Any help appreciated.

Hi Pratik,

Use onLoad client script and then use GlideAjax and Script Include

Regards
Ankur

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

Hi, As suggested I wrote the client Script and Script Include:

Client Script :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga=new GlideAjax('ScriptIncludeGlideAjax');
ga.addParam('sysparm_name','getCategoryName');
ga.addParam('sysparm_categoryName',g_form.getValue('category'));
ga.getXML(callBackFunction);

function callBackFunction(response)
{
var answer=response.responseXML.documentElement.getAttribute("answer");
var userSysIds = JSON.parse(answer);

for (var i=0; i< userSysIds.length; i++)
{
alert(userSysIds[i]);
//g_form.setValue('subcategory','answer');
}
//Type appropriate comment here, and begin script below

}

 

Script Include:

 

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

getCategoryName : function()
{
var res = [];
var gr= new GlideRecord('u_ssp_categories_map');
gr.addQuery('u_incident_category',this.getParameter('sysparm_categoryName'));// tools=sysid
gr.query();
while(gr.next()){
res.push(gr.getDisplayValue('u_incident_subcategory_1'));
gs.addInfoMessage(res);
return JSON.stringify(res);
}
},

type: 'GlideAjaxScriptInclude'
});

 

But still the subcategory lists is not popping or even the alert box highlighted above is not working. 

Any help appreciated.

can you put log in your display business rule ? while executing it from Agent workspace are you able to get correct result in display business rule ?

Jim Coyne
Kilo Patron

Maybe I'm missing something here, but why not use dependent fields?

Make a field dependent