Fully functional UI action
UI action to create an adhoc request from incident
table name of adhoc request = u_ad_hoc_request
Please see the code below, few things to mention in the below code
- gsftSubmit(null, g_form.getFormElement(), 'createadhocrequest'); // This line is needed for the UI action to work if there is a onclick event on the ui action. Replace 'createadhocrequest' with the 'Action name' on the UI action.
- The url that is build should follow below logic
++.do?sys_id=-1&sysparm_query=++++ and so on
- 3. gs.action.setRedirectURL(url); // This is the command to redirect to the new record
- 4. gs.action.setReturnURL(current); // This is the command to set the return to current record
- 5. Add a condition in the UI action to check if the adhoc_request is null (condition: current.adhoc_request.nil()), also add conditions to ensure that the incident state is in the states in which you would allow users to create an adhoc request out of it.
function runClientCode(){
//validate if this field is filled in..
g_form.setMandatory('assigned_to',true);
gsftSubmit(null, g_form.getFormElement(), 'createadhocrequest');// This line is important (//MUST call the 'Action name' set in this UI Action)
}
if(typeof window == 'undefined') { // Ensures serverside call of this code, runs without onclick
createAdhocRequest();
}
function createAdhocRequest() {
var url = gs.getProperty('glide.servlet.uri')+'u_ad_hoc_request.do?sys_id=-1&sysparm_query=';
url += 'category=' + current.category.toString();
url += 'subcategory=' + current.subcategory.toString();
url += 'requested_by=' + current.caller.toString();
url += 'requested_for=' + current.caller.toString();
url += 'priority=' + current.priority.toString();
url += 'assignment_group=' + current.assignment_group.toString();
url += 'assigned_to=' + current.assigned_to.toString();
url += 'short_description=' + current.short_description.toString();
url += 'description=' + current.description.toString();
url += 'comments=' + current.comments.toString();
url += 'work_notes=' + current.work_notes.toString();
url += 'sys_created_on=' + current.sys_created_on.toString();
url+= 'parent=' + current.sys_id;
gs.action.setRedirectURL(url);
gs.action.setReturnURL(current);
- current.state = 7 ; // set incident state to closed
current.work_notes ='adhoc request created';
current.comments' ='adhoc request created';
current.update();
}
Few thoughts Business rule on adhoc request table:
- After business rule
- Insert is checked
- This BR will have code like the one below, I have added a line to set the adhoc_request field on the incident table with the adhoc request created)
var gr = new GlideRecord('incident');
- gr.addQuery('sys_id',current.parent);
- gr.query();
if(gr.next()) {
gr.comments = 'This ticket has been converted to' + current.number+ '.'; // Add additional comments
gr.close_notes = 'This ticket has been converted to' + current.number+ '.';
gr.adhoc_request = current.sys_id;
gr.update();
}
Further reading : https://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/
https://community.servicenow.com/thread/172048
http://wiki.servicenow.com/index.php?title=GlideRecord#gsc.tab=0 (getLink method).
Thanks
Anil
https://www.servicenow.com/community/in-other-news/fully-functional-ui-action/ba-p/2270304