Email Script not working when clicked on Preview Notification

Lucky1
Tera Guru

Hello all,

 

I have configured a notification on my custom table, with When to run is whenever a record is created.

So, when the user receives email on this, I want to provide him a link to "create an Incident".

So, when the user clicks on it from the email he received, an Incident should be created in Service-now instance.

 

But before creating an Incident, it should check if the user is Active user in service-now and does he has 'itil' role.

 

The above condition I have not checked, but I have written an email script like this:

Lucky1_0-1784872728587.png

 

So, can someone help me in achieving this? Validating the email receiving user in Service-now and also create incident when clicked on the link.

 

 

Thank you,

Regards,

Lucky

 

1 ACCEPTED SOLUTION

Tanushree Maiti
Tera Patron

Hi @Lucky1 

 

Try this:

 

1: Create/update your Mail Script

 

(function runMailScript(/* Template */ template, /* TemplatePrinter */ email, /* Event */ event, /* Table */ current, /* EmailLog */ log) {

    var sysId = current.sys_id;

    var tableName = current.getTableName();

    var emailUrl = gs.getProperty('glide.servlet.uri') + 'nav_to.do?uri=';

    var mailto = "mailto:" + gs.getProperty('glide.email.smtp.from') +

                 "?subject=Create%20Incident%20from%20" + tableName%20+:%20" + sysId +

                 "&body=Ref:%20" + sysId;

                

    template.print('<a href="' + mailto + '">Create an Incident</a>');

})(current, template, email, event, current);

 

2: Create an Inbound Email Action

Go to System Policy > Email > Inbound Actions > click New

  • Name: Create Incident from Custom Table Email
  • Type: Record Action
  • Target Table: Incident [incident]
  • Condition: email.subject.indexOf("Create Incident from") > -1

 

(function runAction() {

    var userGr = new GlideRecord('sys_user');

    userGr.addQuery('email', email.from);

    userGr.query();

   

    if (userGr.next()) {

        var isActive = userGr.active == true;

        var hasItil = userGr.hasRole('itil');

       

        if (isActive && hasItil) {

            current.caller_id = userGr.sys_id;

            current.short_description = email.subject;

            current.description = email.body.text;

            current.insert();

        } else {

           

            gs.log('User ' + email.from + ' attempted to create incident via email but failed active/itil validation.');

        }

    } else {

        gs.log('Inbound email sender ' + email.from + ' not found in sys_user table.');

    }

})();

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

View solution in original post

13 REPLIES 13

Ankur Bawiskar
Tera Patron

@Lucky1 

not a good practice to insert record from email script

Let them reply to that email and you can configure inbound action to create INC

OR

Have a link to portal where they can raise incident

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Tera Patron

Hi @Lucky1 

 

Try this:

 

1: Create/update your Mail Script

 

(function runMailScript(/* Template */ template, /* TemplatePrinter */ email, /* Event */ event, /* Table */ current, /* EmailLog */ log) {

    var sysId = current.sys_id;

    var tableName = current.getTableName();

    var emailUrl = gs.getProperty('glide.servlet.uri') + 'nav_to.do?uri=';

    var mailto = "mailto:" + gs.getProperty('glide.email.smtp.from') +

                 "?subject=Create%20Incident%20from%20" + tableName%20+:%20" + sysId +

                 "&body=Ref:%20" + sysId;

                

    template.print('<a href="' + mailto + '">Create an Incident</a>');

})(current, template, email, event, current);

 

2: Create an Inbound Email Action

Go to System Policy > Email > Inbound Actions > click New

  • Name: Create Incident from Custom Table Email
  • Type: Record Action
  • Target Table: Incident [incident]
  • Condition: email.subject.indexOf("Create Incident from") > -1

 

(function runAction() {

    var userGr = new GlideRecord('sys_user');

    userGr.addQuery('email', email.from);

    userGr.query();

   

    if (userGr.next()) {

        var isActive = userGr.active == true;

        var hasItil = userGr.hasRole('itil');

       

        if (isActive && hasItil) {

            current.caller_id = userGr.sys_id;

            current.short_description = email.subject;

            current.description = email.body.text;

            current.insert();

        } else {

           

            gs.log('User ' + email.from + ' attempted to create incident via email but failed active/itil validation.');

        }

    } else {

        gs.log('Inbound email sender ' + email.from + ' not found in sys_user table.');

    }

})();

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Hi Tanushree,

 

var emailUrl = gs.getProperty('glide.servlet.uri') + 'nav_to.do?uri=';

    var mailto = "mailto:" + gs.getProperty('glide.email.smtp.from') +

                 "?subject=Create%20Incident%20from%20" + tableName%20+:%20" + sysId +

                 "&body=Ref:%20" + sysId;

 

Also, where does this variable used?   emailUrl 

From these two lines, I don't see the property in system properties.

 

 

Hi @Lucky1 

 

Just create the sys_properties  ,  put an email id in the value where email will be sent and you can test in non-prod .

You will give sys_properties name as per your choice like glide.email.to 

 

Let me know whether you are getting expected your result

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti