Convert SCTask to an Incident

Gemma4
Mega Sage

Hi,

Does anyone have detailed instructions on converting an SCtask to an Incident?  This change would copy the sctask caller, short description to the incident and then close the sctask, request item and request. 

 


Can any help on this?

Thank you in advance!

 
 
 
3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@Gemma4 You need to create an onAfter Update business rule on SC Task table with conditions

1. Short description is <short description of your SC Task>

2. State Changes to Closed Complete

 

In this BR you can initialise a GlideRecord with incident table and assign caller, short description from the current object to the newly initialised incident object. run insert() method on the incident record and update the state of RITM and Request by updating their state to closed complete.

 

Let me know if you are looking for a script, I will provide the same.

 

Hope this helps.

Thank you @Sandeep Rajput that helps point me in the right direction. Yes could you help me with the script. I have been trying to use the following script to close the RITM but the technicians all use the sctask so I thought it best to try to close the sctask and ritm, request. Here's what I am using to close the ritm and it works perfectly but the link in the comments does not work

 

I did make sure this property is set to true in case that was the issue with the link glide.ui.security.allow_codetag

 

I would really prefer to close the sctask and then ritm and req but my first priority is to utilize as little code is possible and keep the process simple. Let me know what you think is best overall. 

thanks

 

 

/* Construct RITM Links
-----------------------------------------------------------------------------------------------------------------------*/
 
var ritmTable = current.getTableName();
var ritmSysID = current.getUniqueValue();
var ritmNum = current.getValue('number');
 
//SERVICE PORTAL: construct a link to the original RITM for inclusion in the customer comments of the incident.
var spRITMLink = spLink(ritmTable,ritmSysID,ritmNum);
 
//PLATFORM: construct a link to the parent RITM that itil users can use to navigate from the newly created incident.
var platformRITMLink = platformLink(ritmTable,ritmSysID,ritmNum);
 
/* Construct comments and work notes
-----------------------------------------------------------------------------------------------------------------------*/
var comments = '[code]Incident created from ' + spRITMLink + '<br><br>';
 
comments += '<strong>Catalog Item Information:</strong><br>';
comments += '<table>';
 
//Query RITM variables, utilising the variable label and displayValue to build a table.
var vars = current.variables.getElements();
for (var i = 0; i < vars.length; i++){
var question = vars[i].getQuestion();
comments += '<tr><td>' + question.getLabel() + ':</td><td>' + question.getDisplayValue() + '</td></tr>';
}
comments += '</table>[/code]';
 
//include customer comments from the RITM (-1 returns all journal entries for specified journal field)
var ritmComments = current.comments.getJournalEntry(-1);
 
if (JSUtil.notNil(ritmComments)) {
 
comments += '[code]<br><br><strong>Customer comments from ' + ritmNum + '</strong><br>[/code]';
comments += ritmComments;
}
 
var workNotes = 'Link to [code]' + platformRITMLink + '[/code] from within ServiceNow.';
 
/* Ascertain the Assignment Group
-----------------------------------------------------------------------------------------------------------------------*/
var assignmentGroup = getAssignmentGroup(ritmSysID);
 
/* Create Incident
-----------------------------------------------------------------------------------------------------------------------*/
var inc = new GlideRecord("incident");
 
inc.caller_id = current.request.requested_for;
inc.assignment_group = assignmentGroup;
inc.short_description = current.short_description;
inc.cmdb_ci = current.cmdb_ci;
inc.impact = current.impact;
inc.urgency = current.urgency;
inc.priority = current.priority;
inc.company = current.company;
inc.sys_domain = current.sys_domain;
inc.comments = comments;
inc.work_notes = workNotes;
inc.parent = ritmSysID;
 
var incSysID = inc.insert();
 
var incTable = inc.getTableName();
var incNum = inc.getValue('number');
 
/* Copy attachments to Incident
-----------------------------------------------------------------------------------------------------------------------*/
 
//copy any RITM attachments to the newly created incident
GlideSysAttachment.copy(ritmTable, ritmSysID, incTable, incSysID);
 
/* Construct Incident Links
-----------------------------------------------------------------------------------------------------------------------*/
 
//SERVICE PORTAL: construct a link to the Incident for inclusion in the customer comments of the RITM.
var spIncLink = spLink(incTable,incSysID,incNum);
 
//PLATFORM: construct a link to the Incident that itil users can use to navigate from the parent RITM in platform.
var platformIncLink = platformLink(incTable,incSysID,incNum);
 
/* Update comments and work notes
-----------------------------------------------------------------------------------------------------------------------*/
current.comments = 'RITM converted to [code]' + spIncLink + '[/code]. Please use this reference from now on.';
current.work_notes = 'Link to [code]' + platformIncLink + '[/code] from within ServiceNow.';
 
/* Close RITM as "Closed Incomplete"
-----------------------------------------------------------------------------------------------------------------------*/
current.setValue('state', '4');
current.setValue('stage', 'Request Cancelled');
current.setValue('active', 'false');
 
var mySysID = current.update();
 
gs.addInfoMessage(gs.getMessage("Incident {0} created", incNum));
 
action.setRedirectURL(inc);
action.setReturnURL(current);
 
/* Link builders
-----------------------------------------------------------------------------------------------------------------------*/
function spLink(table,sysID,num){
 
var link = '<a title="Service Portal Link" href="?id=ticket&table=' + table + '&sys_id=' + sysID + '">' + num + '</a>';
 
return link;
}
 
function platformLink(table,sysID,num){
 
var link = '<a title="Platform Link" href="' + table + '.do?sys_id=' + sysID + '">' + num + '</a>';
 
return link;
}
 
/* Locate the last assignment group
-----------------------------------------------------------------------------------------------------------------------*/
function getAssignmentGroup(ritmSysID) {
/* Return all active tasks relating to RITM ordered by newest task first i.e. last created ticket. */
 
var tskGrp = new GlideRecord('sc_task');
tskGrp.addActiveQuery();
tskGrp.addQuery('request_item', ritmSysID);
tskGrp.orderByDesc('number');
tskGrp.orderByDesc('sys_created_on');
tskGrp.query();
 
var assignmentGroup = '';
 
if(tskGrp.next()) {
 
//set the assignment group to the last generated active task's assignment group
assignmentGroup = tskGrp.getValue('assignment_group');
}
else {
/* Default to the Service Desk if a assignment group cannot be found */
//NOTE: This is a custom system property, containing the group sys_id for your service desk
//You will need to create it in sys_poperties if you want to fall back to your service desk
 
assignmentGroup = gs.getProperty('sc.service_desk');  
}
return assignmentGroup;
}

 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Gemma4 

You can take help from OOTB UI Actions. and or use the scripts. 

 

Like create Change from Incident. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************