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.

get the email id from the body of email subject

hemantnh
Tera Expert

i have written one inbound email action 

 

i want to read the user email id from the body of the email and update it in the caller fields. please help to get it fied 

1 ACCEPTED SOLUTION

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @hemantnh 

 

If you want to read from body of email then you can check ootb inbound email action named as "Create Incident (Forwarded) or Create Incident" [sys_id = 5e53d645c0a8006400b4b0a49f2139ba or 3ccfeff5c611227a0180144333c87af9] where it checks if email is provided for label assign then get it assigned to caller.

 

Else if no such label present then you can write a script something as below under actions section in script field of inbound email action

 

 // Extract the email body
    var emailBody = email.body_text;

    // Use a regular expression to find the email address in the body
    var emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/;
    var emailMatch = emailBody.match(emailRegex);

    if (emailMatch) {
        var userEmail = emailMatch[0];

        // Find the user record based on the email address
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('email', userEmail);
        userGR.query();

        if (userGR.next()) {
            // Update the caller field with the found user
            current.caller_id = userGR.sys_id;
        }
    }

 

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

View solution in original post

1 REPLY 1

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @hemantnh 

 

If you want to read from body of email then you can check ootb inbound email action named as "Create Incident (Forwarded) or Create Incident" [sys_id = 5e53d645c0a8006400b4b0a49f2139ba or 3ccfeff5c611227a0180144333c87af9] where it checks if email is provided for label assign then get it assigned to caller.

 

Else if no such label present then you can write a script something as below under actions section in script field of inbound email action

 

 // Extract the email body
    var emailBody = email.body_text;

    // Use a regular expression to find the email address in the body
    var emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/;
    var emailMatch = emailBody.match(emailRegex);

    if (emailMatch) {
        var userEmail = emailMatch[0];

        // Find the user record based on the email address
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('email', userEmail);
        userGR.query();

        if (userGR.next()) {
            // Update the caller field with the found user
            current.caller_id = userGR.sys_id;
        }
    }

 

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.