The CreatorCon Call for Content is officially open! Get started here.

setWorkflow(false) use in business rule

VullankiL
Tera Contributor

Hi 

I have one scenario, where I am using setWorkflow(false).

 

VullankiL_0-1736767804068.png

 

But in my case, there is no workflow associated with the table. But I want to stop executing remaining business rules associated with it. (Because there is another business rule that can abort this cancel action) Can I use it or not? Is there is any other option to stop execution of other business rules in this case?

Thank you

17 REPLIES 17

sanketpatil09
Tera Guru
 

In your case, setWorkflow(false) prevents workflows from running during the gr.update() operation, but it may not stop other business rules from executing. If you want to prevent remaining business rules from running after your logic, you can take a few approaches. One option is to add a custom field, like a "skip_rules" flag, to indicate that other business rules should skip their execution. You can then update those rules to check this flag before they run. Another option is to control the order of your business rules by assigning a lower order value to your cancelation rule, so it runs before others. If needed, you can also use gs.setAbortAction(true) in your business rule to stop further rules from executing. Alternatively, if the update doesn't need to happen immediately, you could move it to a Script Include or Scheduled Job to avoid triggering other business rules altogether.

vishwajeet5550
Mega Guru

you can use the gs.addErrorMessage() and gs.stopProcessing() functions to stop further processing. You can add this logic in the business rule that checks for the cancellation action. If the condition for cancellation is met, the script can abort further execution.

if (cancellationCondition) {
    gs.addErrorMessage(' stopping further execution.');
    gs.stopProcessing(); 
}

 also use setAbortAction(). 

MackI
Kilo Sage

HI @VullankiL 

 

what i understand from your content, currently you do not have any workflow/business rules running on the desired table but you want a kind of future proof and want to stop any business rules associated with it.Please see the script below and you can adjust the script by your means ....

 

// Business Rule: After - Update on [Your Table Name] 
// Order: 1000 (or higher)

(function executeRule(current, previous /*null when async*/) {

    // --- Your Existing Logic (from the image) ---
    var gr = new GlideRecord('[Your Table Name]'); // Replace with your table name
    gr.addQuery('related_id_1', current.related_id_1);
    gr.addEncodedQuery('state!=complete^ORstate=NULL'); 
    gr.query();
    while (gr.next()) {
        gr.setValue('state', 'canceled');

        // --- Future-Proofing Additions ---

        // Prevent other business rules from running after this update
        gr.setWorkflow(false); 

        // Prevent updating sys_updated_by, sys_updated_on, sys_mod_count fields.
        gr.autoSysFields(false);

        // Update the related record (only if no other rule should interfere)
        gr.update(); 
    }

    // --- Additional Future-Proofing Logic (if needed) ---
    // Example:  Set a specific field on the current record to lock it down
    if (current.some_field == 'some_value') { 
        current.some_other_field = 'locked value';
        current.setWorkflow(false); // Stop other rules from changing current record
        current.update(); // Update the current record as the very last step
    }

})(current, previous);

 

 

Prioritize business rules by assigning a high order value (1000+). Prevent cascading business rule triggers in 'while' loops using 'gr.setWorkflow(false)'. Suppress system field updates with 'gr.autoSysFields(false)'. Control workflow for current record updates using 'current.setWorkflow(false)' before 'current.update()'. Ensure 'current.update()' is the final operation.


Key Points to the above script--


Business rule order values determine their priority.
'gr.setWorkflow(false)' prevents business rule execution on related records.
'gr.autoSysFields(false)' suppresses system field updates.
'current.setWorkflow(false)' controls workflow for the current record.
'current.update()' should be the final step to apply all changes.

 

Important Note ---

Usually in real word scenario, its not the best practice but you can keep it for your learning. If you consider any enterprise scenario, before design any busienss rules for say a mature org (4-5 years)

 

Use Only When Necessary: Reserve setWorkflow(false) for situations where it's truly essential to prevent subsequent rules from running. Don't use it as a default approach

Script Refactoring and Consolidation

Identify Related Logic: Analyze existing business rules to find those that operate on the same Script Includes to the Rescue:
Centralized Logic: Move common functions and shared logic into script includes. This promotes code reuse, reduces duplication, and makes it easier to update logic in a single place.
tables and fields and have related purposes.

 

 

If you like this opinion and your problem is resolved after reviewing and applying it. Please kindly mark this your best answer‌🌠‌ OR  mark it  Helpful ‌‌ if you think that you get some insight from this content relevant to your problem and help me to contribute more to this community

MackI | ServiceNow Technical Consultant | DXC Technology Australia | ServiceNow Practice | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia