- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
09-18-2023 04:26 AM - edited 02-01-2024 06:23 PM
Hello There,
Have you ever came across a requirement where you need to populate all the variables of an RITM as Description of change, when the change is created from an RITM or Catalog task.
This script include can be used to get all of the variables from a RITM as text. It is client callable, so it can be used from any client script, and Server Side script like a business rule.
Script Include:
var VariablesToDescription = Class.create();
VariablesToDescription.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//For GlideAjax
getDescriptionClient: function() {
var ritm = this.getParameter('sysparm_ritm');
var descStr = this.getDescription(ritm);
return descStr;
},
//For ServerSide Access
getDescription: function(ritm) {
var descStr = '';
var ritmGr = new GlideRecord("sc_req_item");
if (ritmGr.get(ritm)) { //sys_id of RITM record
descStr = ritmGr.getDisplayValue() + ': ' + ritmGr.cat_item.getDisplayValue() + '\n';
descStr += this.getVariablesAsText(ritm);
}
return descStr;
},
getVariablesAsText: function(ritm) {
var descStr = '';
var ritmGr = new GlideRecord("sc_req_item");
if (ritmGr.get(ritm)) { //sys_id of RITM record
var varDataGr = new GlideRecord('sc_item_option_mtom');
varDataGr.addQuery('request_item', ritmGr.getUniqueValue());
varDataGr.orderBy('sc_item_option.order');
varDataGr.query();
var question_text = '';
var answer_text = '';
while (varDataGr._next()) {
question_text = varDataGr.sc_item_option.item_option_new.getDisplayValue();
answer_text = ritmGr.variables[varDataGr.sc_item_option.item_option_new.name].getDisplayValue();
if (!gs.nil(answer_text)) {
descStr += question_text + ': ' + answer_text + '\n';
} else {
descStr += question_text + ': \n';
}
}
}
return descStr;
},
type: 'VariablesToDescription'
});
GitHub Link: https://github.com/anveshmupparaju/servicenow/blob/a0c83e83a3d9d2501119d19a12f4553c566a4fae/Variable...
To use the script include, simply pass the sys_id of the RITM to the getDescription()
function. The function will return a string containing all of the RITM variables, formatted as a String.
Here is an example of how to use the script include from server side script:
var ritmNumber = 'RITM0010014';
var ritmGr = new GlideRecord('sc_req_item');
if(ritmGr.get('number', ritmNumber)){
var description = getDescription(ritmGr.getUniqueValue()); //description will now contain a string containing all of the RITM variables
}
You can then use the description
variable however you need. For example, you could make it description of another record or comments.
Here is an example of a client script that uses the getDescriptionClient()
function to get the RITM variables as text:
var ga = new GlideAjax('VariablesToDescription');
ga.addParam('sysparm_name', 'getDescriptionClient');
ga.addParam('sysparm_ritm', "your_ritm_sys_id"); //You need to pass the RITM sys_id
ga.getXML(ParseDescription);
function ParseDescription(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//answer variable contains the description including the variables
alert(answer);
}
You could then attach this client script to a button on the RITM form, so that users can click the button to view the RITM variables.
Benefits of using the script include
There are several benefits to using this script include to get RITM variables as text:
- It is easy to use, simply pass the sys_id of the RITM to the
getDescriptionClient
()
function. - It is client callable, so it can be used from any client script.
- It returns the RITM variables as a String, which is a flexible format that can be used in many different ways.
- It is a reusable script include, so you can use it in multiple places in your ServiceNow instance.
Conclusion
This script include is a valuable tool for any ServiceNow developer. It can be used to get RITM variables as text in a simple and efficient way.
- 3,331 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @AnveshKumar M ,
Thanks for the post.
In the
getVariablesAsText:
function of script include,
while (ritmGr._next())
to be replaced with
while (varDataGr._next())
then the above code will work perfectly.
Thanks for the post/article.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Rupesh Ram Chi1 Thanks for finding out, some how it missed my site while posting.
The article is now updated.