
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2017 08:53 AM
Hi folks,
I replicated this on a customer's Istanbul instance as well as on my own Developer instance from within a Business Rule and running as a Background Script:
var current = new GlideRecord('sc_req_item'); |
//current.get('number', 'RITM0011547');
current.get('number', 'RITM0010001');
var sys_journal_field = new GlideRecord('sys_journal_field');
sys_journal_field.addQuery('name', 'task'); | |
sys_journal_field.addQuery('element_id', current.sys_id); | |
sys_journal_field.addQuery('element', 'comments'); | |
sys_journal_field.addQuery('sys_created_by', gs.getUserName()); | |
sys_journal_field.orderByDesc('sys_created_on'); | |
sys_journal_field.query(); |
var commentText = sys_journal_field.value;
//var commentText = sys_journal_field.getValue('value'); | |||||||
|
if(sys_journal_field.next()) {
gs.print('Comment: ' + sys_journal_field.value, 'RITM comment BizRule'); | |
gs.print('Formatted Comment: ' + commentText, 'RITM comment BizRule'); |
}
The text in green shows my comment, whereas the text in red shows "undefined" or "null" depending on how I tried to set the commentText variable.
Anyone run into this before? Any ideas?
Thank you!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2017 10:18 AM
I just want to make sure I understand the requirements:
Instead of the BR firing an event, which triggers a notification, you want the comment to be added to the catalog tasks beneath the RITM? Or do you want to do both?
I would probably leave the BR as is, and create a new one which will copy the comments from the RITM down to each sc_task beneath it. Something like this:
Table: sc_req_item
when: after
filter conditions: active is true and additional comments changes
script conditions: gs.getUserID() == current.opened_by || gs.getUserID() == current.request.requested_for
(function executeRule(current, previous /*null when async*/) {
// Regex to strip header info from journal entries
var headerRE = /.+\n/;
// Get the latest comment in the record; strip out the header
var sComment = "Comment added from " + current.number + "\n\n";
sComment += current.comments.getJournalEntry(1).replace(headerRE, '');
var oTask = new GlideRecord("sc_task");
//oTask.addActiveQuery();
oTask.addQuery("request_item", current.sys_id);
oTask.addActiveQuery();
oTask.query();
while(oTask.next()) {
//gs.log("TM===>Updating " + oTask.number, "Q: Cascade Comments BR");
oTask.comments = sComment;
oTask.update();
}
})(current, previous);
Any comments added to the sc_task record should appear on the activity log.
Cheers,
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2017 09:20 AM
I don't think we will get the value of queried GlideRecord until we processed using next() or hasNext() methods
var commentText = sys_journal_field.value; //this will always gives undifined

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2017 09:26 AM
Thanks Raju. sys_journal_field.next() is being used in an if statement in the code I posted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2017 09:22 AM
Hi Taarik
Instead of gs.print() give gs.log()
Thanks
Prateek Gupta

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2017 09:26 AM
Thanks Prateek. I used gs.log in the Business Rule and gs.print in the background script.