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

Thanks @PrashantLearnIT ,

I used this your login and used this code and it worked.

var gr = new GlideRecord('cmdb_data_management_task');
gr.addEncodedQuery('numberSTARTSWITHCMDBTASK0001187');
gr.query();
if(gr.next()){
     gs.print('in the if loop gr is: '+ gr);
     gs.eventQueue('sn_cmdb_ws.attestationtask.reassign', gr , '5c6ee44637d05a00ed735ad843990ec2', 'random var2');
     gs.print('after execution of eventQueue');
}

Ravi Gaurav
Giga Sage
Giga Sage

Hi @HrishabhKumar 

Steps:

  1. Retrieve the record: Use GlideRecord to retrieve the relevant record on which the event should be triggered.
  2. Trigger the event: Call gs.eventQueue() with the event name and necessary parameters.



    var eventName = 'my.event.name';

    // Define the list of sys_ids (you can replace these with actual sys_ids)
    var sysIdList = 'sys_id1,sys_id2,sys_id3'; // Example format: '1234567890abcdef,234567890abcdef1'


    var gr = new GlideRecord('your_table'); // Replace 'your_table' with the appropriate table name

    // Get any valid record (you can modify this as per your use case)
    if (gr.get('some_sys_id')) { // Replace 'some_sys_id' with the sys_id of the record

    // Trigger the event using gs.eventQueue
    gs.eventQueue(eventName, gr, sysIdList, null); 
    gs.info('Event triggered: ' + eventName + ' with sys_ids: ' + sysIdList);
    } else {
    gs.error('Record not found!');
    }

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi @Ravi Gaurav  will try and let you know.

Thanks @Ravi Gaurav 

USed your logic and it worked,

var gr = new GlideRecord('cmdb_data_management_task');
if(gr.get('20b27fbfc36f42943473531c0501318e')){
     gs.print('in the if loop gr is: '+ gr);
     gs.eventQueue('sn_cmdb_ws.attestationtask.reassign', gr , '5c6ee44637d05a00ed735ad843990ec2', 'random var2');
     gs.print('after execution of eventQueue');
}