Solved: creating an alert from a UI action - ServiceNow Community

creating an alert from a UI action

scottjus82
Kilo Guru

Morning guys,

 

We have a button on our change form that takes our change back to an editable phase (if a client needs to change dates after approval etc) We want to have a confirm message pop up on clicking that button that warns the user that the change will need to be re approved, they then click yes to continue. We have a similar script in incident under a client script that warns the user if someone else has over written the ticket since they opened it and do they want to overwrite that update. I am assuming the script is not working as its been written for use in a client script not a UI Action. the script is below.

 

function onSubmit() {

      confirm(getMessage("Please note this will require the change to go back through the approval process."));

     

}

current.phase = "In Definition";

current.u_backed_out = "true";

current.u_cab_approved = "false";

action.setRedirectURL(current);

 

 

 

current.update();

 

Any thoughts?

1 ACCEPTED SOLUTION

Jake Gillespie
Mega Guru

Hi David,



Try putting the following script in your UI Action:



//Client-side 'onclick' function


function changeToEdit(){ // this is function called on the "onClick" field


if(confirm(getMessage("Please note this will require the change to go back through the approval process."))){


  // run Server Action


  gsftSubmit(null, g_form.getFormElement(), 'changeToEditAction'); // this refers to the "Action name"


}


else{


  return false; // cancel action if confirm response is false/cancel


}


}




//Code that runs without 'onclick'


if(typeof window == 'undefined')


runPricingBusRuleCode(); // call Server code




//Server-side


function runPricingBusRuleCode(){


current.phase = "In Definition";


current.u_backed_out = "true";


current.u_cab_approved = "false";


action.setRedirectURL(current);


}



Note that this script refers to the current UI Action as having an "Action Name" of changeToEditAction, and an "onClick" function name of changeToEdit().



Regards,


Jake


View solution in original post

8 REPLIES 8

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Steve,



By default UI Actions run server side code, but they can be configured to run client, server, or both. The code you have posted is a mix of both, so he is a pretty good article explaining how to do both in a ui action.



http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/


Anurag Tripathi
Mega Patron
Mega Patron

Hi Steve,



In general a UI action can be client side or server side. current and confirm/alert wont work in same.



Your UI action contains both client side and server side code. Sow hat you need here is a ui action that can incorporate both of these.



Please check this link and let me know if you still have any doubts. This link explains how you can create a ui action that can work on both server and client side



http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/


-Anurag

Kalaiarasan Pus
Giga Sage

Check this thread. Had answered the same thing



https://community.servicenow.com/message/725976#725976


Jake Gillespie
Mega Guru

Hi David,



Try putting the following script in your UI Action:



//Client-side 'onclick' function


function changeToEdit(){ // this is function called on the "onClick" field


if(confirm(getMessage("Please note this will require the change to go back through the approval process."))){


  // run Server Action


  gsftSubmit(null, g_form.getFormElement(), 'changeToEditAction'); // this refers to the "Action name"


}


else{


  return false; // cancel action if confirm response is false/cancel


}


}




//Code that runs without 'onclick'


if(typeof window == 'undefined')


runPricingBusRuleCode(); // call Server code




//Server-side


function runPricingBusRuleCode(){


current.phase = "In Definition";


current.u_backed_out = "true";


current.u_cab_approved = "false";


action.setRedirectURL(current);


}



Note that this script refers to the current UI Action as having an "Action Name" of changeToEditAction, and an "onClick" function name of changeToEdit().



Regards,


Jake