On clicking button on the portal notification should be triggered

Deepika Mishra
Mega Guru

Hi All,

Usecase: On clicking the button a notification should be triggered to caller of the incident.

For this I developed below, however it is not working
Event:

DeepikaMishra_0-1743668256320.png

 

Notification:

DeepikaMishra_1-1743668311866.png

DeepikaMishra_2-1743668336638.png

DeepikaMishra_3-1743668363409.png



Widget:

HTML:

<h2>Button triggering notification</h2>
<div>
  <button class="btn btn-primary" ng-click="c.sendEmail()">Send Email</button>
</div>

Client Controller:

api.controller = function($http) {
    var c = this;

    c.sendEmail = function() {
        c.server.get({
            action: 'triggerEmail'
        }).then(function(response) {
            if (response.data.success) {
                alert('Email sent successfully');
            } else {
                alert('Failed to send email');
            }
        });
    };
};

Server:

(function() {
  if (input && input.action === 'triggerEmail') {
    gs.eventQueue('trigger_email_event', null, gs.getUserID(), 'Email sent successfully');
    data.success = true;
  } else {
    data.success = false;
  }
  return data;
})();

 

I am not sure why it isn't working. Can someone help with correction and execution ?

1 ACCEPTED SOLUTION

@Deepika Mishra 

some corrections in server side

gs.eventQueue() should be within the IF and it should include inc object

Hope event parm1 contains Recipient=true in your notification

(function() {
    gs.log('1. DeeT Server script started');
    if (input && input.action === 'triggerEmail') {
        var obj;
        var inc = new GlideRecord('incident');
        inc.addQuery('sys_id', '57af7aec73d423002728660c4cf6a71c');
        inc.query();
        if (inc.next()) {
            gs.eventQueue('trigger_email_event', inc, gs.getUserID(), 'Email sent successfully');
            data.success = true;
            gs.log('3. DeeT Event triggered: true');
        }
    } else {
        data.success = false;
        gs.log('3. DeeT Event triggered: false');
    }
    gs.log('4. DeeT Server script completed');
    return data;
})();

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

5 REPLIES 5

Shivalika
Mega Sage

hello @Deepika Mishra 

 

The format for gs.eventQueue() is this -

 

Shivalika_0-1743671579035.png

 

 

I don't think "Eail sent succesfully and null was required ? No record is passed. 

Please verify this. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY QQ

Ankur Bawiskar
Tera Patron
Tera Patron

@Deepika Mishra 

this link has similar requirement, please check and enhance/fix your widget code

Email notification at the click of a button in Service Portal 

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Deepika Mishra 

where is this widget used? share some more details about it along with screenshots

how are you determining email should be triggered foe which INC?

I believe in eventQueue() since you are not passing GlideRecord object as 2nd parameter it's not knowing which RECORD to use to trigger

Try to use this and see if it goes to server side

HTML:

<h2>Button triggering notification</h2>
<div>
  <button class="btn btn-primary" ng-click="c.sendEmail()">Send Email</button>
</div>

Client Controller:

api.controller = function($http) {
    var c = this;

    c.sendEmail = function() {
        c.server.get({
            action: 'triggerEmail'
        }).then(function(response) {
            if (response.data.success) {
                alert('Email sent successfully');
            } else {
                alert('Failed to send email');
            }
        });
    };
};

Server:

(function() {
    data.success = false;
    if (input && input.action === 'triggerEmail') {
		gs.addInfoMessage('server script called');

		// you should query incident table with sysId and then use the GlideRecord object here

        gs.eventQueue('trigger_email_event', obj, gs.getUserID(), 'Email sent successfully');
        data.success = true;
    }
})();

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

Deepika Mishra
Mega Guru

I added below, still facing issue

HTML:

<h2>Button triggering notification</h2>
<div>
  <button class="btn btn-primary" ng-click="c.sendEmail()">Send Email</button>
</div>

 

Server:

(function() {
    gs.log('1. DeeT Server script started');
    if (input && input.action === 'triggerEmail') {
        var obj;
        var inc = new GlideRecord('incident');
        inc.addQuery('sys_id', '57af7aec73d423002728660c4cf6a71c');
        inc.query();
        if (inc.next()) {
            obj = inc.sys_id;
            gs.info('2. DeeT Incident Number: ' + inc.number + ' with sys_id: ' + obj);
        }
        gs.eventQueue('trigger_email_event', obj, gs.getUserID(), 'Email sent successfully');
        data.success = true;
        gs.log('3. DeeT Event triggered: true');
    } else {
        data.success = false;
        gs.log('3. DeeT Event triggered: false');
    }
    gs.log('4. DeeT Server script completed');
    return data;
})();

 

Client:

api.controller = function($http) {
    var c = this;

    c.sendEmail = function() {
        console.log('sendEmail function called');
        c.server.get({
            action: 'triggerEmail'
        }).then(function(response) {
            console.log('Server response:', response);
            if (response.data.success) {
                alert('Email sent successfully');
            } else {
                alert('Failed to send email');
            }
        });
    };
};

 

Log Results:

DeepikaMishra_0-1744003979551.png

DeepikaMishra_1-1744004030820.png

DeepikaMishra_2-1744004074083.png



Guide me what to change in this ?