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.

Issue with setting values using inbound email action

Karishma Dubey
Tera Expert

Hi,

I created inbound action to create supplier record. I will be receiving email in below format, and I want to set Stichting Rust Nederland to username string field and AN11217385725 to sap number field of record.How can we achieve this in inbound script?

 

Email format 

 

This notification contains important information about your SAP Business Network account (ANID: AN01008912882).

 

Stichting Rust Nederland (AN11217385725) CAN NOW TRANSACT WITH YOU ON SAP Business Network.

4 REPLIES 4

YaswanthKurre
Tera Guru

Hi @Karishma Dubey ,

 

You have to use regex on email body to get the texts you want.

 

Try this inside your inbound action script.

var body = email.body_text;

    // Match the pattern: "Stichting Rust Nederland (AN11217385725)"
    var match = body.match(/([\w\s]+)\s+\((AN\d+)\)/);

    if (match && match.length === 3) {
        var username = match[1].trim(); // Stichting Rust Nederland
        var sapNumber = match[2].trim(); // AN11217385725
//do your script on required table
}

 

Mark this as helpful and correct if this solves your question.

Thanks,

Hi @YaswanthKurre 

if the string is different for example "Carolina Soares da Conceição (AN112173857252)" it is not working and it is giving me result like below.

x_itag_myproc: username o
x_itag_myproc: sapNumber AN112173857252

The sap number is correct, and name is coming as o because the regular expression is not handling names with special characters likeç or ã . Make sure you include the string check as well.

 

Ty this updated regex:

var match = body.match(/([\p{L}\s]+)\s+\((AN\d+)\)/u);

 

Mark this as helpful and correct, if this helps you.

Thanks

  

gaurrohi
Tera Expert

Hi @Karishma Dubey 

To extract the required details from the incoming email, you can use a regular expression to parse the content. Here's an example you can try:

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

	// Implement email action here
	var body = email.body;
	var supplierName, sapNumber;

	var regex = /(.+?)\s+\((\w+)\)\s+CAN NOW TRANSACT WITH YOU/i;
	var match = regex.exec(body);

	if (match) {
		supplierName = match[1].trim();
		sapNumber = match[2].trim();

		current.username = supplierName;
		current.u_sap_number = sapNumber; // replace with the field name 
	} else {
		gs.error("Failed to parse info from email: " + email.subject);
	}

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

 

 

Please mark the response as the correct answer and helpful. This may help other community users to follow the correct solution.

Regards,
Rohit Singh