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

DrewW
Mega Sage
Mega Sage

It looks like you are returning an array of objects.  Have you considered using JSON.stringify on it and returning a string?

Have you thought about using a business rule that runs when the comments is update and another one for when the work notes is updated and then triggering the update to the other system from there?  You can then just use getJournalEntry to get the last one and pass it to whatever you are using to send the data to the other system.

 

https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server_legacy/c_GlideElementAPI#r_G...

 

Hi Drew,

 

I will try the JSON.stringify and let you know how I get one. I haven't done the business rule approach as I had built the API to run off a flow.

 

Thanks,

 

Rob

The create an action that you pass in a GlideRecord and it will have two outputs.  One is comment and the other is work note.  Then use GlideRecord.comment.getJournalEntry(1) and GlideRecord.work_note.getJournalEntry(1) to set the outputs.  No reason to be anymore complicate than that.  You can then use the output of your action in the other parts of the flow/subflow.

 

You can also just build the flow as a subflow and pass in the values and then use the flow API to call it from a BR.

 

Hi Drew,

 

Thank you for the suggestion but I'm struggling with setting up this action. In the inputs section which option am I putting for type, 'Glide var', as I can't choose records.sys_journal_field? I have also tried just setting the input to the sys_id of the updated incident and then running a script step with the glide record in it but again that is still returning no values?

 

(function execute(inputs, outputs) {
var element = '';
var gr = new GlideRecord('sys_journal_field');
gr.addQuery('element_id', inputs.number_sys);
gr.setLimit(2);
gr.orderByDesc('sys_created_on');
gr.query();

while(gr.next()){
element = gr.getValue('element');
if (element === 'comments') {
outputs.comment = gr.getJournalEntry(1);
} else if (element === 'work_notes') {
outputs.work_note = gr.getJournalEntry(1);
} else {
// Handle the case where the journal entry is neither a comment nor a work note
// For example, you could set another output to the journal entry text
outputs.other = gr.getJournalEntry(1);
}
}
})(inputs, outputs);

 

Thanks in advance of any further guidance.

 

Rob