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

jirehcabrales
Tera Contributor

Hi Brian, can you help me out with my script, it is just only generating the sa.write function and copying the actual attachment itself. Here is the whole script 

createRequest();

function createRequest() {

    current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
    current.short_description = email.subject;

    var cart = new Cart();

    var item = cart.addItem('d912ff6d87f5b110daf7fc08cebb3567');

    cart.setVariable(item, 'subject', email.subject);  

    cart.setVariable(item, 'emailbody', email.body_html);

    var rc = cart.placeOrder();

    updateRITM(rc.sys_id);
}

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

    ritm.query();

    while (ritm.next()) {

        ritm.contact_type = "email";
        ritm.requested_for = gs.getUserID();
        ritm.description = email.body_text;
        ritm.short_description = email.subject;
        sa.write(ritm, email.subject, strEmailContent);
        GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'sc_req_item', ritm.sys_id);


        if (sys_email.hasAttachments()) {
            var att = new GlideRecord('sys_attachment');
            att.query();

            while (att.next()) {
                att.table_name = 'sc_req_item';
                att.update();
                sys_email.target_table = 'sc_req_item';
                sys_email.instance = ritm.sys_id;
            }
        }

        ritm.update();

    }
}



 

createRequest();
 
function createRequest() {
 
    current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
    current.short_description = email.subject;
 
    var cart = new Cart();
 
    var item = cart.addItem('d912ff6d87f5b110daf7fc08cebb3567');
 
    cart.setVariable(item, 'subject', email.subject);  
 
    cart.setVariable(item, 'emailbody', email.body_html);
 
    var rc = cart.placeOrder();
 

 

    updateRITM(rc.sys_id);

 

}
 
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);
 
    ritm.query();
 
    while (ritm.next()) {
 
        ritm.contact_type = "email";
        ritm.requested_for = gs.getUserID();
        ritm.description = email.body_text;
        ritm.short_description = email.subject;
        sa.write(ritm, email.subject, strEmailContent);

 

        GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'sc_req_item', ritm.sys_id);


        if (sys_email.hasAttachments()) {

 

            var att = new GlideRecord('sys_attachment');
            att.query();
 
            while (att.next()) {
                att.table_name = 'sc_req_item';
                att.update();
                sys_email.target_table = 'sc_req_item';
                sys_email.instance = ritm.sys_id;
            }
        }
 
        ritm.update();
 

 

 

    }
}