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

JamesEcoStratus
Mega Guru

Yes, you can trigger Business Rules from a Scheduled Script in ServiceNow. However, if the events are not firing as expected, there may be some issues with your current setup. To help you achieve your goal of identifying Knowledge Base articles with comments added by employees and triggering emails to different recipients based on certain conditions, consider the following steps and best practices:

 

Step 1: Check Business Rule Conditions

  • Ensure that the conditions in your Business Rules are correctly set up to trigger events. Double-check that your conditions align with the criteria for sending emails, as outlined in your question (e.g., comments added by employees).

Step 2: Utilize gs.eventQueue

  • You're on the right track with using gs.eventQueue in your Scheduled Script to trigger events. Ensure that the event names in your Scheduled Script ('event_A' in your example) match the event names specified in your Business Rules.

Step 3: Debugging for Issue Resolution

  • Debugging is crucial if events are not firing as expected:
    • Check the System Log (syslog_transaction table) for any error messages or logs related to your Scheduled Script or Business Rules. This helps identify script or rule condition issues.
    • Test your Business Rules individually by manually triggering them in a relevant record (e.g., a Knowledge Base article) to ensure they work as intended.
    • In your Scheduled Script, use gs.log to log messages at different script points. This facilitates tracing the execution flow and identifying potential issues.

Step 4: Verify Script Execution Context

  • Ensure that your Scheduled Script has the correct permissions and executes on the relevant record. Confirm that it operates on the appropriate table and that the executing user has the necessary access.

Step 5: Order of Execution

  • Verify the order of execution for your Business Rules, especially if multiple Business Rules on the same table may be triggered. Adjust the order if necessary to achieve the desired outcome.

Step 6: Thorough Testing

  • Test your script and Business Rules extensively with sample data to verify that events are triggered as expected. Create test records that meet the specified conditions to validate the entire process.

Step 7: Consider Versioning

  • Ensure that your Business Rules are active and aligned with the correct version of the table you are working with.

It's important to note that ServiceNow configurations can vary among organizations and ServiceNow versions. For precise insights into your organization's ServiceNow setup, version, and environmental requirements, it is recommended to consult with your ServiceNow administrator or IT team. They can provide specific details and considerations based on your unique implementation.

 

Best of luck with your ServiceNow project.

 

James @Ecostratus

 

If you found this response helpful, please consider marking it as "Helpful" or "Correct."

thank you... wondering if your are able to provide any examples of how the event are being setup in Scheduled Script and Business Rules respectively?

 

so that I can refer and make the changes accordingly..

LadySNWizard
Tera Contributor

Hi,

Please refer the approach below and let me know if you need any help with the code.

 

Scheduled Script Event:

  • Your Scheduled Script can run on a scheduled basis to look for KBs (Knowledge Base articles) that have comments added by employees.

Update Records and Trigger Events:

  • In the Scheduled Script, when you identify KBs with employee comments, you can update these KB records to include a flag or some indicator that an email needs to be sent for feedback gathering.

  • Additionally, you can set up a custom field in the KB records to store the recipient(s) of the email, such as the employee's email address, supervisor's email address, or department manager's email address.

  • After updating the KB records, you can trigger a custom event using the gs.eventQueue or gs.event API to signal that feedback gathering emails need to be sent. Pass relevant information, such as the KB record's sys_id or the recipient's email address, as event parameters.

Business Rule to Handle Events:

  • Create a Business Rule that listens for the custom event you triggered from the Scheduled Script. The Business Rule should be configured to run when this custom event is fired.

  • In the Business Rule's script, you can check the custom field in the KB records to determine who the email should be sent to (employee, supervisor, or department manager). Based on this information, construct and send the appropriate email.

Email Handling:

  • If an email needs to be sent to an employee for feedback gathering, the Business Rule can send the email directly to the employee's email address.

  • If the employee is no longer active, the Business Rule can retrieve the supervisor's email address and send the email to the supervisor instead.

  • If both the employee and the supervisor are no longer active, the Business Rule can further escalate to the department manager's email address and send the email there.

 

Scheduled Script to run on a scheduled basis(Daily) to look for KBs (Knowledge Base articles) that have comments added by employees.

 

 

 

// Scheduled Script: FindKbArticlesWithEmployeeComments
// Description: This script runs daily to identify KB articles with employee comments.

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

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

    // Create a new GlideRecord to query the kb_feedback table
    var feedbackGR = new GlideRecord('kb_feedback');
    feedbackGR.addQuery('created_at', '>', twentyFourHoursAgo); // Comments created in the last 24 hours
    feedbackGR.query();

    // Iterate through comments and find associated KB articles
    while (feedbackGR.next()) {
        var kbArticle = new GlideRecord('kb_knowledge');
        if (kbArticle.get(feedbackGR.kb_knowledge)) {
            // Process KB article with an employee comment here
            gs.log('KB Article with Employee Comment: ' + kbArticle.number);
            // Add your logic to handle these KB articles
        }
    }
})();

 

 

 

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

thank you...do you mind to provide example of the Business Rule script too?

 

I think my issue now is how to get the Scheduled Script to talk to the Business Rule

 

Currently it's like they have the connection established, but the Business Rule can't understand what the Script wants it to do..