The CreatorCon Call for Content is officially open! Get started here.

Workflow activity timing issues

CJB
Tera Expert

Hi, team! I have been struggling with a workflow activity not grabbing the most recent comments from sys_journal_field.

 

In a UI action, on the client side, I'm setting a value in the comment field. In the server side of this UI action, it triggers a workflow activity. This workflow activity does a GlideRecord query on the sys_journal_field table to pull in the most recent comment. However, the comment that I added with the UI action does not get pulled in. Instead, the workflow activity pulls in a comment previous to the one that posts with the UI action. It's almost like the workflow activity gets triggered before the UI action even finishes updating the record.

 

Any ideas on how to resolve this? Thanks.

6 REPLIES 6

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @CJB - Can you post the UI action script here? I think you could use gs.eventQueue() to generate an event after calling update(), and then use that event to trigger the workflow activity.

jcmings
Mega Sage

I believe this is happening because the server-side code is running immediately when the UI action is clicked, which is technically before the comment is posted. Your script would be helpful to take a look at.

CJB
Tera Expert

Thank you for responding @Sheldon  Swift and @jcmings. You'll see a substate being set to -1. The substate is being used as a way to trigger a Wait For workflow activity and If workflow activity (Approve/Reject). Once rejected, the run script workflow activity is executed.

Reject UI Action

 

//Client-side 'onClick' function
function Btn_Reject_onclick() {
    var rej_rat = g_form.getValue('comments');
    if (rej_rat == '') {
        g_form.setDisplay('comments', true);
        g_form.setMandatory('comments', true);

        g_form.hideFieldMsg('comments');
        g_form.flash('comments', "#FFFACD", -2);
        g_form.showFieldMsg('comments', 'Please add a comment.', 'error');
        g_form.addInfoMessage('Please add a comment.');
        return false;
    } else {
        g_form.setValue('comments', rej_rat);
    }

    g_form.setMandatory('close_code', false); // Prevent mandatory fields check
    g_form.setMandatory('close_notes', false); // Prevent mandatory fields check

    gsftSubmit(null, g_form.getFormElement(), 'Btn_Reject');
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
    Btn_Reject_server();

//server-side function
function Btn_Reject_server() {
    current.u_substate = -1;
    current.comments = "Task has been rejected";
    current.update();
    action.setRedirectURL(current);
}

 


Run Script Workflow Activity

 

current.u_substate = '0';
var ID = current.ID;

var sjf = new GlideRecord('sys_journal_field');
sjf.addEncodedQuery("element_id=" + current.sys_id + "^element=comments^value!=Task has been rejected");
sjf.orderByDesc('sys_created_on');
sjf.setLimit(1);
sjf.query();
if (sjf.next()) {
    var note = sjf.value.toString();
}

var a = new TestRestApi();
a.putIssue(ID, note);

 

If you already have the comment string (stored in rej_rat) from the UI action and only need that latest comment, there's no need to query the sys_journal_field table. You can simply pass that string directly to your a.putIssue(ID, rej_rat) call.