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.

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

Brian Lancaster
Kilo Patron

Instead of using a GlideRecord Query to copy the attachment try using GlideSysAttachment.  I would also consider putting this in the first function right before you call the update function.

Didn't work:

 

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);
GlideSysAttachment.copy('sys_email',current.sys_id,'sc_req_item',ritm);
ritm.update();
}

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);

Brian - I made that tweak and updated the current.sys_id to sys_email.sys_id and it's working now. Thank you so much!