In servicenow, i have a table task_card. My requirement is if the time card is being inserted and Category Is Task_work then cost center field should be automatically updated to cost center value on the task record,(Cost Center is the reference field on both the tables).
Assistant
User
In servicenow, i have a table task_card. My requirement is if the time card is being inserted and Category Is Task_work and cost center field is empty then cost center field should be automatically updated to cost center value on the task record,(Cost Center is the reference field on both the tables).
Assistant
User
There is one table called Assessment instance in that table when state is completed. at that time there is one custom table called F&F in that table there is one field called interviews conducted in witch 2 choices are available Yes/No it should automatically set as yes.
in short when Assessment instance's state is completed at that time FNF tables field ( Interview conducted) should set as yes.
Assistant
To achieve your requirement in ServiceNow, you can make use of Business Rules. A Business Rule is a server-side script that runs when a record is displayed, inserted, updated, deleted, or when a table is queried. It can be set to run before or after the database action has occurred. In your case, you would use a "before insert" business rule to update the Cost Center field on the task_card record based on the related Task record's Cost Center, given the category field is equal to "Task_work".
Here's a step-by-step guide on how you can set up this Business Rule:
Navigate to System Definition > Business Rules in your ServiceNow instance.
Click on the New button to create a new Business Rule.
Fill out the form as follows:
Name: Give your Business Rule a name, e.g., Update Cost Center from Task.
Table: Select the task_card table from the dropdown.
Ensure the Advanced checkbox is checked to write a script.
When to run: Check the before checkbox since you want this to run before the insert operation is committed to the database. Also, check the insert checkbox since you want this to run on record insertion.
In the Filter Conditions area, you could add a condition to only run this rule when the category equals Task_work to ensure it only runs under the specific condition you mentioned.
Script:
code
(function executeRule(current, previous /*null when async*/) {
// Check if the category is 'Task_work'
if (current.category == 'Task_work') {
// Reference the related task record
var taskGR = new GlideRecord('task');
if (taskGR.get(current.task)) { // Assuming 'task' is the reference field to the task table
// Set the Cost Center value from the related task record to the task_card record
current.cost_center = taskGR.cost_center;
}
}
})(current, previous);
Save your Business Rule.
This script checks if the category is Task_work for the task_card being inserted. Then, it looks up the related Task record using the GlideRecord API and fetches the cost_center from that record. If found, it updates the task_card's cost_center field to match before the record is inserted into the database.
PLEASE NOTE:
Replace 'task' with the actual name of the reference field in the task_card table that points to the related Task record, if it's named differently in your setup.
Ensure you have the necessary access rights to create a Business Rule and modify the task_card table.
Test this solution in a sub-production or test instance before deploying it in your production environment to avoid any unintended side effects.
Business Rules can significantly impact system performance. Ensure your conditions and scripts are optimized for efficiency.
To address the updated requirement where the Cost Center field should only be automatically filled from the Task record if it is initially empty, you can modify the Business Rule by adding a condition to check if the cost_center field on the task_card is empty before fetching and setting the value from the related Task record. You can follow the same steps as outlined previously for creating a Business Rule, with a slight adjustment to the script:
Steps:
Navigate to System Definition > Business Rules in your ServiceNow instance.
Click on the New button to create a new Business Rule.
Fill out the form as per the instructions given previously, with an emphasis on:
Name: A descriptive name, such as Update Cost Center from Task if Empty.
Table: Choose the task_card table.
Advanced: Ensure this checkbox is checked.
When to run: Opt for Before and Insert to ensure the rule runs before a new task_card is inserted. You may also want to apply this rule on Update if you wish for the Cost Center to be filled in the event it's emptied in an update operation and the rule's conditions are still met.
Filter Conditions: Though you can specify the category as Task_work here, for more complex logic, you might prefer handling that within the script. This way, you can have more control over checking if the cost_center field is empty.
In the Script field, modify the provided code snippet to include a check on the cost_center field:
javascript
(function executeRule(current, previous /*null when async*/) {
// Check if the category is 'Task_work' and the cost_center is empty
if (current.category == 'Task_work' && current.cost_center.nil()) {
// Reference the related task record
var taskGR = new GlideRecord('task');
if (taskGR.get(current.task)) { // Assuming 'task' is the reference field name to the task table
// Check if the task record has a cost_center defined
if (!taskGR.cost_center.nil()) {
// Set the Cost Center value from the related task record to the task_card record
current.cost_center = taskGR.cost_center;
}
}
}
})(current, previous);
Save your Business Rule.
This script now only updates the cost_center on the task_card if it's initially empty (current.cost_center.nil()), ensuring it doesn't overwrite any existing values in the cost_center field unless those conditions are met.
Additional Considerations:
Testing: Confirm the functionality in a sub-production instance before deploying it to production.
Performance: Ensure that the rule is optimized to not adversely affect system performance.
Accessibility: Verify that you have the necessary permissions to create and modify Business Rules and the target table (task_card).
By following these revised steps, you should be able to meet your requirement of conditionally filling the cost_center field on the task_card table based on the associated Task record, but only if it is empty at the time of insertion.
To achieve this functionality in ServiceNow, you can create a Business Rule on the "Assessment Instance" table that triggers when the state changes to "Completed". This Business Rule will then update the associated record(s) in your custom "F&F" table to set the "Interviews Conducted" field to "Yes". Here’s how you can set it up step-by-step:
Step 1: Identify the relationship between "Assessment Instance" and "F&F" Table
Before writing the Business Rule, you need to understand the relationship between the "Assessment Instance" table and your custom "F&F" table. Specifically, you need to identify how these tables are linked (e.g., through a reference field, a related list, etc.).
Step 2: Create a Business Rule
Navigate to System Definition > Business Rules in your ServiceNow instance.
Click on the New button to create a new Business Rule.
Fill out the form:
Name: Enter a descriptive name like Set Interview Conducted on F&F Completion.
Table: Select the "Assessment Instance" table from the dropdown list.
When to run:
Check the After checkbox, as you want this to run after the record has been updated.
Choose Update to have the script run when an assessment instance is updated.
Under Conditions, specify the script to run only when the record's state changes to "Completed". You can use the condition builder or script if it’s a complex condition.
Advanced:
Check the Advanced checkbox to write a script for your Business Rule.
Script:
javascript
(function executeRule(current, previous /*null when async*/) {
// Ensure the script runs only if the state is set to 'Completed'
if (current.state == 'completed' /* Use the appropriate value or field that represents "Completed" status */) {
// Fetch the related F&F record. Assuming 'assessment_instance' is the reference field in F&F pointing back to Assessment Instance
var ffGR = new GlideRecord('custom_ff_table'); // Replace 'custom_ff_table' with the actual table name of F&F
ffGR.addQuery('assessment_instance', current.sys_id); // Adjust 'assessment_instance' to the correct field name if needed
ffGR.query();
while (ffGR.next()) {
// Assuming 'interviews_conducted' is the field you want to set to 'Yes'
ffGR.interviews_conducted = 'yes'; // Adjust 'yes' if the choice value for "Yes" is different in your setup
ffGR.update();
}
}
})(current, previous);
Save the Business Rule.
Points to Note:
Replace 'completed', 'custom_ff_table', 'assessment_instance', and 'interviews_conducted' with the actual values from your instance. The choice value for "Yes" in the interviews_conducted field should correspond to the internal value stored in your choice list.
This script assumes a one-to-many relationship from "Assessment Instance" to "F&F", where multiple "F&F" records could relate back to a single "Assessment Instance".
Test this functionality thoroughly in a sub-production instance before moving it to production to ensure no unintended behaviors.
By following these steps, you are linking the completion of an Assessment Instance to the automatic updating of related records in your "F&F" table, streamlining what could otherwise be a manual process.