story should not close if associated defect is open

Prakash_S
Tera Contributor

I have a requirement to prevent story closer if defect is still active.

I tried with writing BR but not working.

(function executeRule(current, previous /*null when async*/) {
    // Query the defect table for associated defects
    var defectGr = new GlideRecord('rm_defect'); // Replace 'rm_defect' with the actual defect table name
    defectGr.addQuery('story', current.sys_id); // Assuming 'story' is the reference field on the defect table
    defectGr.addQuery('state', '!=', 'Closed'); // Replace 'state' and 'Closed' with actual field and value
    defectGr.query();

    if (defectGr.hasNext()) {
        // Prevent the story from being closed
        gs.addErrorMessage('This story cannot be closed because it has associated defects that are still open.');
        current.state = previous.state; // Revert the state change
        gs.log('Story closure prevented due to open defects: ' + current.number, 'Story Closure Rule');
    }
})(current, previous);

  

2 ACCEPTED SOLUTIONS

Runjay Patel
Giga Sage

Hi @Prakash_S ,

 

You can achieve it via configuration of BR.

RunjayPatel_0-1734678768483.png

 

RunjayPatel_1-1734678859845.png

 

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

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

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

 

 

View solution in original post

Hemanth M1
Giga Sage
Giga Sage

Hi @Prakash_S ,

 

Why don't use defect field on the story to check if still active and abort action. 

 

You don't need to any scripting logic

Step 1)

HemanthM1_2-1734679225441.png

 

step 2)

HemanthM1_1-1734679076544.png

 

Hope this helps!!!

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

View solution in original post

5 REPLIES 5

ag0908
Tera Contributor

Hello Prakash,

 

You have not specified which table this BR is running on, assuming it is on Story table.

Use below instead of trying to revert state to previous state.

Your BR should also run on before so state change can be prevented.

Additional improvement, use GlideAggregate instead of GlideRecord for performance reasons.

current.setAbortAction(true);

 

Br, Abhishek

PrashantLearnIT
Giga Sage

Hi @Prakash_S 

 

 if (current.state.changesTo('closed')) {
        // Query the defect table for associated open defects
        var defectGR = new GlideRecord('rm_defect'); // Replace with the correct table name for defects
        defectGR.addQuery('story', current.sys_id);
        defectGR.addQuery('state', '!=', 'closed'); // Adjust state field and value as needed
        defectGR.query();

        if (defectGR.hasNext()) {
            gs.addErrorMessage('Cannot close the story. It has associated open defects.');
            current.setAbortAction(true); // Prevent the save or update
        }
    }
 
********************************************************************************************************
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
********************************************************************************************************

Why if condition in code "if (current.state.changesTo('closed'))" ?

This should be in the filter condition field for BR to execute or in Condition field, not in script body.

Runjay Patel
Giga Sage

Hi @Prakash_S ,

 

You can achieve it via configuration of BR.

RunjayPatel_0-1734678768483.png

 

RunjayPatel_1-1734678859845.png

 

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

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

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