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

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

try this

(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 requestedBy = email.body.requested_by.trim();

    var requestedForSysId = '';
    var gr = new GlideRecord("sys_user");
    gr.addQuery("user_id", requestedBy);
    gr.query();
    if (gr.next()) {
        requestedForSysId = gr.getUniqueValue();
    }


    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.requested_for = requestedForSysId;
        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);

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

@Community Alums 

Hope you are doing good.

Did my reply answer your question?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

Runjay Patel
Giga Sage

Hi @Community Alums ,

 

You can use Revised code.

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

    // Extracting 'Requested By' and 'Email address' from the email body
    var emailBody = email.body_text.toString();
    var requestedByMatch = emailBody.match(/Requested By:\s*(.*)/i);
    var emailAddressMatch = emailBody.match(/Email address:\s*(.*)/i);
    
    var requestedBy = requestedByMatch ? requestedByMatch[1].trim() : null;
    var emailAddress = emailAddressMatch ? emailAddressMatch[1].trim() : null;

    // Logging the extracted information for debugging
    logger.info('Requested By: ' + requestedBy);
    logger.info('Email Address: ' + emailAddress);

    // Find the user in ServiceNow based on the extracted email address
    var requestedFor = null;
    if (emailAddress) {
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('email', emailAddress);
        userGR.query();
        if (userGR.next()) {
            requestedFor = userGR.sys_id.toString();
        }
    }

    // Create a request using the cart API
    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');
    
    // If the requestedFor user was found, set it in the cart
    if (requestedFor) {
        cart.setVariable(item, 'requested_for', requestedFor);
    }

    var rc = cart.placeOrder();

    // Update the RITM with additional details
    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 the email target
    if (rc) {
        var email_rec = new GlideRecord('sys_email');
        email_rec.get(email.sys_id);
        email_rec.instance = rc.sys_id;
        email_rec.target_table = "sc_request";
        email_rec.update();
    }

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

 

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

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

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

@Runjay Patel  Your solution worked. I appreciate your help.