Fetch/Extract 'Template values' of a template and populate the values in catalog item fields when a template is selected from a reference field.

Akanksha Rawoo1
Tera Contributor

I have a field called 'Select Template'(template) on a catalog item which is reference field to sys_template table. When I select any template the template values like description, short description, category, subcategory should be auto populated in catalog item fields on catalog form.

I want to achieve this by Script Include and Catalog client script which will be written on variable set of the catalog item. I have also attached the XML format for Template Values to which i need to split and populate in catalog fields.

Since I am new to scripting can anyone please let me know how to write it.

Thanks in advance.

2 REPLIES 2

Sumeet1877
Mega Expert

Please refer this link for calling script include via glideajax 

https://community.servicenow.com/community?id=community_article&sys_id=25975379dbe0f8d0190dfb2439961937

I tried to populate short description of Template to the Short Desc field when any template is selected on the catalog field but I am getting value as undefined. Can you suggest here?

Script Include:

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

GetTemplateValue: function(){
gs.addInfoMessage('script include triggered');

var TemplateObj = {};
var gr = new GlideRecord('sys_template');
if (gr.get(this.getParameter('sysparm_TemplateSelect'))){
TemplateObj.Templatename = gr.getValue('short_description');

}
gs.addInfoMessage(TemplateObj+JSON.stringify(TemplateObj));

return JSON.stringify(TemplateObj);
},
type: 'FetchTemplateValues'
});

 

Catalog client script on change:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var TemplateSelected = g_form.getValue('Template');
alert ('TemplateSelected');
var TemplateValue = new GlideAjax('FetchTemplateValues');
TemplateValue.addParam('sysparm_name', 'GetTemplateValue');
TemplateValue.addParam('sysparm_TemplateSelect', 'TemplateSelected');
TemplateValue.getXMLAnswer(function(response){

var TemplateObj = JSON.parse(response);

alert ('TemplateObj' +TemplateObj.Templatename);
g_form.setValue('shrt_des',TemplateObj.Templatename);


});

}