Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Can you please look into The above scenario where I wrote an after insert/Update BR on 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.

Tai Vu
Kilo Patron
Kilo Patron

Hi @Sam Hanson 

Let's give my adjustment below a try.

 

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

	var grTask = current.task_work.getRefRecord();
	if(grTask.isValidRecord()){
		current.u_cost_center = grTask.getValue('u_cost_center');
	}

})(current, previous);

 

 

And also try to avoid using update() inside a business rule.

Prevent Recursive Business Rules

Do not use current.update() in a Business Rule script. The update() method triggers Business Rules to run on the same table for insert and update operations, potentially leading to a Business Rule calling itself over and over.

 

Cheers,

Tai Vu

I tried your script too but its doing exactly the same. Cost center is being populated on after I save the record(time_card). It has to update the cost center while creating itself.

@Sam Hanson 

 

If you want this to be shown on the form as soon as you select the task than you should use a combination of Client Script onChange for Task field with a GlideAjax call to a client callable script include.

 

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.