Correct way to trigger a Business Rules from Scheduled Script

Happy S
Tera Expert

Hi,

 

As per subject, I just wanted to know the correct way to trigger Business Rules from Scheduled Script.

 

In my Scheduled Script, i have put in - example :

gs.eventQueue('event_A', table, current.user);

 

And in my Business Rules. I have put in a few events to trigger emails based on different conditions.

 

So did I do it wrongly? Since I tested out and the Events did not fire at all..

 

This is what I am trying to do.

 

1. Look for KBs that has comment added by employees

2. Send emails to these employees to gather more feedback -- trigger event

3. If these employees are no longer active then trigger the emails to their Supervisors - -- trigger event

4. If both the employees and Supervisors are no longer active then trigger the emails to the Department Managers - -- trigger event

 

In summary - my question is -- Can I trigger events in a Business Rule from an event in a Scheduled Script?

 

Appreciate if anyone could just guide me on how to proceed with the request.

 

Thank you in advance ! 🙂

 

12 REPLIES 12

Hi, 

I have updated the scheduled script to trigger the event. Now you can create a notification which triggers on 'kb_comment_added' event. 

 

If your requirement is to send notifications when someone adds comment to kb article then the below mentioned code should work.

 

// Scheduled Script: TriggerKBCommentAddedEvent
// Description: This script runs daily to identify KB records with new comments by employees and triggers a custom event.

(function execute() {
    // Define a GlideDateTime object for the current date and time
    var now = new GlideDateTime();

    // Calculate the date 24 hours ago (for daily check)
    var twentyFourHoursAgo = new GlideDateTime();
    twentyFourHoursAgo.subtract(1);

    // Create a GlideRecord object for the "kb_knowledge" table (KB articles)
    var kbGR = new GlideRecord('kb_knowledge');

    // Add conditions to filter KB records with comments added by employees in the last 24 hours
    kbGR.addQuery('sys_created_on', '>', twentyFourHoursAgo);
    kbGR.addQuery('comments', 'CONTAINS', '@employee_mention'); // Adjust to match your mention format
    
    kbGR.query();

    // Loop through the filtered KB records and trigger the custom event
    while (kbGR.next()) {
        gs.eventQueue("kb_comment_added", kbGR, kbGR.getUniqueValue());
    }
})();

 

 

 And if you want to trigger BR from scheduled script you can do something like below 

 

 

// Trigger a custom event
gs.eventQueue("custom_event_name", current, current.getUniqueValue());

 

 

in your BR add the below lines

 

 

(function executeRule(current, previous /*, g_scratchpad*/) {
    // Check if the custom event "custom_event_name" triggered this Business Rule
    if (current.u_custom_event_name == 'custom_event_name') {
        // Add your logic here to respond to the custom event
        gs.addInfoMessage("Custom event received! Do something...");
    }
})(current, previous);

 

 

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Thank you for the quick reply..

 

I tested out your code for both Scheduled Script and Business Rules

 

Scheduled Script

(function execute() {
    
	var Test = new GlideRecord('kb_knowledge');
	Test.query();

    // Loop through the filtered KB records and trigger the custom event
    while (Test.next()) {
		
	gs.eventQueue("custom_event_name", current, current.getUniqueValue());
    }
})();

 

Business Rules 

(function executeRule(current, previous /*, g_scratchpad*/) {
    // Check if the custom event "custom_event_name" triggered this Business Rule
    if (current.u_custom_event_name == 'custom_event_name') {
        // Add your logic here to respond to the custom event
        gs.addInfoMessage("Custom event received! Do something...");
    }
})(current, previous);

 

 Outcome :

- the event triggered out from Scheduled Script is working as expected, I can see it in the Event logs

- but I cannot see any event being triggered by the Business Rule, I cannot locate the Info Message so I must have did something wrong and this is the same issue that I have been having..

 

Appreciate if you can help me on this..

Can you try this code in your BR ? 

 

 

    var event = new GlideEvent('your_event_name'); // Replace 'your_event_name' with the actual name of your event
    event.setPriority(GlideEventPriority.NORMAL); // Set event priority if needed
    event.setSource('Business Rule'); // Set the source of the event
    event.setParms({
        changeRequestId: current.getValue('sys_id') // Pass any parameters you need with the event
    });
    event.fire();

 

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Ok I tested this out and in the Event log I see the output:

(function executeRule(current, previous /*, g_scratchpad*/) {
	
// Firing a custom event
var event = new GlideEvent('custom_event_name'); // Use the same event name here
event.setPriority(GlideEventPriority.NORMAL); // Set event priority if needed
event.setSource('sys_script'); // Set the source of the event
event.setParms({
    changeRequestId: current.getValue('sys_id') // Pass any parameters you need with the event
	
});
event.fire();
	gs.addInfoMessage("Trigger Test for custom_event_name");
    })(current, previous);

Name: text_index

Table: Business Rule [sys_script]

Parm1: [script]

Parm2; update

 

But somehow I still cannot locate the info message which is strange since the Event is fired out from the Business Rule via the Event triggered in the Scheduled Script.

 

Thank you..

So this worked ? just that you not able to locate the info message right? 

 

Also , If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.