To synchronize a reference field between sc_req_item and sc_task, making sure that changes to the field in either table are reflected in both, you can follow these steps in ServiceNow:
Steps Overview
- Create a business rule on the
sc_task table: This will handle the scenario when the user changes the field on the sc_task form.
- Create a business rule on the
sc_req_item table: This will handle the scenario when the user changes the field on the sc_req_item form.
- Enforce mandatory field logic: Ensuring that the field is mandatory on both
sc_req_item and sc_task tables.
Detailed Steps
1. Business Rule on sc_task Table:
Create a business rule on the sc_task table to update the corresponding field in the sc_req_item table when it is changed.
- Table:
sc_task
- When to run:
After or Update
- Filter conditions: Add condition to check if the specific field is changed (e.g.,
field_name changes)
- Script:
(function executeRule(current, previous /*null when async*/) {
var reqItem = new GlideRecord('sc_req_item');
if (reqItem.get(current.request_item)) { // Assuming 'request_item' is the reference field to `sc_req_item`
reqItem.your_field_name = current.your_field_name;
reqItem.update();
}
})(current, previous);
2. Business Rule on sc_req_item Table:
Create a business rule on the sc_req_item table to update the corresponding field in the sc_task table when it is changed.
- Table:
sc_req_item
- When to run:
After or Update
- Filter conditions: Add condition to check if the specific field is changed (e.g.,
your_field_name changes)
- Script:
(function executeRule(current, previous /*null when async*/) {
var task = new GlideRecord('sc_task');
task.addQuery('request_item', current.sys_id);
task.query();
while (task.next()) {
task.your_field_name = current.your_field_name;
task.update();
}
})(current, previous);
3. Making Field Mandatory:
Ensure the field is mandatory on both forms via dictionary settings.
-
Go to Dictionary Entry for the field on sc_req_item:
- Set the
Mandatory checkbox to true.
-
Configure Dictionary Entry for the same field on sc_task:
- This might already be inherited as a reference field. If not, set the
Mandatory checkbox to true if it allows.
Alternatively, you can make it mandatory via Client Scripts if there are conditional requirements:
-
Create Client Script on sc_req_item:
function onLoad() {
g_form.setMandatory('your_field_name', true);
}
-
Create Client Script on sc_task:
function onLoad() {
g_form.setMandatory('your_field_name', true);
}
Summary
To ensure that changes to the reference field are reflected across both sc_req_item and sc_task tables, you need two business rules. Ensure that the field is marked mandatory on both tables via dictionary settings or client scripts. This approach will keep your reference field synchronized between the two tables and ensure that it is always populated.