How can I run a business rule retro-actively?

taylor11
Giga Contributor

Hi all,

I have a new business rule that is working beautifully for mapping data from one table to another. This is working on insert/update and is currently working on new or updated records. However, I would like to apply this business rule to existing records that are already in the system. What is the best way to do this? 

Thank you for your help! 

7 REPLIES 7

vinothkumar
Tera Guru

If you want to update the existing record, you can go with one time scheduled job.

 

Business rule typically run only whenever an server side action take place

Thank you, I will try this! Would I just take the same script that is in the business rule and use that for the scheduled job?

Yes, mostly similar. It would be better if you share the BR code.

 

If you are using current in your BR, you may to create a gliderecord query and then do an update.

Sure thing, here is the code in the business rule:

(function executeRule(current, previous /*null when async*/) {
		
	// If Sys_user field is blank, i.e. HR profile was not matched to sys_user
	if(current.user==''){
		
		// Search for matching sys_user record by Colleague ID
		var sysUsrQry = new GlideRecord('sys_user');
		sysUsrQry.addQuery("u_colleague_id", current.u_colleague_id);
		sysUsrQry.query();
		
		if(sysUsrQry.next()) {
			
			// If found, update match found sys_user record to current HR profile
			current.user=sysUsrQry.sys_id;
		}
		
		else {
				
			// Otherwise, create a stub of a sys_user record and match to current HR profile
			var sys_user = new GlideRecord('sys_user');
			sys_user.initialize();
			sys_user.first_name = current.u_preferred_first_name;
			sys_user.last_name = current.u_preferred_last_name;
			sys_user.u_preferred_first_name = current.u_preferred_first_name;
			sys_user.u_preferred_last_name = current.u_preferred_last_name;
			sys_user.u_colleague_id = current.u_colleague_id;
			sys_user.u_workday_id = current.employee_number;
			sys_user.u_personal_email = current.personal_email;
			sys_user.u_supervisory_organization = current.u_supervisory_organization;
			sys_user.location = current.location;
			sys_user.title = current.position.getDisplayValue();
			sys_user.city = current.city;
			sys_user.state = current.state;
			sys_user.u_active = current.u_active;
			sys_user.u_pay_rate_type = current.u_pay_rate_type;
			current.user = sys_user.insert();			
		}
	}
})(current, previous);