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

Glad it worked.  Please mark my answer about using GlideSysAttachment as correct to close the loop on this question.

Hi @Brian Lancaster ,

 

Im trying to do the same, will this still work even if the sender of the email doesn't have a role in SN?

Yes it should.

Thanks for the reply. Im having an issue, when i send a reply even from me as admin, the email is not being processed if the target record is sc_task, but i tried multiple times for incident and it works properly. any thoughts on this?find_real_file.png

 

find_real_file.png

find_real_file.png

 

I was suspecting if this is caused by inbound actions not working properly but I already disabled all the custom ones but still email is stuck in inbox.

Is the email not being processed at all or is it just the attachments. My first thought is that users don't have access to the sctask but as administrator it should work. Can you share the code?