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.

Adding Related SCTASK record to an Incident ticket.

RobertPC
Tera Contributor

I have a script (see below) that converts Incident tickets to REQ/RITM/SCTASK. One of the last steps of the script is to add a note to the Incident activity log and then cancel the Incident. I created a new field on the Incident form called Related Task (u_reference_2) so we can have a link between the canceled incident and the new REQ/RITM/SCTASK. The issue I am having is that I cannot get the newly created SCTASK number to populate into the new Incident filed. I'm new to this whole script stuff, so if any one can help me I would appreciate it greatly.

 

// Below few lines of code works as a Client side script
function CreateStask() {
   
if(g_form.getValue('comments') == ''){
//Remove any existing field message, set comments mandatory, and show a new field message
g_form.setMandatory('comments', true); // Setting Additional comments are mandatory.
g_form.showFieldMsg('comments','Additional comments are mandatory when converting an Incident to SC Task.','error');
 
return false; //Abort submission
}
gsftSubmit(null, g_form.getFormElement(), 'create_catalog'); //MUST call the 'Action name'(Here action name is 'create_catalog' set in this UI Action
}
 
 
createTask();
 
function createTask(){
var Req = new GlideRecord('sc_request');
Req.initialize();
Req.setValue('short_description',current.short_description);
Req.setValue('requested_for',current.caller_id);
Req.requested_for= current.caller_id;
Req.insert();
 
var RITM = new GlideRecord('sc_req_item');
RITM.initialize();
RITM.setValue('request', Req.sys_id);
RITM.setValue('parent', Req.sys_id);
//RITM.setValue('assignment_group',current.assignment_group);
RITM.setValue('short_description',current.short_description); // give short description for the task
RITM.setValue('requested_for',current.caller_id);
RITM.insert();
//gs.addInfoMessage(RITM.number); //This line just for dispalying RITM number after conversion from incident to sc task.
 
var taskRec = new GlideRecord('sc_task');
taskRec.initialize();
taskRec.setValue('request_item',RITM.sys_id);
taskRec.setValue('requested_for',current.caller_id);
taskRec.setValue('assignment_group',current.assignment_group);
taskRec.setValue('assigned_to',current.assigned_to);
taskRec.setValue('short_description',current.short_description); // give short description for the task
taskRec.setValue('description',current.description);
 
 
taskRec.insert();
//gs.addInfoMessage(taskRec.number);
   current.comments .setJournalEntry("The Incident was canceled due to wrong ticket type, the Smart Desk has created Service Catalog Task " + taskRec.getDisplayValue());  
taskRec.setValue('u_reference_2',taskRec.current.number);
current.state ='8'; // Cancelling the incident after conversion to SC Task
current.setValue('active', 'false'); // Incident will be active = false after conversion
 
current.update();
action.setRedirectURL(taskRec);
}
1 ACCEPTED SOLUTION

Jon23
Mega Sage

Hi @RobertPC ,

 

Try changing the line:

taskRec.insert();

 To:

var scTaskSysId = taskRec.insert();

Then use the variable to update the current incident:

current.setValue('u_reference_2', scTaskSysId);

 

View solution in original post

2 REPLIES 2

Jon23
Mega Sage

Hi @RobertPC ,

 

Try changing the line:

taskRec.insert();

 To:

var scTaskSysId = taskRec.insert();

Then use the variable to update the current incident:

current.setValue('u_reference_2', scTaskSysId);

 

RobertPC
Tera Contributor

That works great, thanks so much for the help