copying fields when converting incident to ritm (requested irm)

ofekg
Tera Contributor

I am converting incident to a Ritm using a ui action button,
i neeed to copy some field fron the incident to the ritm.
everythomg is copied fine except the caller_id  to the requested for from the incident to the ritm.
the requested for field stays empty and all the rest is coppied well.
any help to fix his ?

/* Construct Incident Links
-----------------------------------------------------------------------------------------------------------------------*/
var incTable = current.getTableName();
var incSysID = current.getUniqueValue();
var incNum = current.getValue('number');

// SERVICE PORTAL: Construct a link to the original Incident for inclusion in the comments of the RITM
var spIncLink = spLink(incTable, incSysID, incNum);

// PLATFORM: Construct a link to the parent Incident for navigation from the newly created RITM
var platformIncLink = platformLink(incTable, incSysID, incNum);

/* Create Requested Item (RITM)
-----------------------------------------------------------------------------------------------------------------------*/
var catalogItemSysID = '<INSERT_CATALOG_ITEM_SYS_ID>'; // Replace with the sys_id of the relevant catalog item

if (!catalogItemSysID) {
    gs.addErrorMessage('Catalog item sys_id is missing. Please update the script with the correct sys_id.');
    action.setReturnURL(current);
   // return;
}

var ritm = new GlideRecord('sc_req_item');

ritm.initialize();
ritm.cat_item = catalogItemSysID;

// Map Incident fields to RITM fields
ritm.requested_for = current.caller_id;
ritm.short_description = current.short_description; // Incident Short Description → RITM Short Description
ritm.description = current.description; // Incident Description → RITM Description
ritm.assignment_group = current.assignment_group; // Incident Assignment Group → RITM Assignment Group
ritm.assigned_to = current.assigned_to; // Incident Assigned To → RITM Assigned To
ritm.priority = current.priority || 4; // Default Priority to Low
ritm.state = 1; // Set State to Open
ritm.u_task_type = 'catalog task';

// Map custom fields/variables to RITM fields
ritm.variables.please_describe_how_can_we_help_you = current.description; // Incident Description to RITM variable
ritm.variables.incident_reference_number = incNum; // Store the Incident number for reference (custom variable)

// Insert the RITM record
var ritmSysID = ritm.insert();

if (!ritmSysID) {
    gs.addErrorMessage('Failed to create the Requested Item (RITM).');
    action.setReturnURL(current);
   // return;
}

var ritmTable = ritm.getTableName();
var ritmNum = ritm.getValue('number');

/* Copy attachments from Incident to RITM
-----------------------------------------------------------------------------------------------------------------------*/
GlideSysAttachment.copy(incTable, incSysID, ritmTable, ritmSysID);

/* Update Comments and Work Notes
-----------------------------------------------------------------------------------------------------------------------*/
var comments = '[code]RITM created from ' + spIncLink + '<br><br>';
comments += '<strong>Incident Information:</strong><br>';
comments += '<table>';
comments += '<tr><td>Caller:</td><td>' + current.caller_id.getDisplayValue() + '</td></tr>';
comments += '<tr><td>Short Description:</td><td>' + current.short_description + '</td></tr>';
comments += '<tr><td>Description:</td><td>' + current.description + '</td></tr>';
comments += '<tr><td>Assignment Group:</td><td>' + current.assignment_group.getDisplayValue() + '</td></tr>';
comments += '<tr><td>Assigned To:</td><td>' + current.assigned_to.getDisplayValue() + '</td></tr>';
comments += '</table>[/code]';

var workNotes = 'Link to [code]' + platformIncLink + '[/code] from within ServiceNow.';

ritm.comments = comments;
ritm.work_notes = workNotes;
ritm.update();

/* Update Incident
-----------------------------------------------------------------------------------------------------------------------*/
current.comments = 'Incident converted to [code]' + platformIncLink + '[/code]. Please use this reference from now on.';
current.work_notes = 'Link to [code]' + spIncLink + '[/code] from within ServiceNow.';

