
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2019 02:31 PM
i have this below piece of code in a business rule on sc_task
business rule conditions are as below
the business rule intends to copy work notes of one task into all adjacent tasks of the same request item.
the code however doesn't work and i do not get any error. when i printed different messages, it seems all the code works fine except for the part where i am trying to update the work notes. i de-activated all other business rules on task and sc_task and it still doesn't work.
the execution reaches till line 16.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019 12:45 PM
Hi Ravish,
I had a similar requirement to copy customer comments from a sc_task to sc_req_item. I had to clean up the comments and compare in order to prevent an infinite loop. I've modified the code below which should work for your use case:
(function executeRule(current, previous /*null when async*/) {
var taskSysId = current.getUniqueValue();
//gs.info(gs.getMessage("BLS: taskSysIdk: {0}",[taskSysId]));
var trgt = new GlideRecord("sc_task");
trgt.addQuery("parent",current.parent);
trgt.addQuery("sys_id","!=",taskSysId);
trgt.addActiveQuery();
trgt.query();
while (trgt.next()) {
gs.info("BLS: trgt: " + trgt.number);
// Before we update the target task, we need to prevent an infinite loop by ensuring the work_notes that is causing this business rule to run is not the same text that was in the last work_notes on the target task.
//return the last work_notes journal entry for the current task and extract the journal text below the line break.
var workNotes = cleanWorkNotes(current.work_notes.getJournalEntry(1));
//return the last work_notes journal entry for the Catalog Task and extract the journal text below the line break.
var trgtWorkNotes = cleanWorkNotes(trgt.work_notes.getJournalEntry(1));
gs.info(gs.getMessage("BLS: Current Task: {0} >>> Current Task Work Notes: {1} - Target Task: {2} >>> trgtWorkNotes: {3} indexOf: {4}",[current.number,workNotes,trgt.number, trgtWorkNotes,trgtWorkNotes.indexOf(workNotes)]));
/***** NOTE: If the same work_notes are entered twice, the second journal entry will not be copied to the target task as it will match the first work_notes *****/
if (trgtWorkNotes.indexOf(workNotes) < 0) {
// OnAfter business rules cannot process comments so we need to pull from the journal
trgt.work_notes = workNotes;
trgt.update();
}
}
/**
* Description: Cleans up the journal entry to remove timestamp, audit information and trim any white space.
* Parameters:
* @PARM1 - journalEntry (String) - Journal entry by index number i.e. current.work_notes.getJournalEntry(1)
* Returns: note (String) - cleaned up journal entry with just the work_notes information.
*/
function cleanWorkNotes(journalEntry){
var note = "";
if (journalEntry) {
//We need to clean-up the journal entry
//return all text after the first line break in the journal entry (first line contains timestamp and audit information).
note = journalEntry.substr(journalEntry.indexOf('\n')+1);
note = note.trim(note); //remove any whitespace before and after string
}
return note;
}
})(current, previous);
Let me know if it worked for you.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2019 03:43 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 08:29 AM
Hello Brent,
Thank you so much for the help. I do have Insert and Update checked and the business rule is set to after as well. I have tried several combinations of State is, State is not, and State changes but I am still only able to get the work notes to pull through if I add them to a task after it is closed. Any further assistance would be greatly appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 12:33 PM
The script will only populate work_notes across tasks that are active at the same time.
Is the task that you are trying to update active at the same time as the subsequent task?
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 12:51 PM
So in my setup the 2nd task gets created once the 1st task is closed. Some of my requests has as many as six tasks but each one needs to be completed before the next task is opened. I did discover that I can get the notes from a 2nd task to pass to a 3rd task but still am not able to get the 1st task notes to go to the 2nd unless the first task is closed before adding the work notes. If your script is designed to only pass from one active task to another, it sounds like I would need to make some modifications in order to get my desired results is that sounding correct? I am just confused as to how it passes from the 2nd to 3rd but not from the 1st to 2nd task?
Thanks again for all of your help with this,
Jason
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019 01:30 PM
You'll probably need to change the logic of the business rule to look up the previous task in the RITM and copy all the work_notes to the newly created task. This could be run as a before business rule and just run on an insert.
Something like the following would do it:
(function executeRule(current, previous /*null when async*/) {
var preTask = new GlideRecord("sc_task");
preTask.addQuery("request_item", current.request_item);
preTask.orderByDesc("sys_created_on"); //order so last created task is returned first
preTask.setLimit(1); //set the limit to 1. We only need to return the last task which will hold work_notes from all subsequest tasks
preTask.query();
if (preTask.next()) {
current.work_notes = preTask.work_notes.getJournalEntry(-1); //copy all the previous tasks work_notes into the new task.
}
})(current, previous);
Give that a go and let me know how you got along.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.