|
1 | 1 | (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'. |
5 | 7 |
|
6 | | - |
| 8 | + // Instantiate a GlideAggregate object on the many-to-many (m2m) table |
7 | 9 | // 'sn_compliance_m2m_policy_policy_statement'. This table links policies (via the 'document' field) |
8 | 10 | // to control statements (via the 'content' field). Using GlideAggregate is more |
9 | 11 | // performant than GlideRecord for counting records, as it performs the aggregation |
10 | 12 | // directly in the database. |
11 | 13 | var grControlAggregate = new GlideAggregate('sn_compliance_m2m_policy_policy_statement'); |
12 | 14 |
|
13 | 15 | // 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. |
15 | 17 | grControlAggregate.addQuery('document', current.getUniqueValue()); |
16 | 18 |
|
17 | 19 | // Add a second query using 'dot-walking' to filter for records where the related |
|
38 | 40 |
|
39 | 41 | // Check if the count of active controls is greater than zero. |
40 | 42 | 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. |
42 | 44 | // 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.'); |
44 | 46 |
|
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'. |
47 | 49 | current.setAbortAction(true); |
48 | 50 | } |
49 | 51 |
|
|
0 commit comments