Add an alert(pop-up message) to the UI action when all tasks are not closed in the Problem ticket.

SNOW32
Giga Expert

Hello Everyone,

I hope you all are doing good. 🙂

I need to add an alert(pop-up message) as "Please close all Ptasks before taking this action" to the UI action when all tasks are not closed in the Problem ticket. And If task is closed then state field will update.

When we will click UI Action "RCA Completed", if task is closed then state will update to "RCA Completed" and if task is not closed then it will pop up a message.

I wrote an UI action but after closing the task also ,state is not updating. 😞

In the Problem ticket,

UI action: RCA Completed

Phase: Investigation & Analysis

Stage: Investigation & Analysis 

State: Work in Progress

find_real_file.png

find_real_file.png

Please refer below, I tried to write script in UI Action, but after closing the task also ,state is not updating. 😞

var gr = new GlideRecord("problem_task");
        gr.addQuery("state", '1'); // value 1 is for Open
        gr.addQuery("active", 'true');
        gr.addActiveQuery();
        gr.query();

        if (gr.getRowCount() > 0) {
            alert("test");  alert is not working in server script( like UI Action)
           //gs.log("2"); not working as well
            gs.addInfoMessage("Please close all Ptasks before taking this action"); its working but the message is popping up in the list rather than in the form.
    }
else
    { current.state = 105; Not updating
    //current.u_stage = 103;
    current.approval = "requested"; Not updating
    current.update();

   }

 Any ideas on how this might be able to function?

 Thanks in advance!

Swati

 

1 ACCEPTED SOLUTION

ccajohnson
Kilo Sage

Since you do not want the record to save, you should return after your info message.

var gr = new GlideRecord("problem_task");
gr.addQuery("state", '1'); // value 1 is for Open
gr.addQuery("active", 'true');
gr.addActiveQuery();
gr.query();

if (gr.getRowCount() > 0) {
//    alert("test");  alert is not working in server script( like UI Action)
//    gs.log("2"); not working as well
    gs.addInfoMessage("Please close all Ptasks before taking this action");
    return;
} else { 
    current.state = 105;
    //current.u_stage = 103;
    current.approval = "requested";
    current.update();
    action.setRedirectURL(current); //Added this in to stay on the page.
}

I also added a setRedirectURL method to stay on the same page after the button is pressed.

View solution in original post

2 REPLIES 2

ccajohnson
Kilo Sage

Since you do not want the record to save, you should return after your info message.

var gr = new GlideRecord("problem_task");
gr.addQuery("state", '1'); // value 1 is for Open
gr.addQuery("active", 'true');
gr.addActiveQuery();
gr.query();

if (gr.getRowCount() > 0) {
//    alert("test");  alert is not working in server script( like UI Action)
//    gs.log("2"); not working as well
    gs.addInfoMessage("Please close all Ptasks before taking this action");
    return;
} else { 
    current.state = 105;
    //current.u_stage = 103;
    current.approval = "requested";
    current.update();
    action.setRedirectURL(current); //Added this in to stay on the page.
}

I also added a setRedirectURL method to stay on the same page after the button is pressed.

Phuong Nguyen
Kilo Guru
Hi Swati,
You UI action is not set to run on the client side and alert() is a client side function. If you check "Client" check box in your UI action, you should then be able run your client side logic/script. I should note that you would need to adapt your codes to work on the client side appropriately.
Refer to this link on how you can run both client side and server side code in the ui action: https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/
Let me know how it goes 🙂
Thanks,
Phuong