How to enter only comments from approval email

sath
Tera Expert

Hi,

 

When an approver is approving via email, the below should be sent to them:

 

"If you would like to enter comments, place them between the starred lines below, then click Send:

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

(comment entered by approver)

****************************************************************************** "

When the email is received, the comments entered by the approver should be written to the approval record, just the comment itself (and not the surrounding text) should be written to the approval record.

 

We can update the content on email templates (mailto.rejection and mailto.approval), but not sure how to remove surrounding content, can you please assist?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sath 

for removing the surrounded content, you will have to update the OOTB inbound action and use string manipulation to extract the comments

Something like this worked for me, so please enhance as per your requirement

// Simulated email body with instructional text and approver comment
var emailBody = "If you would like to enter comments, place them between the starred lines below, then click Send:\n" +
"******************************************************************************\n" +
"This is the comment entered by the approver.\n" +
"Please review carefully.\n" +
"******************************************************************************\n" +
"Additional trailing text or signature";

// Regex to capture content between 78 or more stars (asterisks)
var regex = /[*]{78,}\s*([\s\S]*?)\s*[*]{78,}/m;

// Extract the comment
var match = regex.exec(emailBody);
var comment = "";
if (match && match[1]) {
    comment = match[1].trim();
}

// Output the extracted comment for confirmation
gs.info("Extracted comment:\n" + comment);

AnkurBawiskar_1-1754316484588.png

You can update like this

var body = email.body_text;
    var comment = '';
    var re = /[*]{78,}\s*([\s\S]*?)\s*[*]{78,}/m; // Match 78+ stars and capture what’s between

    var match = re.exec(body);
    if (match && match[1]) {
        comment = match[1].trim();
    }

    // Set the comment on the approval record (example for a task approval)
    current.comments = comment;

AnkurBawiskar_2-1754316537286.png

 

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

9 REPLIES 9

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @sath 

 

I think it is OOTB available. 

*************************************************************************************************************
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]

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

Ankur Bawiskar
Tera Patron
Tera Patron

@sath 

for removing the surrounded content, you will have to update the OOTB inbound action and use string manipulation to extract the comments

Something like this worked for me, so please enhance as per your requirement

// Simulated email body with instructional text and approver comment
var emailBody = "If you would like to enter comments, place them between the starred lines below, then click Send:\n" +
"******************************************************************************\n" +
"This is the comment entered by the approver.\n" +
"Please review carefully.\n" +
"******************************************************************************\n" +
"Additional trailing text or signature";

// Regex to capture content between 78 or more stars (asterisks)
var regex = /[*]{78,}\s*([\s\S]*?)\s*[*]{78,}/m;

// Extract the comment
var match = regex.exec(emailBody);
var comment = "";
if (match && match[1]) {
    comment = match[1].trim();
}

// Output the extracted comment for confirmation
gs.info("Extracted comment:\n" + comment);

AnkurBawiskar_1-1754316484588.png

You can update like this

var body = email.body_text;
    var comment = '';
    var re = /[*]{78,}\s*([\s\S]*?)\s*[*]{78,}/m; // Match 78+ stars and capture what’s between

    var match = re.exec(body);
    if (match && match[1]) {
        comment = match[1].trim();
    }

    // Set the comment on the approval record (example for a task approval)
    current.comments = comment;

AnkurBawiskar_2-1754316537286.png

 

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

@sath 

Hope you are doing good.

Did my reply answer your question?

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

Hi @Ankur Bawiskar Sorry for the delay, the above script worked when I ran it on background scripts. How can we check this in real time on sub prod instances?

We have a test email box where all the emails from our sub prod instances will be sent to. I have enabled inbound emails on our dev instance and triggered approval email and input the comments, but the email says it is undeliverable.