How to Script an 'alert' from a UI Action?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-03-2012 12:36 PM
When I click the 'Close Task' button on an sc_task form, I want to be able to check the corresponding RITM form to ensure some custom fields are populated before I allow the task to close. If the fields are not populated, I'd like to have an 'alert' tell the analyst about the error. I've build a UI action on the 'Close Task' button (screenshot attached), but I can't quite get it to work. The script I've written looks like this...
~~~~~~~~~~~~~~~~~~~~~
var ritm = new GlideRecord('sc_req_item');
ritm.get(current.request_item.sys_id);
If (ritm.u_access_application.nil() || ritm.u_access_server.nil() || ritm.u_access_website.nil()){
alert('Mandatory fields on the RITM form are not populated, you cannot proceed until they are.');
current.setAbortAction(true);
}
current.state = 3;
current.update;
~~~~~~~~~~~~~~~~~~~~~
Has anyone done alerts from a UI action before? Checked fields on the RITM form from the task form through a UI Action? See anything wrong with my code?
Any help would be very much appreciated!
With thanks... Karen

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-03-2012 12:59 PM
Alerts are client-side code. The update to the record is server-side code. In order to get this to work, you have to design your UI action to use both. I've written a post on how this can be accomplished at SNGuru.
http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2012 08:39 AM
Hi Mark,
REally appreciated you getting back to me. I gave your code a try, I'm just struggling with checking a field on the RITM from from the TASK form. Would you mind taking a quick look at my code... is there something I'm missing?
~~~~~~~~~~~~~~~~~~~~
Name: Close Task
Action name: check_ritm
Client: True
Form button: true
Onclick: check();
Condition: current.state < 3 && current.approval != 'requested' && current.short_description == 'BT Documentation - New Vendor Group - VPN Access'
//Client-side 'onclick' function
function check(){
var ritm = new GlideRecord('sc_req_item');
ritm.get(current.request_item.sys_id);
if(ritm.u_access_application.nil()){
alert("Mandatory fields on the RITM form are not populated, you cannot proceed until they are.");
g_form.showFieldMsg('comments','Mandatory fields on the RITM form are not populated, you cannot proceed until they are.','error');
return false; //Abort submission
}
//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'check_ritm'); //MUST call the 'Action name' set in this UI Action
}
~~~~~~~~~~~~~~~~~~~~
With my thanks... Karen
PS. You probably get this alot, but I really appreciate all the info your blog provides and we've put quite a few of your suggestions into our instances. The 'Edit Workflow' related link is the most recent and we just love it! What a time saver. We met last year at Knowledge12... you helped Ian Broz and myself get green-checkmarks and red-x images into approvals. Hope you are doing well!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2012 10:27 AM
Thanks Karen, I remember that conversation with you and Ian well. I'll be at knowledge again this year so be sure to say hello if you're going and see me there.
I'm not 100% sure, but I think your problem is in this line...
if(ritm.u_access_application.nil()){
I think .nil() might not be supported client-side so you might just have to check for empty quotes there. You should test the value of 'ritm.u_access_application' by adding it to an alert message in that line to see.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2012 07:58 PM
Karen try this: You're trying to pull a value, client side, from the current record however you're asking for it in server side talk (3rd line).
function check(){
var ritm = new GlideRecord('sc_req_item');
ritm.get(g_form.getValue('request_item'));
if(ritm.u_access_application.nil()){
alert("Mandatory fields on the RITM form are not populated, you cannot proceed until they are.");
g_form.showFieldMsg('comments','Mandatory fields on the RITM form are not populated, you cannot proceed until they are.','error');
return false; //Abort submission
}
//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'check_ritm'); //MUST call the 'Action name' set in this UI Action
}