How to copy attachment from email using inbound action

Blair5
Tera Guru

I have an inbound action that creates a service request. Currently, I have it set to attach the email to the RITM and I also want to include whatever attachments were on the email. See below for the script -- I see that it should happen by default, but that's not the case. What's missing?

createRequest();

function createRequest(){
	var cart = new Cart();  //calling the cart API
	var gu = gs.getProperty('standards.generic_user'); // sys id generic user
	var ds = gs.getProperty('standards.item');
	var item = cart.addItem(ds);  //sys_id of the catalog item I want to fire

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

	var usr = new GlideRecord('sys_user');
	usr.addQuery('email', email.from);
	usr.query();

	if(usr.next()){
		//if user is found in user table, set the requested for and opened by to that user
		cart.setVariable(item, 'requested_for', usr.sys_id);
	}else{
		cart.setVariable(item, 'requested_for', gu); //generic user
	}

	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 strEmailContent = email.content_type + "\n" + email.headers + "\n\n\n\n" + email.body_text;
	var sa = new GlideSysAttachment();

	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.setValue('requested_for', email.from);
		ritm.contact_type="email";
		sa.write(ritm, email.subject + ".eml", " application/octet-stream ", strEmailContent);

		if (sys_email.hasAttachments()){
			gs.log('has attachments');
			var att = new GlideRecord("sys_attachment");
			att.addEncodedQuery("table_name=sc_req_item^table_sys_id=" + sys_email.getValue("sys_id"));
			att.query();
			while (att.next()){
				att.table_name = "sc_req_item";
				att.table_sys_id = ritm.getValue("sys_id");
				att.update();
			}
		}
		ritm.update();
	}
}

event.state="stop_processing";  
1 ACCEPTED SOLUTION

This GlideSysAttachment.copy('sys_email',current.sys_id,'sc_req_item',ritm); needs to be GlideSysAttachment.copy('sys_email',current.sys_id,'sc_req_item',ritm.sys_id);

View solution in original post

20 REPLIES 20

I can see the email body in the catalog task activity but the attachment is not copied and the email stayed in inbox. I tried the same for incident. This is OOB right? where if the reply email has the target record it will automatically attach the email and the attachment to the target record.

Also, i am the sender of the email and I am an admin, i dont think its an access issue

find_real_file.png

can you share the code in your inbound action?

I dont have an inbound action. Shouldnt this be OOB? as it is working on incident but i have not done any inbound action for that.