Copy the Field from the task table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 01:41 PM
Hi folks,
Please find the below screenshot
Now My reequrement 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).
For this Purpose I created a BR as following but itsnt working.
with following script.
But it's not working, can anyone gimme some pointers please.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 09:02 PM
Hi @Sam Hanson
Your business rule is almost correct, you just need some modifications in your business rule.
1. Navigate to System Definition > Business Rules in your ServiceNow instance.
2. Click on the New button to create a new Business Rule.
3. Fill out the form as follows:
- Name: Give your Business Rule a name
- 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.
// 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;
}
}
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2024 06:57 AM - edited 03-22-2024 08:39 AM
modified according to your script, but Cost center is being populated on after I save the record(time_card). It has to update the cost center while creating itself.
can you let me know what nned to be changed?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 06:57 AM
For now this gives me what is needed but I want cost_center on the time card to be inserted when people inserts the cost_center field on any records in the task table too.
So, I proceeded and wrote another BR on the task table.
Table- Task
When to Run
AFTER - Update
cost center is not empty
Advanced
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 08:30 PM
Hi @Sam Hanson
To ensure the Cost Center is populated while creating a time_card record itself, and not after saving, you’ll need to tweak your approach slightly.
Table: time_card (or your custom time card table name if it’s different)
When to run:
- When: before
- Insert: true
4. Filter Conditions: Leave Blank
// Ensure the category is 'Task_work' and there’s a task selected
if (current.category == 'Task_work' && current.task) {
var taskGR = new GlideRecord('task');
if (taskGR.get(current.task)) {
// Set the Cost Center value from the related task record to the time_card record
current.cost_center = taskGR.cost_center;
}
}
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning
Thanks & Regards
Deepak Sharma