how to use business rule to set reference field based on string value

James Roberts
Giga Contributor

Hi All

 

Got myself stuck again, this time using a business rule. I have a script include doing something similar already so based this script on that but it does appear to be working.

We need to populate the incident record with something from the sys_users table but its a string on the sys_users table and a reference field on the incident.

The script is meant to get the string value from the u_office field on the sys_users table and then compare it to the name column on the custom u_practice table that the incident u_affected_practice field would reference, it grabs the sys_id and then is supposed to populate the affected practice field before inserting the record.

It doesn't work though and i'm not sure where I've gone wrong. I might be using "current" incorrectly or not understanding exactly what it would be containing. Its the first business rule I've tried to script.

(function executeRule(current, previous /*null when async*/) {
	var grUser1 = new GlideRecord('sys_user');
	grUser1.addQuery('sys_id',current.u_office);
	grUser1.query();
	
	if(grUser1.next()){
		var Ostring = grUser1.u_office;
		var groffice = new GlideRecord('u_practice');
		groffice.addQuery('u_name',Ostring);
		groffice.query();

		if(groffice.next()){
			var off1 = groffice.sys_id;
			current.u_affected_practice=off1;
 }
 }

})(current, previous);
1 ACCEPTED SOLUTION

Great!
Thought it looked a bit off, hence the question. 

 


Cleaned up the code a bit. 

Checkout GlideRecord.get(), it's relay useful. 

(function executeRule(current, previous /*null when async*/) {
	var grUser1 = new GlideRecord('sys_user');
	if(grUser1.get('sys_id',current.caller_id)){
		var Ostring = grUser1.u_office;
		var groffice = new GlideRecord('u_practice');
		if(groffice.get('u_name',Ostring){
			var off1 = groffice.sys_id;
			current.u_affected_practice=off1;
 }
 }

})(current, previous);

View solution in original post

5 REPLIES 5

Thanks

 

It was the first bit of code I mostly wrote myself to be honest (well, the working script include that this business rule script was based off). Genuinely was amazed that I got the script include working with having to post on here asking for help.

 

I'll have a read up on the GlideRecord.get()