Copy attachments to RITM record created through inbound action

prasannasun
Tera Contributor

Hi Team,

 

I have created an onbound action in sc_request table and itis creating the RITM catalog item and I have to copy the attachments records which are given in email to RITm but it is not happening. Can someone help me with this if there is anything wrong here in script or is there any way that I can use to copy the attachments which are coming through inbound actions to RITm record

 

here is the script that I have written everything is working s expected but the ttachments are not copying to RITm from emails.

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

    // Check for the sender's email and ensure it matches the specified ones
 
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);

        // Add your requested item to the cart using sys_id of the catalog item
        var item = cart.addItem('HERE ITEM SYS-ID', 1);
        cart.setVariable(item, "short_description", email.body.company + ' Request Access for ' + email.body.an_offer_has_been_extended_to_the_following_candidate);
        cart.setVariable(item, "description", email.body_text);
        cart.setVariable(item, "entity", email.body.company);
        cart.setVariable(item, "supervisor", email.body.supervisor);
        cart.setVariable(item, "phone_number", 'XXX-XXX-XXXX');
        cart.setVariable(item, "department", email.body.department);
       

        var reqTitleMatch = email.body_text.match(/Req\s*-\s*Title:\s*(.+)/i);
        var reqTitle = reqTitleMatch ? reqTitleMatch[1].trim() : "";
        cart.setVariable(item, "title", reqTitle);

        // Extract the date for 'needed_by' from the email body
        var emailBody = email.body_text;
        var dateRegex = /Preliminary Start Date:\s*([A-Za-z]+\s+\d{1,2},\s+\d{4})/;
        var match = dateRegex.exec(emailBody);

        if (match) {
            var preliminaryStartDate = match[1]; // e.g., "January 15, 2025"

            // Parse the date to "yyyy-MM-dd" format
            var dateParts = preliminaryStartDate.match(/([A-Za-z]+)\s+(\d{1,2}),\s+(\d{4})/);
            if (dateParts) {
                var monthName = dateParts[1];
                var day = dateParts[2];
                var year = dateParts[3];

                var monthMap = {
                    January: "01", February: "02", March: "03", April: "04",
                    May: "05", June: "06", July: "07", August: "08",
                    September: "09", October: "10", November: "11", December: "12"
                };

                var month = monthMap[monthName];
                var formattedDate = year + "-" + month + "-" + (day.length === 1 ? "0" + day : day);
                cart.setVariable(item, "needed_by", formattedDate);
            }
        }

        // Place the order
        var rc = cart.placeOrder();
        gs.info('Inbound email processed, order placed: ' + rc.number + ' ' + email.body_text);
       
        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.addQuery('request', rc.sys_id);
        ritmGR.query();
        while (ritmGR.next()) {
            GlideSysAttachment.copy('sys_email', email.sys_id, 'sc_req_item', ritmGR.sys_id);
            gs.info('Attachments copied from email ' + email.sys_id + ' to RITM ' + ritmGR.number);
        }
})(current, event, email, logger, classifier);
    
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@prasannasun 

update your line with this and test once

           new GlideSysAttachment().copy('sys_email', sys_email.sys_id, 'sc_req_item', ritmGR.sys_id);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

rachel33berry
Giga Contributor

Hello,

Your current script correctly uses GlideSysAttachment.copy() to move attachments from the inbound email to the newly created RITM. The code GlideSysAttachment.copy('sys_email', email.sys_id, 'sc_req_item', ritmGR.sys_id); is the proper method for this task. Since you've confirmed everything else works, the issue might be a slight timing problem where the RITM record is not fully committed to the database when the attachment copy script runs. Placing your attachment copy logic at the very end of the inbound action's script is the correct approach to ensure the RITM record is fully available.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@prasannasun 

update your line with this and test once

           new GlideSysAttachment().copy('sys_email', sys_email.sys_id, 'sc_req_item', ritmGR.sys_id);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks Ankur, It worked.

Community Alums
Not applicable

Hi @prasannasun ,

Attachments in inbound emails are stored on the sys_email record and need to be explicitly copied to the RITM. Use the supported GlideSysAttachment.copy() API as shown below:

while (ritmGR.next())
{
GlideSysAttachment.copy('sys_email', email.sys_id, 'sc_req_item', ritmGR.sys_id);
}


If attachments aren’t copying, check that:

  • The inbound action runs after attachments are saved.

  • Attachments actually exist on sys_email for the processed email.

Useful references:

 

Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.