Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Change - Revert to New - what did I do wrong?

Jeffrey Barton
Tera Contributor

Hello out there awesome developers!
I am new to scripting and put together the following script based on a business requirement for a "revert to new" UI Action.  They want it to set the state back to new and delete all related approval records (all - meaning all states of approval records not just approved).
So here's what I wrote (pieced together):

 

function moveToNew() {
    g_form.setValue("state", "-5");
    gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_new");
    var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval','current.sys_id');
gr.query();
gr.deletemultiple();
}

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

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}

 

 
With the UI Action settings:
2023-09-21_8-57-40.png
6 REPLIES 6

Ankur Bawiskar
Tera Patron

@Jeffrey Barton 

make the UI action as server side

1) Client checkbox - uncheck

2) Onclick field - clear this

3) Script

var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.query();
gr.deletemultiple();
action.setRedirectURL(current);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

That doesn't seem to have worked.  All the approval records are still there in the approval section.

Hi @Jeffrey Barton ,

 

In deleteMultiple 'M' is suppose to be capital Jeffrey. I have update the code n pasted below.

var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.query();
gr.deleteMultiple();
action.setRedirectURL(current);

 

Please mark my answer helpful & Accepted if it resolves your query.

 

Thanks,

Danish Bhairagdar

 

 

Mike Strik
Tera Guru

Can you try:

 

function moveToNew() {
    // Make sure that "-5" is indeed the correct state value for "new" in your ServiceNow instance. 
    g_form.setValue("state", "-5");
    gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_new");

    // Query for related approval records
    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id); // Use 'current' to reference the current record
    gr.query();

    // Delete all related approval records
    while (gr.next()) {
        gr.deleteRecord();
    }

if (typeof window == 'undefined') {
    setRedirect();
}

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}