How to create checklist for Template
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2023 08:47 AM
Hi all,
How to add the checklist item form checklist template in to change request work notes whenever new change request is created using script include we tried using after insert business rule which is not working as expected .
When ever error message is throwing for some other field at that time when change request is created the check list item is not getting attached in change request work notes but it is working fine when there is no error message.
And the error message is showing multiple times because of this BR.
Can anyone help me for this.
var theList = new GlideRecord("checklist");
theList.initialize();
theList.table = "change_request";
theList.owner = gs.getUserID();
theList.document = current.sys_id;
var listId = theList.insert();
var checklistArr = {};
var grTemplate = new GlideRecord("checklist_template");
grTemplate.addQuery("name", "Global Template");//added Global template to add all the 9 checklist item at once.
grTemplate.query();
if(grTemplate.next())
{
checklistArr = JSON.parse(grTemplate.getValue('template'));
//Loop through template and create checklist
for(var key in checklistArr.items)
{
var chki = new GlideRecord('checklist_item');
chki.initialize();
chki.setValue('checklist', listId);
chki.setValue('name', checklistArr.items[key]['name']);
chki.setValue('order', checklistArr.items[key]['order']);
chki.insert();
}
}
})(current, previous);
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2023 08:53 AM
Based on the description provided, it seems like you are trying to add a checklist item from a checklist template to the work notes of a newly created change request in ServiceNow using a script include. However, you are facing issues with the after insert business rule, which is not working as expected, and the error message is showing multiple times.
To troubleshoot this issue, you can try the following steps:
Check the business rule: Verify that the after insert business rule is correctly configured and is triggered when a new change request is created. You can check this by adding a debug statement in the business rule to log its execution.
Check the error message: Check the error message that is being thrown and investigate why it is occurring. This error message might be related to some other field or business rule, and it might not be directly related to the checklist item insertion.
Use try-catch block: To avoid the issue with multiple error messages, you can use a try-catch block in your script include to catch the error and handle it gracefully. This will prevent the error message from being displayed multiple times and will also allow the checklist item insertion to occur.
Debug the script include: Debug the script include by adding debug statements at various points to verify that the checklist item is being inserted correctly and that there are no errors in the script.
Here's an updated version of your script include that includes a try-catch block to handle errors gracefully:
(function executeRule(current, previous) {
var theList = new GlideRecord("checklist");
theList.initialize();
theList.table = "change_request";
theList.owner = gs.getUserID();
theList.document = current.sys_id;
var listId = theList.insert();
var checklistArr = {};
var grTemplate = new GlideRecord("checklist_template");
grTemplate.addQuery("name", "Global Template");
grTemplate.query();
if(grTemplate.next()) {
checklistArr = JSON.parse(grTemplate.getValue('template'));
try {
for(var key in checklistArr.items) {
var chki = new GlideRecord('checklist_item');
chki.initialize();
chki.setValue('checklist', listId);
chki.setValue('name', checklistArr.items[key]['name']);
chki.setValue('order', checklistArr.items[key]['order']);
chki.insert();
}
} catch (e) {
gs.logError('Error inserting checklist item: ' + e);
}
}
})(current, previous);
Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!