Copy the Field from the task table.

Sam Hanson
Tera Contributor

Hi folks,

Please find the below screenshot

SamManguson_0-1711052999310.png

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.

SamManguson_1-1711053469557.png

with following script.

SamManguson_2-1711053544913.png

(function executeRule(current, previous /*null when async*/ ) {

// Add your code here
var gr = GlideRecord('time_card');
gr.addQuery('category', task_work);
gr.query();
if (gr.next()) {
gr.u_cost_center = task.u_cost_center;
gr.update();
}
})(current, previous);
 

 

But it's not working, can anyone gimme some pointers please.

 

 

 

9 REPLIES 9

Deepak Shaerma
Kilo Sage

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


 

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.

SamManguson_0-1711115797847.png

SamManguson_0-1711121933862.png

can you let me know what nned to be changed?

 
 
 
 
 

Hi @Deepak Shaerma 

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

(function executeRule(current, previous /*null when async*/ ) {

// Add your code here
var GDOT = current.u_cost_center;
var gr = GlideRecord('time_card');
gr.addQuery('task', current.getUniqueValue());
gr.query();
if (gr.next()) {
gr.u_cost_center = GDOT;
gr.update();
}
})(current, previous);
 
Can you please let me know if I am following the right path.

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