Encounter Business Rule Script Processing Issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2024 08:08 PM
Hello,
I'm currently learning about Business Rule Scripts, but I've encountered an issue. While the business rule runs on sc_req_item table without errors, it fails to correctly populate the 'requested for' name in the 'sc_task'.
Can someone please help review the code and let me know where I did incorrectly. Thank you
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('sc_task');
gr.initialize();
gr.request_item = current.sys_id;
var senderName = getUserByEmail('abraham.lincoln@example.com');
if (senderName) {
gr.request_item.requested_for = senderName;
gr.description = "Requester: " + senderName;
} else {
return null;
//gs.info("Unable to find user for email: abraham.lincoln@example.com");
}
gr.insert();
})(current, previous);
// Function to get name from sys_user table based on email
function getUserByEmail(userEmail) {
var userGR = new GlideRecord('sys_user');
if (userGR.get('email', userEmail)) {
// return userGR.name.toString();
return userGR.getUniqueValue();
} else {
return null;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2024 08:50 PM
Hi, if you take close look a look at the sc_task table, it does not have a requested_for field, and the field shown on the sc_task form is the requested_for of the related sc_req_item record, which means that your core issue is the field you are trying to set does not exist.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2024 09:20 PM
Hello @Tony Chatfield1
I wasn't aware of that. Thank you for bringing it to my attention. I have a situation where one RITM generates multiple tasks, each of which should have a different requester name. Do you have any suggestions on how I can manage this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2024 09:44 PM
Hi, you could consider adding an additional sys_user reference field to sc_task so that you can deliver this requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2024 11:29 PM
Thanks for the suggestion. I've never added a new field to an existing form before, so I want to clarify the steps to make sure I don't make any mistakes.
1. I should go to the form design and add the new field from there. Is that the correct approach?"
2. I don't want the new field to appear on every SC task. Is there a way to hide it based on certain criteria?
3. Will the new field be overwritten after the next ServiceNow upgrade?