variables not populating on RITM
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 07:46 AM
Script Include:
*************************************************************************************
var RequestUtil = Class.create();
RequestUtil.prototype = {
initialize: function() {},
createRequestFromIncident: function(incidentId, catalogItemId) {
var incident = new GlideRecord('incident');
if (!incident.get(incidentId)) {
gs.error('Invalid incident ID: ' + incidentId);
return null;
}
var request = new GlideRecord('sc_request');
request.initialize();
request.requested_for = gs.getUserID();
request.requested_by = gs.getUserID();
request.short_description = 'Request from Incident ' + incident.number;
request.source = 'incident';
var requestId = request.insert();
if (requestId) {
var ritm = new GlideRecord('sc_req_item');
ritm.initialize();
ritm.request = requestId;
ritm.cat_item = catalogItemId;
ritm.short_description = 'Request Item from Incident ' + incident.number;
var ritmId = ritm.insert();
if (ritmId) {
// Add variable values
this.addVariableToRITM(ritmId, catalogItemId, 'request_summary', incident.short_description);
this.addVariableToRITM(ritmId, catalogItemId, 'request_description', incident.description);
this.addVariableToRITM(ritmId, catalogItemId, 'request_urgency', incident.urgency);
gs.info('Variables populated successfully.');
return requestId;
}
}
return null;
},
// Helper function to add variable to RITM
addVariableToRITM: function(ritmId, catalogItemId, variableName, value) {
var itemOption = new GlideRecord('item_option_new');
itemOption.addQuery('cat_item', catalogItemId);
itemOption.addQuery('name', variableName);
itemOption.query();
if (itemOption.next()) {
var itemVariable = new GlideRecord('sc_item_option_mtom');
itemVariable.initialize();
itemVariable.request_item = ritmId;
itemVariable.sc_item_option = itemOption.sys_id;
itemVariable.value = value;
itemVariable.insert();
gs.info('Variable ' + variableName + ' inserted with value: ' + value);
} else {
gs.error('Variable not found: ' + variableName);
}
},
type: 'RequestUtil'
};
********************************************************************************
Servrer Side UI Action Script:
**********************************************************
(function executeAction() {
if (current.isNewRecord()) {
gs.addErrorMessage('Save the incident before converting.');
return;
}
var catalogItemId = 'sys_id of the catalog item';
var requestUtil = new global.RequestUtil();
var requestId = requestUtil.createRequestFromIncident(current.sys_id, catalogItemId);
if (requestId) {
gs.addInfoMessage('Request created: ' + requestId);
action.setRedirectURL('/sc_request.do?sys_id=' + requestId);
} else {
gs.addErrorMessage('Failed to create request.');
}
})();
*******************************************************************************************************
The catalog item and the script include in Global Scope, but the UI Action Script is in Service Operation Workspace application Scope.
By using the above script include and server-side UI Action script I am able to create a request and RITM from incident. Those 3 variables 'request_summary', request_description' and 'request_urgency' values are showing in the logs but these 3 variables are not even populating on the RITM while creating the RITM from Incident.
Can anyone please help me out ASAP as it is very urgent.
Thanks&Regards,
Abhisek Chattaraj.
2 REPLIES 2

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 08:09 AM
Hi Abhisek,
Rather than creating records directly in the request management tables ( not recommended) use the CartJS API https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/sn_sc-namespace/c_CartJS...

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 08:14 AM
For example, your script could become the following which is far more manageable
createRequestFromIncident: function(incidentId, catalogItemId) {
var incident = new GlideRecord('incident');
if (!incident.get(incidentId)) {
gs.error('Invalid incident ID: ' + incidentId);
return null;
}
var cart = new sn_sc.CartJS();
var request = {
'sysparm_id': catalogItemId,
'sysparm_quantity': '1',
'variables': {
'request_summary' : incident.getValue('short_description'),
'request_description' : incident.getValue('description'),
'request_urgency' : incident.getValue('urgency')
}
};
var cartDetails = cart.orderNow(request);
gs.info(cartDetails);
},