Pull requested for details from inbound email to create request

Community Alums
Not applicable

Hi Experts,

We have a requirement to create a Request from an inbound email action, which is working fine with my existing code. The client wants us to populate Requested for details from the Email Body. The format of the email would look something like this:

Requested By: (User Name)

Email address: (email address)

I'm not very good at coding, so kindly help me with this requirement.

 

Existing code is as below:

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
 
    var cartId = GlideGuid.generate(null);
    var cart = new Cart(cartId);

        var item = cart.addItem('sys_ID_of_catalog_item');
        cart.setVariable(item, 'short_description', email.subject.toString());
        cart.setVariable(item, 'description', email.body_text);
        cart.setVariable(item, 'assg_group', 'sys_id');
       
        var rc = cart.placeOrder();

        var ritmRec = new GlideRecord("sc_req_item");
        ritmRec.addQuery("request", rc.sys_id);
        ritmRec.query();
        if (ritmRec.next()) {
            ritmRec.short_description = email.subject.toString();
            ritmRec.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
            ritmRec.contact_type = "email";
            ritmRec.update();
        }
       // update target in email table, as it won't be updated as the record was inserted through cart API
        if (rc != '') {
            var email_rec = new GlideRecord('sys_email');
            email_rec.get(sys_email.sys_id);
            email_rec.instance = rc.sys_id;
            email_rec.target_table = "sc_request";
            email_rec.update();
        }

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

Thanks in advance.

11 REPLIES 11

Hi @Sagar Garige ,

 


Thank you for marking my solution as helpful! If my answer addressed your query, feel free to accept it to help others in the community benefit as well.

@Runjay Patel  The above code is working for ServiceNow users. It is not working for external users for eg: I have sent an email from my gmail and that didn't create a request. Any suggestions what else I need to do?

Hi @Sagar Garige ,

it will work for all user, is your instance receiving the email outside of your organisation?

@Runjay Patel  Yes my instance is receiving emails from outside of the organization. See below screenshot email has received but target is still empty.

SagarGarige_0-1736420063672.png

 

Hi @Sagar Garige ,

 

Check your inbound when to run configuration or share the screenshot.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------