i want to add attachment in the ritm from inbound email action.

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

    // Implement email action here
    var cartID=GlideGuid.generate(null);
    var cart= new Cart(cartID);
    var item= cart.addItem('158edf4a2bacfe10eacaf2a6d391bf51');
    //cart.setVariable(item,'short_description','test short');
    cart.setVariable(item,' requested_for',gs.getUserID());
    cart.setVariable(item,'short_description',email.subject);
    cart.setVariable(item,'subject','This request created when the caller sended email for creating a request');
    cart.setVariable(item,'description',email.body_text);
    var rc=cart.placeOrder();

    var ritm=new GlideRecord("sc_req_item");
    ritm.addQuery('request',rc.sys_id);
    ritm.query();
    if(ritm.next())
    {
        ritm.short_description = email.subject.toString();
        ritm.contact_type="email";
        ritm.update();
    }

})(current, event, email, logger, classifier);


This is the code . Can you pls help me
1 ACCEPTED SOLUTION

@NeethuB 

So as per your information the RITM is getting created and the incoming file to email should get copied to RITM

please update code as this

var ritm=new GlideRecord("sc_req_item");
    ritm.addQuery('request',rc.sys_id);
    ritm.query();
    if(ritm.next())
    {
        ritm.short_description = email.subject.toString();
        ritm.contact_type="email";
        ritm.update();
new GlideSysAttachment().copy('sys_email', sys_email.sys_id, 'sc_req_item', ritm.sys_id);
    }

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

6 REPLIES 6

ChallaR
Giga Guru

HI @NeethuB 

i have updated few lines , can you try this code -

(function runAction(current, event, email, logger, classifier) {
    try {
        var cartID = GlideGuid.generate(null);
        var cart = new Cart(cartID);

        // Add Catalog Item
        var item = cart.addItem('158edf4a2bacfe10eacaf2a6d391bf51');

        // Set variables (ensure these exist in the catalog item)
        cart.setVariable(item, 'requested_for', gs.getUserID());
        cart.setVariable(item, 'short_description', email.subject || 'Email Request');
        cart.setVariable(item, 'subject', 'Request created from email');
        cart.setVariable(item, 'description', email.body_text || 'No description provided');

        // Place order
        var rc = cart.placeOrder();
        if (!rc) {
            logger.error('Failed to place order from email.');
            return;
        }

        // Update RITM
        var ritm = new GlideRecord('sc_req_item');
        ritm.addQuery('request', rc.sys_id);
        ritm.query();
        if (ritm.next()) {
            ritm.short_description = email.subject.toString();
            ritm.contact_type = 'email';
            ritm.update();
        }

        logger.info('Request created successfully from email: ' + rc.number);
    } catch (e) {
        logger.error('Error in email action: ' + e.message);
    }
})(current, event, email, logger, classifier);

NOTE -

 

  • Validate that the catalog item and variables exist.
  • Use gs.getUserID() carefully; if you want the caller from the email, you might need email.origemail or email.from.
  • Add logging for success and failure.
  • Consider security: only allow certain email domains to create requests.

Thanks,

Rithika.ch

 

NeethuB
Tera Contributor

Not working