How to create inbound email action for updating record

Suyash Joshi
Tera Contributor

Hello Everyone,

I need a small requirement in Inbound Email Action script in which I want to update any field record in 'Incident' table.

Thanks and Regards,

Suyash

2 ACCEPTED SOLUTIONS

pooja_m
Mega Guru

Hi Suyash,

 

Navigate to Email --> Inbound Actions --> It will open the "Inbound Email Actions " table and create new action or update the existing action if already exists.

 

Below is the code:

 

if (current.getTableName() == "incident") {
    current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
current.state = 1;
        current.update();
}
 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

View solution in original post

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Suyash Joshi 

 

Check this OOTB:

AtulyaLNG_0-1700130160240.png

 

https://INSTANCENAME.service-now.com/now/nav/ui/classic/params/target/sysevent_in_email_action_list....

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

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

View solution in original post

6 REPLIES 6

pooja_m
Mega Guru

Hi Suyash,

 

Navigate to Email --> Inbound Actions --> It will open the "Inbound Email Actions " table and create new action or update the existing action if already exists.

 

Below is the code:

 

if (current.getTableName() == "incident") {
    current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
current.state = 1;
        current.update();
}
 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Suyash Joshi 

 

Check this OOTB:

AtulyaLNG_0-1700130160240.png

 

https://INSTANCENAME.service-now.com/now/nav/ui/classic/params/target/sysevent_in_email_action_list....

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

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

myabrownn
Tera Contributor

Hi Suyash
Here is an example of a small requirement in Inbound Email Action script in which you can update any field record in 'Incident' table:

// Get incident ID from email body
var incidentID = email.body.incidentID;

// Get incident record
var incident = new GlideRecord('incident');
incident.addQuery('number', incidentID);
incident.query();

// If incident found, update the field you want
if (incident.next()) {
  incident.description = 'Updated description';
  incident.update();
}

This script will get the incident ID from the email body, get the incident record, and then update the description field to 'Updated description'. If you want to update a different field, simply change the field name in the script.

Here is an example of how to update the assignment group field:

// Get incident ID from email body
var incidentID = email.body.incidentID;

// Get incident record
var incident = new GlideRecord('incident');
incident.addQuery('number', incidentID);
incident.query();

// If incident found, update the assignment group field
if (incident.next()) {
  incident.assignment_group = 'IT Service Desk';
  incident.update();
}

This script will get the incident ID from the email body, get the incident record, and then update the assignment group field to 'IT Service Desk'. If you want to update a different field, simply change the field name in the script.

Kavita Bhojane2
Tera Expert

Hi @Suyash Joshi,

 

Updates are normally only carried out when the email is flagged as reply and the associated record is identified, either via a watermark, or by the record number being present in the subject. It's a little challenging to do this for a "new" email, but not impossible. 

First, make sure your inbound email trigger works as expected and your action is triggered by an email using the format you want. Hopefully the formatting of your email is specific enough that you won't have issues with multiple actions being triggered, etc. 

In your inbound email action, you'll need to parse out the incident number value for the record you want to update and then write a GlideRecord query to lookup that record, set the values based on your email, and then call .update() to update the record. 

At the end of the script you will want to call current.setAbortAction(true); which will prevent the system from creating a new record as designed. 

Make sure the action has a low execution order and you want to flag it for "stop processing" otherwise you'll likely create an incident from the email as well. 

Without all of the details I can't give you an exact example, but something like this would, when triggered, look up an 'incident' record based on the incident number value passed in the body of the email and then update the comments to be the comments:value passed in the body.

 

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

    // Implement email action here
    var inc_rec = new GlideRecord('incident');
    inc_rec.addQuery('number', email.body.number);
    inc_rec.query();
    if (inc_rec.next()) {
        inc_rec.comments = email.body.comments;
        inc_rec.update();
    }
    current.setAbortAction(true);

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

Please mark my answer correct and helpful, if you find it useful.

 

Thanks,

Kavita Bhojane