Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

is that possible to read the data in email body ?

kiran kumar m1
Tera Contributor

I want to read the email body from outlook  in which the data will be in table format in this manner 

 

Insured – ABC Corporation

Underwriter – Kiran MK

Broker contact –  mkmk@gmail.com

Broker – CTS solutions

 

I have created custom fields for this and the data should go into  those fields , how can I achieve this ?  can I write a script to read the index of the email body ? is that possible to read the data in email body ?

 

 

3 REPLIES 3

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @kiran kumar m1 

 

Yes, email body can be parse, but few things important that the email body must have a standard structure for all, else script will fail.

 

 

https://www.servicenow.com/community/itsm-forum/how-do-you-parse-the-body-of-an-email/td-p/884923

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Jaspal Singh
Mega Patron
Mega Patron

Hi Kiran,

You say it is tablar format but has - used as name value pair. To ease suggestion will be to check if - can be replaced with :

So,

Insured : ABC Corporation

Underwriter : Kiran MK

Broker contact : mkmk@gmail.com

Broker : CTS solutions

will be the content and can be parsed by name logic pair as below

 

current.field_name1=email.body.insured; (this will give value for insured)

current.field_name2=email.body.underwriter; (this gives value for underwriter)

 

In case it is not possible to replace - with : you can use split method to get the value by parsing it.

 

Amitoj Wadhera
Kilo Sage

Hello @kiran kumar m1 ,

 

You have to write an Inbound Email Action which will have the below script:

var emailBody = email.body.toString();
var lines = emailBody.split('\n');
var extractedData = {
    insured: '',
    underwriter: '',
    brokerContact: '',
    broker: ''
};
for (var i = 0; i < lines.length; i++) {
    var line = lines[i].trim();
    if (line.startsWith('Insured –')) {
        extractedData.insured = line.split('–')[1].trim();
    } else if (line.startsWith('Underwriter –')) {
        extractedData.underwriter = line.split('–')[1].trim();
    } else if (line.startsWith('Broker contact –')) {
        extractedData.brokerContact = line.split('–')[1].trim();
    } else if (line.startsWith('Broker –')) {
        extractedData.broker = line.split('–')[1].trim();
    }
}
var newRecord = new GlideRecord('target_table');
newRecord.initialize();
newRecord.setValue('insured', extractedData.insured);
newRecord.setValue('underwriter', extractedData.underwriter);
newRecord.setValue('broker_contact', extractedData.brokerContact);
newRecord.setValue('broker', extractedData.broker);
newRecord.insert();

gs.info('Extracted data: ' + JSON.stringify(extractedData));

This works perfect in my PDI.

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Regards,

Amitoj Wadhera