How to trigger an event to email using client script on service portal

Shaik Ibrahim k
Tera Contributor

Need to trigger an event to email using client script in Service Portal

4 REPLIES 4

Community Alums
Not applicable

Hi @Shaik Ibrahim khaleelulla ,

Check this link,It will help you.

https://community.servicenow.com/community?id=community_question&sys_id=5286c725db1cdbc01dcaf3231f96...

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

Nilesh2
Tera Expert

Hi @Shaik Ibrahim k 

Please refer to my article to resolve your issue.

Trigger email/event on field change using client script (without saving form) 

If you find my answer correct and helpful, kindly mark it as such.

 

Thank you,
Nilesh

 

 

ChiranjeeviR
Kilo Sage

Hi @Shaik Ibrahim k ,

 

In ServiceNow's Service Portal, Client Scripts (written in JavaScript and running in the browser) cannot directly send emails or trigger server-side events, because they run on the user's browser and do not have access to the server’s email functionality.

However, you can trigger an event (which sends an email) from a client script by using a combination of:

  1. Client Script (to capture the action and call a Script Include or GlideAjax)
  2. Script Include (server-side logic to trigger an event and send an email)
  3. Event Registration and Email Notification based on that event

 

Please follow step by step:

  1. Register an Event

Go to System Policy > Events > Registry and create a new event, e.g.:

  • Name: custom.email.trigger
  • Table: e.g., sc_req_item (or any applicable table)
  • Description: Triggered from portal to send email

ChiranjeeviR_0-1747760280061.png

 

2. Create an Email Notification

Go to System Notification > Email > Notifications:

  • Trigger on the event: custom.email.trigger
  • Define the conditions
  • Choose recipients
  • Create the email content

ChiranjeeviR_1-1747760315459.png

 

3.Create Script Include

Go to System Definition > Script Includes and create one like this:

var PortalEmailTrigger = Class.create();
PortalEmailTrigger.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    sendEmail: function() {
        var gr = new GlideRecord('sc_req_item'); // Use appropriate table
        if (gr.get(this.getParameter('sys_id'))) {
            gs.eventQueue('custom.email.trigger', gr, gs.getUserID(), '');
            return "Event triggered";
        }
        return "Record not found";
    }

});

ChiranjeeviR_2-1747760346304.png

 

 

Ensure it's Client Callable

 

 4. Client Script (Service Portal Widget or Catalog Client Script)

Use this code in your Catalog Client Script or Widget Client Script:

 

function triggerEmail(sys_id) {
    var ga = new GlideAjax('PortalEmailTrigger');
    ga.addParam('sysparm_name', 'sendEmail');
    ga.addParam('sys_id', sys_id); // Replace with appropriate sys_id
    ga.getXMLAnswer(function(response) {
        console.log(response); // Optional: show response
    });
}

// Call this function based on some user action, e.g., button click
triggerEmail(g_form.getUniqueValue()); // For Catalog Client Script

Thanks and Regards,

Chiranjeevi R

Please mark as Correct Answer/Helpful, if applicable.

 

 

Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.

PradeepAdiga
Tera Contributor

Hi Shaik,

First create a client-callable Script Include that contains the server-side logic. Then, in the widget’s Client Script, use the GlideAjax object to call the Script Include method. When the server responds, the result can be processed and displayed in the widget using AngularJS bindings. The widget’s HTML template typically includes a button or trigger to initiate the call. This approach allows you to securely execute server-side logic (like sending emails or fetching data) from the client side in Service Portal.

 

Regards,

Pradeep Adiga