Return value of array in flow designer script

Rob Bushrod
Tera Guru

Hi,

I am trying to run a glide record onto the sys_journal_field table to get the last 2 results. I then need to see if they are comments or work notes and post them into the correct field across an API. I have run into some issues with my script, I can get it to log the values but can't seem to get it to return the values.

 

Here is the script I'm running;

var lastComment = [];
var gr = new GlideRecord('sys_journal_field');
gr.addQuery('element_id',fd_data.trigger.current.sys_id);
gr.setLimit(2);
gr.orderByDesc('sys_created_on');
gr.query();

while (gr.next()){
var comment = {
add_comment: gr.getValue('value'),
}
var lastComments = lastComment.push(comment);
return lastComments;
}
gs.info('RB in Update Flow Comments Script, comment count: ' + lastComment.length);
for(var i = 0; i < lastComment.length; i++){
var comment = lastComment[i];
gs.info('RB in Update Flow Comments Script, comment: ' + comment.add_comment);
}
 
Any help would be greatly appreciated or any other suggestions to how I can solve the underlying issue of, if a user updates both work notes and comments at the same time sending the last additional comment and last work note into the correct field in the receiving system.
 
Thanks,
 
Rob
1 ACCEPTED SOLUTION

Rob Bushrod
Tera Guru

Having attended a lab at Knowledge I was introduced to ServiceNow Remote Instance spoke - this has an action called Look up Worknotes/comments which does all this work for me and solves my issue. 

View solution in original post

6 REPLIES 6

The whole point of my suggestion to use getJournalEntry was to not query the sys_journal_field table.  You should just be passing in a GlideRecord of type Task and a string called say journalField.  Then pass both of those to the script and just do

 

output.journalEntry = "";
if(inputs.myTaskGlideRecord.isValidField(inputs.journalField))
   output.journalEntry = inputs.myTaskGlideRecord[inputs.journalField].getJournalEntry(1);

 

You can then call the action when the comments changes to get the last comment and call it for work notes when work notes changes to get the last work note.

 

Rob Bushrod
Tera Guru

Having attended a lab at Knowledge I was introduced to ServiceNow Remote Instance spoke - this has an action called Look up Worknotes/comments which does all this work for me and solves my issue.