How to trigger event using background script.

HrishabhKumar
Kilo Sage

Issue : 

an email notification is not getting triggered, it's trigger condition is set when a particular event triggers. so we need to trigger the event to trigger this notification

 

Requirement:

For testing purpose I want to trigger the event using script in background script if it is possible.

suppose event name is : my.event.name

param1: should be list of sys_ids in string format.

how can I trigger the event in the background script, using gs.eventQueue('my.event.name',......);

 

Thanks in advance.

1 ACCEPTED SOLUTION

PrashantLearnIT
Giga Sage

Hi @HrishabhKumar 

 

To trigger an event in the background script using gs.eventQueue, you can pass the required parameters to the function. Here's an example of how you could do it:

// sys_id list in string format (assuming it's a comma-separated list of sys_ids)
var param1 = "sys_id1,sys_id2,sys_id3";

// Converting the string into an array of sys_ids
var sysIdArray = param1.split(',');

// Assuming we are triggering the event for each sys_id in the list
for (var i = 0; i < sysIdArray.length; i++) {
var sysId = sysIdArray[i];

// You need to pass a GlideRecord or any object as the second parameter.
// If it's for a specific table, you can retrieve the record.
var gr = new GlideRecord('your_table_name'); // Replace 'your_table_name' with the actual table
if (gr.get(sysId)) {
gs.eventQueue('my.event.name', gr, param1);
gs.info('Event triggered for sys_id: ' + sysId);
}
}

Explanation:

  1. param1 is a string containing a comma-separated list of sys_ids.
  2. split(',') is used to convert the string into an array of sys_ids.
  3. gs.eventQueue('my.event.name', gr, param1); is where the event is triggered. The first parameter is the event name, the second is the GlideRecord, and additional parameters can be passed afterward.
  4. Looping over the sys_ids ensures that the event is triggered for each sys_id.

 

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

View solution in original post

8 REPLIES 8

Omkar Mone
Mega Sage

Hello,

 

Something like this needs to be done.

 

var eventName = 'event_name';
var sysIds = ['sys_id_1', 'sys_id_2', 'sys_id_3'];
var sysIdsString = sysIds.join(',');

gs.eventQueue(eventName, current(of you notif object), sysIdsString, null);

 

Let me know if that is what you were looking for,

Thanks for the reply @Omkar Mone , Previous replies solved my issue.

PrashantLearnIT
Giga Sage

Hi @HrishabhKumar 

 

To trigger an event in the background script using gs.eventQueue, you can pass the required parameters to the function. Here's an example of how you could do it:

// sys_id list in string format (assuming it's a comma-separated list of sys_ids)
var param1 = "sys_id1,sys_id2,sys_id3";

// Converting the string into an array of sys_ids
var sysIdArray = param1.split(',');

// Assuming we are triggering the event for each sys_id in the list
for (var i = 0; i < sysIdArray.length; i++) {
var sysId = sysIdArray[i];

// You need to pass a GlideRecord or any object as the second parameter.
// If it's for a specific table, you can retrieve the record.
var gr = new GlideRecord('your_table_name'); // Replace 'your_table_name' with the actual table
if (gr.get(sysId)) {
gs.eventQueue('my.event.name', gr, param1);
gs.info('Event triggered for sys_id: ' + sysId);
}
}

Explanation:

  1. param1 is a string containing a comma-separated list of sys_ids.
  2. split(',') is used to convert the string into an array of sys_ids.
  3. gs.eventQueue('my.event.name', gr, param1); is where the event is triggered. The first parameter is the event name, the second is the GlideRecord, and additional parameters can be passed afterward.
  4. Looping over the sys_ids ensures that the event is triggered for each sys_id.

 

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

Hi @PrashantLearnIT , will try and let you know.