Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

VaranAwesomenow
Mega Sage

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

  1. 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.
  2. The url that is build should follow below logic

<instance url>+<tablename>+.do?sys_id=-1&sysparm_query=+<field1>+<field1value>+^<field2><field2value>+^<field3><field3value> and so on

  1. 3.   gs.action.setRedirectURL(url); // This is the command to redirect to the new record
  2. 4.   gs.action.setReturnURL(current); // This is the command to set the return to current record
  3. 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);

  1. 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:

  1. After business rule
  2. Insert is checked
  3. 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');

  1. gr.addQuery('sys_id',current.parent);
  2. 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

https://community.servicenow.com/community/it-service-management/blog/2015/05/06/getting-that-url-ri...

http://wiki.servicenow.com/index.php?title=GlideRecord#gsc.tab=0 (getLink method).

Thanks

Anil