Add a confirmation (pop-up) message to a UI action button

richelle_pivec
Mega Guru

I am trying to add a confirmation (pop-up) message to a UI action button. I have a button on our Task form that will close the existing task, launch one of my catalogs and populate the "requested for" field (which allows us to engage a different workflow for our end-users after a team reviews the original request submitted). I want to add a pop-up message that asks the IT user if they are sure they want to create the request to avoid accidental task closures.

I am able to get the message to pop-up, and I am able to get the UI action to take place (when I don't have the pop-up message in the code), but I can't seem to figure out how to get the two to work together. I saw some code about pulling the business rule in after the client script ran, but I'm not sure how to do that with this code. Any help is much appreciated.

thanks,

Richelle

Name: Create Request

Action Name: New Task

Client is checked

Onclick: runClientCode();

Condition: gs.hasRole("itil")

Form Button is checked

Form context menu is checked.

The script...it is popping up the message right now, but just refreshes back to the RITM and doesn't launch the new catalog or close the task like it should.

//Update saves and closes task before going to the catalog homepage  

function runClientCode(){

      var answer=confirm("Are you sure you want to make a new request and close this task?");

      if(answer==true)

{

current.u_related_request_below = true;

current.state = 3;

current.closed_at = gs.nowDateTime();

current.assigned_to = gs.getUserID();

current.update();

gs.addInfoMessage(' ' + current.number + ' has been closed to make this request');  

 

var shopCart = new GlideRecord('sc_cart');  

shopCart.addQuery('user',gs.getUserID());  

shopCart.query();  

while (shopCart.next()){  

    shopCart.deleteRecord();  

}  

 

var newCart = new GlideRecord('sc_cart');  

newCart.user = gs.getUserID();  

newCart.requested_for = current.request_item.request.requested_for.sys_id;  

newCart.special_instructions = "This is an IT-generated request created to fulfill " + current.request_item.getDisplayValue();  

newCart.insert();  

 

var url = "catalog_home.do?v=1&sysparm_catalog=bcfb4c5108fe3900a727632d3899241c&sysparm_catalog_view=catalog_general_request_review&sysparm_processing_hint=setfield:request.parent=";

url += current.sys_id;  

 

action.setRedirectURL(url);

}

else

{

return false;

}

}

1 ACCEPTED SOLUTION

richelle_pivec
Mega Guru

Thank you Pradeep. I found the answer here: http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/ where Mark gives the example for Create a Problem (in the comments).



Here's my code that worked with adjustments...



//Update saves task before going to the catalog homepage  


function runClientCode(){


    if(confirm('Are you sure you want to make a new request and close this task?\n\nThis Action Cannot Be Undone!')){


    //Call the UI Action and skip the 'onclick' function


    gsftSubmit(null, g_form.getFormElement(), 'NewTask'); //MUST call the 'Action name' set in this UI Action


}


    else{


          return false;  


    }


}



//Code that runs without 'onclick'


//Ensure call to server-side function with no browser errors


if(typeof window == 'undefined'){


  NewTask();


}


//Server-side function


function NewTask(){


current.u_related_request_below = true;


current.state = 3;


current.closed_at = gs.nowDateTime();


current.assigned_to = gs.getUserID();


current.update();



gs.addInfoMessage(' ' + current.number + ' has been closed to make this request');  


 


var shopCart = new GlideRecord('sc_cart');  


shopCart.addQuery('user',gs.getUserID());  


shopCart.query();  


while (shopCart.next()){  


    shopCart.deleteRecord();  


}  


 


var newCart = new GlideRecord('sc_cart');  


newCart.user = gs.getUserID();  


newCart.requested_for = current.request_item.request.requested_for.sys_id;  


newCart.special_instructions = "This is an IT-generated request created to fulfill " + current.request_item.getDisplayValue();  


newCart.insert();  


 


var url = "catalog_home.do?v=1&sysparm_catalog=bcfb4c5108fe3900a727632d3899241c&sysparm_catalog_view=catalog_general_request_review&sysparm_processing_hint=setfield:request.parent=";



url += current.sys_id;  


 


action.setRedirectURL(url);


}


View solution in original post

4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Richelle Pivec



Here is the modification of code. Can you try the same once.


function runClientCode(){


  var answer=confirm("Are you sure you want to make a new request and close this task?");


  if(answer==false)


  {


  return false;


  }


  //Call the UI Action and skip the 'onclick' function


  gsftSubmit(null, g_form.getFormElement(), '<button_action_name>');//MUST call the 'Action name' set in this UI Action


}


//Code that runs without 'onclick'


//Ensure call to server-side function with no browser errors


if(typeof window == 'undefined')


  runBusRuleCode();


function runBusRuleCode()


{


  current.short_description = 'test';


  current.update();


  var shopCart = new GlideRecord('sc_cart');


  shopCart.addQuery('user',gs.getUserID());


  shopCart.query();


  while (shopCart.next()){


  shopCart.deleteRecord();


  }


  var newCart = new GlideRecord('sc_cart');


  newCart.user = gs.getUserID();


  newCart.requested_for = '62826bf03710200044e0bfc8bcbe5df1';


  newCart.special_instructions = "This is an IT-generated request created to fulfill " + current.request_item.getDisplayValue();


  newCart.insert();


  var url = "catalog_home.do?v=1&sysparm_catalog=bcfb4c5108fe3900a727632d3899241c&sysparm_catalog_view=catalog_general_request_review&sysparm_processing_hint=setfield:request.parent=";


  url += current.sys_id;


  gs.addInfoMessage(url);


  action.setRedirectURL(url);


}



NOTE: In line 08 replace <button_action_name> with the action name of your UI Action button.button_action_name>button_action_name>


richelle_pivec
Mega Guru

Thank you Pradeep. I found the answer here: http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/ where Mark gives the example for Create a Problem (in the comments).



Here's my code that worked with adjustments...



//Update saves task before going to the catalog homepage  


function runClientCode(){


    if(confirm('Are you sure you want to make a new request and close this task?\n\nThis Action Cannot Be Undone!')){


    //Call the UI Action and skip the 'onclick' function


    gsftSubmit(null, g_form.getFormElement(), 'NewTask'); //MUST call the 'Action name' set in this UI Action


}


    else{


          return false;  


    }


}



//Code that runs without 'onclick'


//Ensure call to server-side function with no browser errors


if(typeof window == 'undefined'){


  NewTask();


}


//Server-side function


function NewTask(){


current.u_related_request_below = true;


current.state = 3;


current.closed_at = gs.nowDateTime();


current.assigned_to = gs.getUserID();


current.update();



gs.addInfoMessage(' ' + current.number + ' has been closed to make this request');  


 


var shopCart = new GlideRecord('sc_cart');  


shopCart.addQuery('user',gs.getUserID());  


shopCart.query();  


while (shopCart.next()){  


    shopCart.deleteRecord();  


}  


 


var newCart = new GlideRecord('sc_cart');  


newCart.user = gs.getUserID();  


newCart.requested_for = current.request_item.request.requested_for.sys_id;  


newCart.special_instructions = "This is an IT-generated request created to fulfill " + current.request_item.getDisplayValue();  


newCart.insert();  


 


var url = "catalog_home.do?v=1&sysparm_catalog=bcfb4c5108fe3900a727632d3899241c&sysparm_catalog_view=catalog_general_request_review&sysparm_processing_hint=setfield:request.parent=";



url += current.sys_id;  


 


action.setRedirectURL(url);


}


Perfect


Chetan26
Mega Contributor

Hello , 

I have raised a similar question & its not working , can someone check my code , I have done it in a similar fasion but it is not working 

 

https://community.servicenow.com/community?id=community_question&sys_id=efd8273adbd9ac50a08a1ea6689619b7