what is the syntax to read the email template date

String
Kilo Sage

Hi Team ,

We are trying to fetch key values attributes from incoming email template using inbound email actions ,So we can convert into json and proceed with validation and insert the data into case record 

But we are getting undefined :

 

Below is my email template :

Category : test2

Subcategory : test2

Short_description :test

Urgency : 2 - High 

Contact : test@test.com

Contact_Work_Phone : 123

Helpdesk_Contact : test1@test.com

 

Below is my code :Inbound email action

  try{

   // Check if the email body exists and contains the expected fields

Var emailBody=email.body;

    if (emailBody) {

        var extractedData = {

             'Category': emailBody.Category,

            'Subcategory': emailBody.Subcategory,

            'Short_description': emailBody.Short_description,

            'Urgency': emailBody.Urgency,

             'Contact': emailBody.Contact,

         'Contact_Work_Phone': emailBody.Contact_Work_Phone,

            'Helpdesk_Contact': emailBody.Helpdesk_Contact

        };

 

        gs.info("Inbound mail test: " + JSON.stringify(extractedData));

    } else {

        gs.error("Email body not found or does not contain the expected fields.");

    }

    }catch (e) {

 

                gs.info('Inbound mail test:error' + e);

            }

 

Please guide me with best practices 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@String 

if the email contains key:value format then simply do this

1) convert everything to lowercase and add _ for spaces

Example below

Contact_Work_Phone -> email.body.contact_work_phone

Helpdesk Contact -> email.body.helpdesk_contact

try this

try{
	// Check if the email body exists and contains the expected fields

	var emailBody = email.body;
	if (emailBody) {
		var extractedData = {
			'Category': emailBody.category,
			'Subcategory': emailBody.subcategory,
			'Short_description': emailBody.short_description,
			'Urgency': emailBody.urgency,
			'Contact': emailBody.contact,
			'Contact_Work_Phone': emailBody.contact_work_phone,
			'Helpdesk_Contact': emailBody.helpdesk_contact
		};
		gs.info("Inbound mail test: " + JSON.stringify(extractedData));
	} else {
		gs.error("Email body not found or does not contain the expected fields.");
	}
}catch (e) {
	gs.info('Inbound mail test:error' + e);
}

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

View solution in original post

5 REPLIES 5

Kartik Magadum
Kilo Sage

Hello @String 

It looks like you're trying to extract key-value pairs from an incoming email template, convert them into JSON, and then proceed with validation and data insertion into a case record. The code you've shared seems mostly correct, but there are a couple of things you might want to check and adjust.

1. **Parsing the Email Body:**
- Ensure that the `email.body` object contains the expected fields. You might want to log the entire `email.body` to the console to see its structure.

gs.info("Email Body: " + JSON.stringify(email.body));

- If the email body is plain text, you might need to split it into lines and then extract the values accordingly.

2. **Using "Var" instead of "var":**

var emailBody = email.body;

3. **Field Access:**
- Ensure that the field names you are trying to access match the exact case and spelling in the email template. "Category" and `"category"

4. **Trimming Values:**
- Consider trimming the extracted values to remove any leading or trailing spaces.

'category': emailBody.Category.trim(),

5. **Error Handling:**

gs.error('Inbound mail test: error - ' + e.message);

6. **Debugging:**
- Use the ServiceNow debugger or log statements effectively to trace the flow and identify any issues.

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

Thanks & Regards,

Kartik Magadum

Ankur Bawiskar
Tera Patron
Tera Patron

@String 

if the email contains key:value format then simply do this

1) convert everything to lowercase and add _ for spaces

Example below

Contact_Work_Phone -> email.body.contact_work_phone

Helpdesk Contact -> email.body.helpdesk_contact

try this

try{
	// Check if the email body exists and contains the expected fields

	var emailBody = email.body;
	if (emailBody) {
		var extractedData = {
			'Category': emailBody.category,
			'Subcategory': emailBody.subcategory,
			'Short_description': emailBody.short_description,
			'Urgency': emailBody.urgency,
			'Contact': emailBody.contact,
			'Contact_Work_Phone': emailBody.contact_work_phone,
			'Helpdesk_Contact': emailBody.helpdesk_contact
		};
		gs.info("Inbound mail test: " + JSON.stringify(extractedData));
	} else {
		gs.error("Email body not found or does not contain the expected fields.");
	}
}catch (e) {
	gs.info('Inbound mail test:error' + e);
}

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

Thanks @Ankur Bawiskar  for your quick response ,Am able to get the values ,But for phone number am getting as below 

Contact_Work_Phone': emailBody.contact_work_phone,

Contact_Work_Phone:123+ [tel:+12345678900]

 

expected value is Contact_Work_Phone:123

any suggestion ?

@String 

did you check the inbound email body how it looks?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader