Create RITM through inbound action by parsing mail body

swapnil15
Tera Contributor

Hi All,

I have an ask to create a RITM whenever an email is received in ServiceNow and the data needs to be parsed, and pushed to the variables in the catalog that creates the RITM.

Below is how the mail body is received in SNOW:

 

Dear Service Desk,

Our HR system has indicated that Simon Beggs is due to leave the company on 16-02-2024.

AD Login - op-simonb1
Email Address - Simon.Beggs@norbrook.co.uk
Location - Norbrook Laboratories Ltd
Area - Armagh Road
Thanks,
IT Services

 

 

Till now, I have used regex to parse the data into the fields on RITM. The inbound script looks as below:

 

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

    // Implement email action here
    var text = email.body_text;
    gs.log('text is: ' + text);
    // Define regular expressions to match the patterns
    var adLoginPattern = /AD Login - (.+)/;
    var emailPattern = /Email Address - (.+)/;
    var locationPattern = /Location - (.+)/;
    var areaPattern = /Area - (.+)/;
    var namePattern = /indicated that (.+) is due/;
    var DepartDatePattern = /([A-Za-z0-9]+(-[A-Za-z0-9]+)+)/i;

    // Function to extract values using regular expressions
    function extractValue(text, pattern) {
        var match = text.match(pattern);
        gs.log('match is: ' + match);
        if (match) {
            // Remove asterisks from the captured group (value) if found
            var value = match[1].replace(/^\*|\*$/g, ''); // This regex removes asterisks from start and end of the string
            return value.trim(); // Trim any extra whitespace
        } else {
            return null; // Return null if not found
        }
    }

    // Extract values using regular expressions
    var adLogin = extractValue(text, adLoginPattern);
    var email = extractValue(text, emailPattern);
    var location = extractValue(text, locationPattern);
    var area = extractValue(text, areaPattern);
    var name = extractValue(text, namePattern);
    var depdate = extractValue(text, DepartDatePattern);

    gs.log('ad login :' + adLogin);
    gs.log('email is: ' + email);
    gs.log('location is: ' + location);
    gs.log('area is :' + area);
    gs.log('name is :' + name);
    gs.log('depart date: ' + depdate);

    current.u_adlogin = adLogin;
    current.u_mailid = email;
    current.u_leaver_location = location;
    current.u_area = area;
    current.u_ad_login = name;
    current.u_departure_date = depdate;
    // current.u_departure_date.setDisplayValue(depdate);
    current.insert();

    // Now set catalog variables with parsed values
    var ritmVariables = current.variables;
    if (ritmVariables) {
        ritmVariables.staff_member_name = name;
        ritmVariables.date_of_departure = depdate;
        ritmVariables.email_address = email;
        ritmVariables.ad_login = adLogin;
        ritmVariables.location = location;
        ritmVariables.area = area;
        ritmVariables.update();
    }
})(current, event, email, logger, classifier);

 

- I am able to parse the data from mail body and push it in the fields as below in the newly created RITM.

swapnil15_0-1708982853725.png

 

Ask is - instead of the above fields (ad login, location, mail, area, departure date), the parsed data from mail needs to be pushed into the variables of a catalog item and then create an RITM from that catalog automatically. Or create an RITM and push the data into the variable, either way.

Below is the catalog item with variables: 

swapnil15_1-1708983043321.png

 

So basically - I need an automated flow where, whenever an email is received in SNOW, the mail body needs to be parsed and pushed to the catalog item as above and create an RITM automatically.

 

I got the parse data in the fields but not sure how to push that data into the variables on RITM. The catalog has a cart section in the RITM where the data needs to be visible, something like below:

swapnil15_2-1708983226343.png

 

Or any other path to complete the ask other than one I mentioned above is appreciated! I came across few threads on forum as this one.

But it didn't helped. The RITM was not created when I use the script from the mentioned thread. 

I am also not aware of  CART API and CARTJS on how to use it.

Can anyone help on how to achieve this?

 

 

1 ACCEPTED SOLUTION

Priyanka_786
Tera Guru
Tera Guru

@swapnil15 : This can be achieved by using flow designer.

 

1.  Set trigger as inbound email and set condition as required.

2.  There is ServiceNow flow action provided by ServiceNow on share "Parse Email body text" Attached word document for steps to download it for reference.

3.  In flow , there is provision to create flow variables -click on three dots at right upper corner- flow variables option- Using this option you can create variables to store values from email body text after parsing. (though script or dot walking should be available). For every value create flow variable which will store respective variable required on this request.

4. then use "Submit catalog item request" step- pass your catalog item and in variables pass the above each flow variables by dot walking. This will create request automatically.

Give a try. Hope it helps. Kindly mark helpful/accepted if it helps.

Regards,

Priyanka Salunke

 

View solution in original post

19 REPLIES 19

@Joel O : I am agree with you as I was also not clear from the document for reference field. I have done some research and tried from my self, but unfortunately it didn't work for me. Please go through the comments of this main blog and you can try and see if it work for you.

 

https://www.servicenow.com/community/itsm-articles/how-to-read-body-text-in-inbound-email-flow/tac-p...

 

Hope it helps.

Please mark my response as helpful/correct if it helps.

Regards,

Priyanka Salunke

Thanks for the shout out. 🙂

@Joel O : Yes it is compatible. However It didn't worked for me for reference fields as expected unfortunately. I got this from this thread- https://www.servicenow.com/community/itsm-articles/how-to-read-body-text-in-inbound-email-flow/tac-p....

 

You can ask author of the above blog if you have any further queries.

Regards,

Priyanka Salunke

 

Regards,

Priyanka Salunke

@Priyanka_786 I have ran into the issue on my PDI that the action is not returning any email variables I'm getting the following message in the logs *** Script: Cannot read property "1" from null am i missing some configuration on the action?

@aac31246 : - I got this from this blog -https://www.servicenow.com/community/itsm-articles/how-to-read-body-text-in-inbound-email-flow/tac-p....

 

Please check with author of above blog for any further queries.

Regards,

Priyanka Salumke