- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 02:35 PM
I have a requirement where I need to populate two fields on a catalog item when a "Template" (reference variable to the sys_template table) is selected: Short Description and Description. This will be done using a Catalog Client Script GlideAjax in conjunction with a Script Include.
My issue is that the <template> in a sys_template's XML has "short_description" and "description" values. So when I attempt to do an "indexOf()" it is pulling from the first field with "description" in the XML and populating two different fields (on the catalog item) with the same data.
var obj = {};
var template = this.getParameter('sysparm_template');
var findTemplate = new GlideRecord('sys_template');
findTemplate.addQuery('sys_id', template);
findTemplate.query();
if (findTemplate.next()) {
var tempDetails = findTemplate.getValue('template');
var ag = '';
var at = '';
var sd = '';
var desc = '';
/*_________________________________________________________________
* Description: retrieve assignment group from template details
* Parameters: template values from GlideRecord result
* Returns: assignment group sys_id
________________________________________________________________*/
if (tempDetails.indexOf('assignment_group') < 0) {
ag = 'This template does not have an assignment group provided.';
}
if (tempDetails.indexOf('assignment_group') >= 0) {
var grpStart = tempDetails.indexOf('assignment_group');
var grpIDStart = grpStart + 17;
var grpIDEnd = grpIDStart + 32;
ag = tempDetails.slice(grpIDStart, grpIDEnd);
} else {
ag = 'Template does not set an Assignment group value';
}
}
/*_________________________________________________________________
* Description: retrieve assigned to from template details
* Parameters: template values from GlideRecord result
* Returns: assigned_to sys_id
________________________________________________________________*/
if (tempDetails.indexOf('assigned_to') < 0) {
at = 'This template does not have an assigned individual provided.';
}
if (tempDetails.indexOf('assigned_to') >= 0) {
var atStart = tempDetails.indexOf('assigned_to');
var atValueStart = atStart + 12;
var atTemplateSnippet = tempDetails.slice(atValueStart);
var atValueEnd = atTemplateSnippet.indexOf('^');
if (atValueEnd != -1) {
at = atTemplateSnippet.slice(0, atValueEnd);
}
}
/*_________________________________________________________________
* Description: retrieve short description from template details
* Parameters: template values from GlideRecord result
* Returns: short description value
________________________________________________________________*/
if (tempDetails.indexOf('short_description') < 0) {
sd = 'This template does not have a short description provided.';
}
if (tempDetails.indexOf('short_description') >= 0) {
var sdStart = tempDetails.indexOf('short_description');
var sdValueStart = sdStart + 18;
var sdTemplateSnippet = tempDetails.slice(sdValueStart);
var sdValueEnd = sdTemplateSnippet.indexOf('^');
if (sdValueEnd != -1) {
sd = sdTemplateSnippet.slice(0, sdValueEnd);
}
}
/*_________________________________________________________________
* Description: retrieve detailed description from template details
* Parameters: template values from GlideRecord result
* Returns: description value
________________________________________________________________*/
if (tempDetails.indexOf('description' < 0)) {
desc = 'This template does not have a detailed description provided.';
}
if (tempDetails.indexOf('description') >= 0) {
var descStart = tempDetails.indexOf('description');
var descValueStart = descStart + 12;
var descTemplateSnippet = tempDetails.slice(descValueStart);
var descValueEnd = descTemplateSnippet.indexOf('^');
if (descValueEnd != -1) {
desc = descTemplateSnippet.slice(0, descValueEnd);
}
}
obj = {
assignmentGroup: ag,
assignedTo: at,
shortDescription: sd,
description: desc
};
var myObject = JSON.stringify(obj);
return myObject;
}
I have tried using regular expressions (regex) with exact word, negative lookbehind (which ServiceNow scripting did not allow /(?<!_)description/), shortening the index to find "_description" to just capture the "short_description" value, and so on. I have scoured through many online resources (including this community) and not having any luck.
Would someone assist in solving for this disjoint in string methods so I populate the fields on the form with the accurate information from the template's <template> XML values?
Thank you.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 02:50 PM
You can index on ^description instead of description and try.
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2023 02:50 PM
You can index on ^description instead of description and try.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2023 06:24 AM
Hi Sanjiv:
Thank you for responding so quick. 😎
I just tried that and it works when "description" is not the first value in the XML's <template> field.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2023 08:40 AM
Why dont you just apply the template using a onLoad client script
applyTemplate('<sysid of the template>');
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2023 04:56 AM
I ended up going about a different solution using two comparison parameters in the "if" statement, which seemed to work. But thank you for your suggestion and direction.