Counter should update based on the due date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2024 03:31 AM
In the Risk event form, Due Date: Add ‘Counter’ field in the Key dates section. The counter field should updated based on the Due date changes.
Due date in the Risk Event Task should not be greater than Risk Event due date.
Scenario 1: If a due date is entered in the Risk Event form and a subsequent due date is entered in the Risk Event Task form that exceeds the due date in the Risk Event form, an error message should appear stating, 'Task Due Date cannot be greater than Event Due Date.'
Scenario 2: If a due date is entered in the Risk Event form and a subsequent due date is entered in the Risk Event Task form, and if the user changes the due date in the Risk Event form to a later date than the due date in the Risk Event Task form, then there should be an error message stating, 'The Task Due Date cannot be later than the Event Due Date.'
Can someone help me on this
#grc #servicenow
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2024 12:20 AM
Hi @MonishramP
Step 1: Add ‘Counter’ field in the Key Dates Section
1. Create the Counter Field:
- Navigate to the Risk Event form.
- Right-click the form header and select Configure > Form Layout.
- Add a new field named counter of type Integer.
2. Add Counter Field to Key Dates Section:
- In the Form Layout, ensure counter field is added under the Key Dates section.
### Step 2: Implement Client Scripts for Validations
We need to create Client Scripts on the Risk Event Task form and Risk Event form to enforce due date constraints.
#### Client Script on Risk Event Task Form
1. Navigate to System Definition > Client Scripts.
2. Click New to create a new Client Script.
3. Configure the following:
- Name: Validate Task Due Date
- Table: Risk Event Task (replace with the actual table name if different)
- Type: onChange
- Field Name: due_date (replace with your actual due date field)
4. Script:
// Query the related Risk Event record for the due date
var riskEvent = new GlideRecord('risk_event'); // Replace ‘risk_event’ with the actual table name
if (riskEvent.get(current.risk_event)) { // Replace ‘risk_event’ with reference field name on task
var eventDueDate = riskEvent.getValue('due_date'); // Risk Event due date field
if (current.due_date > eventDueDate) {
gs.addErrorMessage('Task Due Date cannot be greater than Event Due Date.');
current.setAbortAction(true);
}
}
#### Client Script on Risk Event Form
1. Navigate to System Definition > Client Scripts.
2. Click New to create a new Client Script.
3. Configure the following:
- Name: Prevent Event Due Date Change
- Table: Risk Event (replace with the actual table name if different)
- Type: onChange
- Field Name: due_date (replace with your actual due date field)
4. Script:
// Query all related Risk Event Task records for their due dates
var taskGR = new GlideRecord('risk_event_task'); // Replace ‘risk_event_task’ with the actual table name
taskGR.addQuery('risk_event', current.sys_id); // Replace ‘risk_event’ with reference field name on task
taskGR.query();
while (taskGR.next()) {
if (taskGR.getValue('due_date') > current.due_date) {
gs.addErrorMessage('The Task Due Date cannot be later than the Event Due Date.');
current.setAbortAction(true);
break;
}
}
Step 3: Implement Business Rule to Update Counter Field
1. Navigate to System Definition > Business Rules.
2. Click New to create a new Business Rule.
3. Configure the following:
- Name: Update Counter Field
- Table: Risk Event (replace with actual table name if different)
- When: Before
- Update: Checked
4. Script:
// Calculate the difference in days between the current due date and the previous due date
var counter = 0;
if (current.due_date && previous.due_date) {
var dueDate = new GlideDateTime(current.due_date);
var prevDueDate = new GlideDateTime(previous.due_date);
counter = dueDate.getDiff(prevDueDate, 'day');
}
current.counter = counter; // Update counter field
Please mark this response helpful or Accepted Solution, if this helps you in understand the requirement. This will Help both the community and me.
Thanks,
Deepak Sharma