Insert New Record and Update Current in Same UI Action

Robbie Lacivita
Tera Guru

I am attempting to write a UI action that inserts a new incident, and then goes back and updates the current incident. I can't seem to get it to update the State and Hold Reason fields, but everything else is working as intended. Is there an obvious flaw in my code that I'm missing, or does this need to be handled differently?

//Client side 'onClick' function
function escalateIncidentPrchk() {
    var ans = confirm('Are you sure you wish to escalate this incident? This action cannot be reversed.');
    if (ans == true) {
        gsftSubmit(null, g_form.getFormElement(), 'escalate_inc');
    } else {
        return false;
    }
}

//Code that runs without 'onClick'
//Ensure call to server-side function with no browser errors

if (typeof window == 'undefined')
    escalateIncident();

//server side function
function escalateIncident() {
    var gr = new GlideRecord('incident');
    gr.initialize();
    gr.caller_id = current.caller_id;
    gr.location = current.location;
    gr.contact_type = current.contact_type;
    gr.impact = current.impact;
    gr.urgency = current.urgency;
    gr.short_description = current.short_description;
    gr.description = current.description + ('/n/nThis incident was created as an escalation from Incident: {1}. Please see the parent incident {0} under the "Related Records" tab for more information.', [gr.number, current.number]); 
    var sysID = gr.insert();

    current.state = 3;
    current.hold_reason = 10;
    current.parent_incident = sysID;
    var mySysID = current.update();
    if (global.JSUtil.notNil(sysID)) {
        gs.addInfoMessage(gs.getMessage('Incident {1} escalated from {0}', [gr.number, current.number]));
    }
    action.setRedirectURL(gr);
    action.setReturnURL(current);

}

Thanks in advance!

Robbie

6 REPLIES 6

Jaspal Singh
Mega Patron
Mega Patron

Hi Robbie,

 

Only thing I see is current.update() a miss. Try adding it just before 

action.setRedirectURL(gr);

action.setReturnURL(current);

Jaspal,

I moved the line var mySysID = current.update()to just before the action.setRedirectURL(gr); but I'm still receiving the same result. parent_incident is set to the new record, but state and hold_reason do not get updated.

Thank you,

Robbie

Hi,

so it is creating the new incident but not populating state, hold reason

so did you check any before update BR sets the state and hold reason

Also check field names is correct

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur,

I have ensured that there are no additional business rules running that would set the state and hold reason. If it helps, I've moved this to a clean, OotB New York instance to ensure no customizations are interfering, and I'm getting the same results. The field names and integer values are correct.

What is most confusing to me is that parent_incident gets set to the correct value, while state and hold_reason don't. In fact, I just noticed its reverting the state back to New.

Thanks,

Robbie