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