/* Close Incident
-----------------------------------------------------------------------------------------------------------------------*/
current.state = 6; // Resolved
current.close_code = 'Converted';
current.close_notes = 'This incident has been converted to a Requested Item (RITM).';
current.update();

/* Add Info Message
-----------------------------------------------------------------------------------------------------------------------*/
gs.addInfoMessage('Requested Item (RITM) ' + ritmNum + ' has been successfully created from Incident ' + incNum + '.');

/* Redirect to RITM
-----------------------------------------------------------------------------------------------------------------------*/
action.setRedirectURL('/sc_req_item.do?sys_id=' + ritmSysID);

/* Link Builders
-----------------------------------------------------------------------------------------------------------------------*/
function spLink(table, sysID, num) {
    return '<a title="Service Portal Link" href="?id=ticket&table=' + table + '&sys_id=' + sysID + '">' + num + '</a>';
}

function platformLink(table, sysID, num) {
    return '<a title="Platform Link" href="' + table + '.do?sys_id=' + sysID + '">' + num + '</a>';
}
4 REPLIES 4

fathahhkf
Tera Contributor

Hello,

 

You should not create the record directly in Requested item table instead you should add it in cart. Please use the below template to create the SR,

 

var id = GlideGuid.generate(null); // Generate a new GUID
var cart = new Cart(id); // Instantiate a fresh cart using the random GUID from earlier
var myRitm = cart.addItem('d1399df1874ab9105eb4620d0ebb3517');  // The sys_id of the catalog item to submit
cart.setVariable(myRitm ,'<variable name>', '<variable value>');  
//Add the remaining variable like this. 
var request = cart.placeOrder();
var sysID=request.sys_id;
 var sr = new GlideRecord("sc_req_item");
 sr.addQuery("request",sysID);
 sr.query();
 if(sr.next()){
sr.<field name>= <field value>;
sr.update();
 }

 

If this response was helpful, please consider marking it as 'Helpful' or 'Accept as Solution.'

Your feedback not only supports me, but also helps others in the community who may have similar questions!

If this response was helpful, please consider marking it as 'Helpful' or 'Accept as Solution.'
Your feedback not only supports me, but also helps others in the community who may have similar questions!

Regards,
Abdul Fathah

Anand Kumar P
Giga Patron
Giga Patron

Hi @ofekg ,

 

Try below script this is issue with the caller sys_id , put logs current.caller_id and check.

 

ritm.requested_for = current.caller_id.toString();

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

sadly, didnt work for me, maybe you got different idea?

Juhi Poddar
Kilo Patron

Hello @ofekg 

I have reviewed the script for creating an RITM from an Incident, and it seems to be working correctly. I tested the provided code in my Personal Developer Instance (PDI), and the requested_for field is successfully populated with the value from caller_id.

The relevant code snippet is as follows:

 

var ritm = new GlideRecord('sc_req_item');
ritm.initialize();
ritm.cat_item = catalogItemSysID;

// Map Incident fields to RITM fields
ritm.requested_for = current.caller_id;
ritm.short_description = current.short_description; // Incident Short Description → RITM Short Description
ritm.description = current.description; // Incident Description → RITM Description
ritm.assignment_group = current.assignment_group; // Incident Assignment Group → RITM Assignment Group
ritm.assigned_to = current.assigned_to; // Incident Assigned To → RITM Assigned To
ritm.priority = current.priority || 4; // Default Priority to Low
ritm.state = 1; // Set State to Open
ritm.u_task_type = 'catalog task';

// Map custom fields/variables to RITM fields
ritm.variables.please_describe_how_can_we_help_you = current.description; // Incident Description to RITM variable
ritm.variables.incident_reference_number = incNum; // Store the Incident number for reference (custom variable)

// Insert the RITM record
var ritmSysID = ritm.insert();

 

The script appears functional, so there may be another factor in your instance preventing the requested_for field from being populated. You might want to check the following:

  1. Ensure that the caller_id field in the Incident record is populated with a valid sys_user reference.
  2. Verify if there are any business rules, scripts, or access controls in your instance affecting the RITM creation process.
  3. Check if the catalog item associated with the RITM has specific conditions or constraints impacting field population.

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar