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

SanjivMeher
Kilo Patron
Kilo Patron

I think the easier way would be to use a Flow Designer. Using Inbound Email flow, you can receive the email and then parse it. And then there is an out of box action called Submit Catalog Item Request, which can be used to create the catalog item.

 

If you want to use cartAPI, i have used Option 1 script provided in below blog, which has always worked for me.

https://davidmac.pro/posts/2022-10-10-sn-script-new-request/

 


Please mark this response as correct or helpful if it assisted you with your question.

Hi @SanjivMeher ,

I am new to CART JS.
So from option one, I can directly use the script in the inbound action? Will it automate the ask of RITM creation when an email is received or?

var cart = new sn_sc.CartJS();var newItem = cart.addToCart({	"sysparm_id": "SYS_ID_OF_CAT_ITEM",	"sysparm_quantity": "1",	"sysparm_requested_for": "SYS_ID_OF_REQUESTED_FOR_USER",	"variables":{		"acrobat": "true",		"photoshop": "false",		"Additional_software_requirements": ""	}});var checkoutInfo = cart.checkoutCart();

 How can I put dynamic values in the above script? 

Amit Verma
Kilo Patron
Kilo Patron

Hi @swapnil15 

 

The easiest way out could be using a flow to drive this. Please create a flow with below approach and give this a shot :

 

1. Create a flow with trigger as Inbound Email. Add filter conditions to your flow like Subject is "Something Specific" and/Or body contains "Some text" so that your flow only work on particular emails.

 

2. Inside the flow, make use of an action with the Script you already have to extract the Catalog Variable Values.

 

3. Post this action, make use of OOB action, Submit Catalog Item Request where you can pass in the variable values from your above action as data pills.

 

When the flow will run, it will extract the variable values from the email body and as you have mapped these variables to Submit Catalog Item Request action, you should be able to submit the catalog item and eventually get a RITM created. Please give this a try and let me know in case of any issues.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Hi @Amit Verma ,

I created a flow as below:

swapnil15_0-1709030245515.png



Where to put the script in the action?