Copying Over Attachments in Inbound Action when creating Catalog Item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2023 06:34 PM
I have an inbound action that is making catalog items as it should. I need to also copy over any included attachments and associate them with the created catalog item. What I am finding is that the script is creating a blank catalog item and associating the attachments with that request and then creating a separate catalog item. For example when the script below triggered RITM0412277 was created blank with the attachments associated and then the correct catalog request RITM0412278 was created. What might I need to change in my script below?
createRequest();
function createRequest() {
// Get the Supervisor
var mgr = '';
var gr = new GlideRecord('sys_user');
if (gr.get('employee_number', email.body.buyer_employee_id)) {
mgr = gr.sys_id;
}
var cart = new Cart();
// Add item in cart
var item = cart.addItem('d7d7a9b00fc13640da42d23be1050ecb'); //Furniture Cost Change sys_id
// Set variables
cart.setVariable(item, 'mpc_vendor_code', email.body.vendor_code);
cart.setVariable(item, 'mpc_vendor_name', email.body.vendor_name);
cart.setVariable(item, 'mpc_vendor_email', email.body.vendor_email);
cart.setVariable(item, 'mpc_rep_name', email.body.rep_name);
cart.setVariable(item, 'mpc_buyer_id', email.body.buyer_employee_id);
cart.setVariable(item, 'mpc_date_of_change', email.body.requested_date);
cart.setVariable(item, 'mpc_last_price_increase', email.body.date_of_last_increase);
cart.setVariable(item, 'mpc_comments', email.body.comments);
cart.setVariable(item, 'mpc_buyer', email.body.buyer_employee_id);
cart.setVariable(item, 'mpc_mgr', mgr);
// Place the order
var rc = cart.placeOrder();
// Associate attachments
var sysEmail = new GlideRecord('sys_email');
sysEmail.addQuery('sys_id', current.email.sys_id);
sysEmail.query();
while (sysEmail.next()) {
var att = new GlideSysAttachment();
att.copy('sys_email', sysEmail.sys_id, 'sc_req_item', rc.sys_id.toString());
}
// Update the current record
current.update();
event.state = "stop_processing";
}
Here is the working script with out doing anything with the attachments:
createRequest();
function createRequest() {
// Get the Supervisor
var mgr = '';
var gr = new GlideRecord('sys_user');
if (gr.get('employee_number', email.body.buyer_employee_id)) {
mgr = gr.sys_id;
}
var cart = new Cart();
// add in cart
var item = cart.addItem('d7d7a9b00fc13640da42d23be1050ecb'); //Furniture Cost Change sys_id
cart.setVariable(item, 'mpc_vendor_code', email.body.vendor_code);
cart.setVariable(item, 'mpc_vendor_name', email.body.vendor_name);
cart.setVariable(item, 'mpc_vendor_email', email.body.vendor_email);
cart.setVariable(item, 'mpc_rep_name', email.body.rep_name);
cart.setVariable(item, 'mpc_buyer_id', email.body.buyer_employee_id);
cart.setVariable(item, 'mpc_date_of_change', email.body.requested_date);
cart.setVariable(item, 'mpc_last_price_increase', email.body.date_of_last_increase);
cart.setVariable(item, 'mpc_comments', email.body.comments);
cart.setVariable(item, 'mpc_buyer, mgr');
var rc = cart.placeOrder();
}
var ritmSysID = "";
var ritmRec = new GlideRecord("sc_req_item");
ritmRec.addQuery("request", rc.sys_id);
ritmRec.query();
if(ritmRec.next()){
ritmSysID = ritmRec.sys_id;
}
current.update();
event.state="stop_processing";

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2023 07:27 PM
@Nic Omaha Please update the code as follows and see if it works for you.
createRequest();
function createRequest() {
// Get the Supervisor
var mgr = '';
var gr = new GlideRecord('sys_user');
if (gr.get('employee_number', email.body.buyer_employee_id)) {
mgr = gr.sys_id;
}
var cart = new Cart();
// Add item in cart
var item = cart.addItem('d7d7a9b00fc13640da42d23be1050ecb'); //Furniture Cost Change sys_id
// Set variables
cart.setVariable(item, 'mpc_vendor_code', email.body.vendor_code);
cart.setVariable(item, 'mpc_vendor_name', email.body.vendor_name);
cart.setVariable(item, 'mpc_vendor_email', email.body.vendor_email);
cart.setVariable(item, 'mpc_rep_name', email.body.rep_name);
cart.setVariable(item, 'mpc_buyer_id', email.body.buyer_employee_id);
cart.setVariable(item, 'mpc_date_of_change', email.body.requested_date);
cart.setVariable(item, 'mpc_last_price_increase', email.body.date_of_last_increase);
cart.setVariable(item, 'mpc_comments', email.body.comments);
cart.setVariable(item, 'mpc_buyer', email.body.buyer_employee_id);
cart.setVariable(item, 'mpc_mgr', mgr);
// Place the order
var rc = cart.placeOrder(); //Returns the Request Glide Record
var ritmSysID = "";
var ritmRec = new GlideRecord("sc_req_item");
ritmRec.addQuery("request", rc.sys_id);
ritmRec.query();
if (ritmRec.next()) {
ritmSysID = ritmRec.sys_id;
}
// Associate attachments
var sysEmail = new GlideRecord('sys_email');
sysEmail.addQuery('sys_id', current.email.sys_id);
sysEmail.query();
while (sysEmail.next()) {
var att = new GlideSysAttachment();
att.copy('sys_email', sysEmail.sys_id, 'sc_req_item', ritmSysID + '');
}
// Update the current record
current.update();
event.state = "stop_processing";
}
Also, please be informed that ServiceNow has deprecated Cart API use the scoped version CartJS instead as the script include associated with Cart API may get deactivated soon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2023 06:46 AM
Thank you very much for the suggestion It is much appreciated. This code does the same thing.
RITM0412287 was created blank with attachments and then RITM0412288 was created with the catalog request.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2023 07:30 PM
HI @Nic Omaha ,
I trust you are doing great.
Please find the updated code.
createRequest();
function createRequest() {
// Existing code to get supervisor and set up cart...
// Place the order
var request = cart.placeOrder();
// Retrieve the RITM sys_id
var ritmSysID = '';
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.addQuery('request', request.sys_id);
ritmRec.query();
if (ritmRec.next()) {
ritmSysID = ritmRec.sys_id;
}
// Check if RITM sys_id is retrieved successfully
if (ritmSysID) {
// Associate attachments with the RITM
var sysEmail = new GlideRecord('sys_email');
sysEmail.addQuery('sys_id', current.email.sys_id);
sysEmail.query();
while (sysEmail.next()) {
var att = new GlideSysAttachment();
att.copy('sys_email', sysEmail.sys_id, 'sc_req_item', ritmSysID);
}
} else {
// Handle the case where RITM sys_id is not found
// Log an error or take appropriate action
}
// Update the current record
current.update();
event.state = "stop_processing";
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2023 06:53 AM
Thank you so much for the suggestion. I attempted to use this code as provided and nothing was associated with the attachment created on sys_attachment.