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.

Copying Over Attachments in Inbound Action when creating Catalog Item

Nic Omaha
Tera Guru

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";
5 REPLIES 5

Neil P
Tera Contributor

I am doing something similar, this code I found elsewhere here (sorry, can't credit author, no idea who it was anymore) seems to work.  I think it's down to the added function to update the (current) RITM:

 

 

createRequest();

function createRequest() {

	var cart = new Cart();   //calling the cart API
	var item = cart.addItem('XXXXXXXXXXXXXXXXXXXXXXXX');   //sys_id of the catalog item I want to fire

	cart.setVariable(item, 'xxxx_email_subject', email.subject); //sets catalog variable to the email's subject
	cart.setVariable(item, 'xxxx_email_body', email.body_text);   //sets catalog variable to email's body

	var rc = cart.placeOrder();   //this launches the catalog item, and creates a request object.   rc = the request object

	updateRITM(rc.sys_id);   //call a function immediately to update the ritm.   This must be a nested function, otherwise inbound actions get weird.
	//also, we're passing the sys_id of the request so we know what RITM to grab.

}

function updateRITM(req){

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', req);   //req is what we passed from the previous function. the sys_id of the request.
ritm.query();

	while (ritm.next()){
		ritm.description = email.body_text;   //we're still in the inbound action so why not exploit?
		ritm.short_description = email.subject;
		GlideSysAttachment.copy('sys_email',sys_email.sys_id,'sc_req_item',ritm.sys_id); // Should copy attachments
		ritm.update();
	}
}

event.state="stop_processing";