How to make a business rule run retroactively?

Brandon8
Kilo Expert

I created a business rule to updated a created by username. It is very simple but only runs on update and created. Is there away to make it go back an update all cases? Thanks!

 

find_real_file.png

1 ACCEPTED SOLUTION

Brian Bouchard
Mega Sage

Hi Brandon - You can't run a business rule retroactively, but you could run either a scheduled job to determine which records need updating and then do the update, or you could run a fix script to do similar.

View solution in original post

8 REPLIES 8

Brian Bouchard
Mega Sage

You would want to query the table in question to get only those records you want to update, then for each of those records apply your business rule logic.

I tried this code, do you know why it didnt work? I told it to grab cases that are active. I want it to update the field called "created by (display name)"... it did nothing though. 

 

var rec = new GlideRecord('case');
rec.query();
while (rec.next()) {
rec.active = false;
rec.update ('createdbydisplayname)');
}

Hi Brandon - I'm not familiar with the case tables, but I suspect "case" isn't the actual name of the table, but just the display name.  You can usually tell the "real" table name when looking at a list of the records by looking at your URL.  It will have something like: "/nav_to.do?uri=%2Fchange_request_list.do"   >> where change_request is the table name I'm looking at.

So, assuming your table name is case (check the URL when looking at a list of cases), and your goal is to set the Created By field to the Name field of the User table for every record in the case table, you would do something like the following. (This is completely untested, since I don't have access to the case table in my environment)

 

//get all the case table records where the case is still active
var caseRec = new GlideRecord('case');
caseRec.addQuery('active', 'true');
caseRec.query();
while (caseRec.next()) {
	
	//foreach case, find the user record where the User ID matches the Created By on the case
	var userRec = new GlideRecord('sys_user');
	userRec.addQuery('user_name', caseRec.sys_created_by);
	userRec.query();
	if (userRec.next()) {
		//set the created by field on the case = the name field of the User
		caseRec.sys_created_by=userRec.name;
		caseRec.update();
	}
	
}

Brandon8
Kilo Expert

Ah, so a custom code. I am not a coder, nor do we have one on staff. I am more of an admin and was trying to fix it from the business rule side. I think you have the right answer, I just need to learn to right that script lol.