Attachment is not getting copy from email and post in request record with sn_sc.CartJS() API

Pritam Khude
Tera Contributor

Hello, 

I have an Requirement : When an Email is received in ServiceNow on Support@servicenow.com , We need to create a sc_request record like order a (XYZ )catalog item, and, imp=If Any attachment is attached in the email we need to copy the attached attachment and  post in the sc_request record which is created by inbound email action. when catalog item is ordered. 

Catalog item getting ordered Successfully with appropriate catalog item variables value, 

What i did=

1. I created Inbound action with Type "New",

Conditions: Reciepients is "servicedesk@servicenow.com"

In Action I wrote this script 

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
 
    var emailSysId = sys_email.getUniqueValue();
    var bodyText= email.body_text;

    gs.log("EMAIL IS  " + emailSysId);

    var cart = new sn_sc.CartJS();
    gs.log("Line 3" + cart + ' ' + typeof cart);

    var user = new GlideRecord('sys_user');
    user.addQuery('email', email.origemail);
    user.query();
    var user_sys_id;
    if (user.next()) {
        user_sys_id = user.sys_id;
    } else {
        user_sys_id = '5136503cc611227c0183e96598c4f706';
    }
    var item = {
        'sysparm_id': '0bceb9e14726311063cc6c6b416d438a',
        'variables': {
            'who_is_this_request_for': '' + user_sys_id,
            'what_is_your_contact_number': email.subject.toString(),
            'what_do_you_need_help_with': email.body_html
        }


    };
   

    cart.addToCart(item); // The addToCart function returns a JSON object reflecting the status of the cart  var cartVals =
    var checkoutVals = cart.checkoutCart();
   // gs.log("Checkout values: " + JSON.stringify(checkoutVals) + typeof checkoutVals);

    var jasonString = JSON.stringify(checkoutVals);
    var jsonVals = JSON.parse(jasonString);
    var req_sys_id = jsonVals.request_id;

    var request= new GlideRecord('sc_request');
    request.addQuery('sys_id',req_sys_id);
    request.query();
    if(request.next()){
        request.description=email.body_text;
        request.update();
    }
// var attachmentRecord= new GlideRecord('sys_attachment');
// attachmentRecord.addQuery('table_name','sys_email');
//  attachmentRecord.addQuery('table_sys_id', emailSysId);
//  attachmentRecord.query();

// var attachment = new GlideSysAttachment();

// while (attachmentRecord.next()) {
//   var attachmentSysId = attachmentRecord.sys_id;

// attachment.copy('sys_attachment', attachmentSysId, 'sc_request', req_sys_id );
//  gs.info('Copied attachment sys_id: ' + attachmentSysId + ' to sc_request ' + req_sys_id);
// }

    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('request', req_sys_id);
    ritm.query();
    if (ritm.next()) {
        ritm.description = bodyText;
    //  ritm.work_notes = "reply from: " + email.origemail + "\n\n" + bodyText;

        ritm.update();
    }


})(current, event, email, logger, classifier);
 
I tried with=
For attachment I was separate Query on attachmenttable to copy the attachment and post in request record however it was not worked and i was also used  copy.attachment method with GlideAttachment API however the attachment not getting copied.
Could you please help me to capture attachment which added in email and post in sc_request or RITM record when catalog item is ordered by using inbound action. 
2 REPLIES 2

Pritam Khude
Tera Contributor

@Tony Chatfield1 

@Ankur Bawiskar 

@Pedro Grilo1 

@gyedwab 

@Uncle Rob 

Could you please see my question.

thank you.

Just an FYI, its not considered good community etiquette to hail people to threads.
There are thousands of active minds on the community.  If you're not getting traction here  you could also try the Reddit, SNDevs slack channel, or ServiceNow Developer Discord.