Skip to content

Instantly share code, notes, and snippets.

@cmcdevitt
Last active April 7, 2023 21:29
Show Gist options
  • Select an option

  • Save cmcdevitt/d63a2886a390da205497bf63d60c6919 to your computer and use it in GitHub Desktop.

Select an option

Save cmcdevitt/d63a2886a390da205497bf63d60c6919 to your computer and use it in GitHub Desktop.
UI Action that runs a Client Script then a Server Script
/*
UI Action
Action name:reopen_incident
Client: True
Onclick: reopen()
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1005843
Note:
- getFormElement() - Returns the HTML element for the form.
- gsftSubmit()
-- Pass a control (button) or a form and action name if the control does not exist.
-- function gsftSubmit(control, /* optional */ form, /* optional */ action_name)
-- So the control / button is just another UI Action for example:
-- gsftSubmit(gel('sysverb_update'));//This could be used in a UI Action, UI Policy, or UI Script
*/
//Client-side 'onclick'
function reopen(){//Client Side Script
//Client Script Stuff here
//alert('DO IT - Client Side Script!');
//The use case is to TEST the Form and/or validate that the user really want to do this action
var resp = prompt("Mhhh... this looks sketchy... enter 'Yes' if you are sure!").toLowerCase();
if(resp == 'yes'){
//Call this UI Action Again, but this time run the Server Side Code!
gsftSubmit(null,g_form.getFormElement(),'reopen_incident');//This is the 'Action name' of the UI Action above
}else{
alert("Just what I thought, this is too sketchy to submit!");
}
}
//Code that runs without the 'onclick' function being called
if(typeof window == 'undefined'){//Test javascript for a global varable called 'window'
serverReopen();//Call Server Side Script!
/*
Server work could also be done here like:
current.update();
action.setRedirectURL(current);//Stay on the same record
*/
}
function serverReopen(){//Server Side Script!
gs.addInfoMessage("DO IT - Server Side Script!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment