Skip to content

Commit ed09dac

Browse files
Update script.js
1 parent c1a0b3c commit ed09dac

File tree

1 file changed

+11
-9
lines changed
  • Server-Side Components/Business Rules/Prevent Deletion of Policy with Active Controls

1 file changed

+11
-9
lines changed

Server-Side Components/Business Rules/Prevent Deletion of Policy with Active Controls/script.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
(function executeRule(current, previous /*null when async*/ ) {
2-
// This Business Rule runs 'before' a record is deleted from the 'sn_compliance_policy' table.
3-
// Its purpose is to prevent a policy from being deleted if it is currently linked to any active controls.
4-
// This helps maintain data integrity and prevents the creation of orphaned or invalidated compliance records.
2+
// This Business Rule runs 'before' a record is updated on the 'sn_compliance_policy' table.
3+
// Its purpose is to prevent a policy from being retired if it is currently linked to any active Control Objectives.
4+
// This enforces a proper decommissioning process, ensuring that Control Objectives are delinked.
5+
// before the policy that governs them, thereby preventing compliance gaps.
6+
// The condition for this rule would be: 'State' changes to 'Retired'.
57

6-
8+
// Instantiate a GlideAggregate object on the many-to-many (m2m) table
79
// 'sn_compliance_m2m_policy_policy_statement'. This table links policies (via the 'document' field)
810
// to control statements (via the 'content' field). Using GlideAggregate is more
911
// performant than GlideRecord for counting records, as it performs the aggregation
1012
// directly in the database.
1113
var grControlAggregate = new GlideAggregate('sn_compliance_m2m_policy_policy_statement');
1214

1315
// Add a query to filter for records in the m2m table where the 'document' field matches
14-
// the sys_id of the policy record currently being deleted.
16+
// the sys_id of the policy record currently being retired.
1517
grControlAggregate.addQuery('document', current.getUniqueValue());
1618

1719
// Add a second query using 'dot-walking' to filter for records where the related
@@ -38,12 +40,12 @@
3840

3941
// Check if the count of active controls is greater than zero.
4042
if (activeControlCount > 0) {
41-
// If active controls were found, add an error message to display to the user.
43+
// If active control objectives were found, add an error message to display to the user.
4244
// The message includes the count for better clarity.
43-
gs.addErrorMessage('Cannot delete this policy because it has ' + activeControlCount + ' active controls linked to it.');
45+
gs.addErrorMessage('Cannot retire this policy because it has ' + activeControlCount + ' active control objectives linked to it. All control objectives must be delinked first.');
4446

45-
// This crucial line aborts the current database transaction (the delete operation).
46-
// It prevents the policy record from being deleted.
47+
// This crucial line aborts the current database transaction (the update operation).
48+
// It prevents the policy record from being marked as 'Retired'.
4749
current.setAbortAction(true);
4850
}
4951

0 commit comments

Comments
 (0)