email attchments to incident

shivaadapa
Tera Expert

Hi 

I am trying to attach files through email below code attachments are not working provide any alternate one.
"

current = new GlideRecord('incident');
            current.initialize();
            current.caller_id = email.from_sys_id;
            current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
            current.short_description = email.subject;

            current.category = "inquiry";
            current.incident_state = 1;
            current.notify = 2;
            current.contact_type = "email";
             current.u_most_recent_caller_mail = email.body_html;

           var emailAsAttachment = new global.emailAsAttachmentUtil();
            emailAsAttachment.createAttachment(email, current);

"

4 REPLIES 4

Anirudh Pathak
Mega Sage

Hi @shivaadapa,

You can use "GlideSysAttachment" in order to copy the attachments -

Here's a sample code -

 

GlideSysAttachment.copy(sourceTable, sourceSysID,targetTable,targetSysID);

 

 Reference - https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server_legacy/GlideSysAttachmentGl...

Amit Pandey
Kilo Sage

Hi @shivaadapa 

 

Can you try this-

 

var incidentGR = new GlideRecord('incident');
incidentGR.initialize();
incidentGR.caller_id = email.from_sys_id;
incidentGR.comments = "Received from: " + email.origemail + "\n\n" + email.body_text;
incidentGR.short_description = email.subject;
incidentGR.category = "inquiry";
incidentGR.incident_state = 1;
incidentGR.notify = 2;
incidentGR.contact_type = "email";
incidentGR.u_most_recent_caller_mail = email.body_html;
var incidentSysId = incidentGR.insert();


if (incidentSysId) {
    // Copy attachments from email to the incident record
    var attachmentCopied = GlideSysAttachment.copy('sys_email', email.sys_id, 'incident', incidentSysId);
    if (attachmentCopied) {
        gs.info('Attachments copied successfully.');
    } else {
        gs.error('Failed to copy attachments.');
    }
} 

Hi @Amit Pandey 

incident is creating but attachments are not adding to the new incdient record. attachments are attaching to the email logs.

Hi @shivaadapa 

 

Can you help me with the error, you're getting?

 

Regards,

Amit