How to prevent Business Rule from inserting duplicate user records on related list?

DP10
Tera Contributor

Can anyone let me know how I can have my after update business rule stop inserting duplicate records into my related list? I just need one record in the related list per user. Here is my business rule: 

 

 

 And here is the issue. This is the related list (duplicates appeared for the other two users as well but I deleted them):

 

I have a scheduled job that updates "Last reminded" and a few other fields. I run the scheduled job after the Business Rule inserts a record. The job updates the fields it needs to update but then the BR inserts the same user again. How can I stop this from happening?

1 ACCEPTED SOLUTION

//See  the extra filter in the user exists check

function insertUser(sys_id) 
{
	var gdt = new GlideDateTime(current.trips_return_date);
	gdt.addDaysLocalTime(10);
	
	//Query if there is already a user for this scc with this due date.
	var grUser = new GlideRecord('x_g_dnf3_fedsim_scc_ref_list');
	grUser.addQuery('user', current.trips_user);
	grUser.addQuery('sample', sys_id);
	grUser.addQuery('due_date', gdt.getDate()); //check due date
	grUser.query();
	
	//insert only if there is not one already
	if(!grUser.next()){
		var grUserNew = new GlideRecord('x_g_dnf3_fedsim_scc_ref_list');
		grUserNew.initialize();
		grUserNew.user = current.trips_user;
		grUserNew.sample = sys_id;
		grUserNew.due_date = gdt.getDate();
		grUserNew.insert();
	}        
}
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

9 REPLIES 9

-O-
Kilo Patron
Kilo Patron

Can you show the conditions for the BR? It may be that those are at fault.

DP10
Tera Contributor
Yes condition is “total_cost > 0.. this condition is important though. It was created because our flow that populates data on the trip report is a little delayed. Any ideas on changing that condition?

DP10
Tera Contributor
But you are right that is the issue

vkachineni
Kilo Sage
Kilo Sage
function insertUser(sys_id) 
{
	var gdt = new GlideDateTime(current.trips_return_date);
	gdt.addDaysLocalTime(10);
	
	//Query if there is already a user for this scc.
	var grUser = new GlideRecord('x_g_dnf3_fedsim_scc_ref_list');
	grUser.addQuery('user', current.trips_user);
	grUser.addQuery('sample', sys_id);
	grUser.query();
	
	//insert only if there is not one already
	if(!grUser.next()){
		var grUserNew = new GlideRecord('x_g_dnf3_fedsim_scc_ref_list');
		grUserNew.initialize();
		grUserNew.user = current.trips_user;
		grUserNew.sample = sys_id;
		grUserNew.due_date = gdt.getDate();
		grUserNew.insert();
	}        
}
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022