We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

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

 

3 REPLIES 3

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

yashkamde
Mega Sage

Hello @Lucky1 ,

 

as per your conditions you can write the email script as this :

(function runMailScript(current, template, email, email_action, event) {
    var recipient = new GlideRecord('sys_user');
    if (!recipient.get(current.u_requested_for)) {
        template.print('Unable to identify requesting user. Incident not created.');
        return;
    }

    if (!recipient.active) {
        template.print('User is inactive. Incident not created.');
        return;
    }

    var hasItil = new GlideRecord('sys_user_has_role');
    hasItil.addQuery('user', recipient.sys_id);
    hasItil.addQuery('role.name', 'itil');
    hasItil.query();

    if (!hasItil.hasNext()) {
        template.print('User does not have the ITIL role. Incident not created.');
        return;
    }

    // Passed validation — create the incident
    var inc = new GlideRecord('incident');
    inc.initialize();
    inc.short_description = current.u_sd;
    inc.description = current.u_details;
    inc.caller_id = recipient.sys_id;
    var incSysId = inc.insert();

    if (incSysId) {
        var url = gs.getProperty('glide.servlet.uri') + 'incident.do?sys_id=' + incSysId;
        template.print('An incident has been created for you: <a href="' + url + '">' + inc.number + '</a>');
    } else {
        template.print('Incident creation failed. Please contact your administrator.');
    }
})(current, template, email, email_action, event);

 

If my response helped mark as helpful and accept the solution.