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

Stewe Lundin
Mega Guru

Hi!
Can you show the rules form the  BR.
The u_office feild is that a reference to the sys_user table  ? 



Running on the Incident table

 

When to run

When - Before

Order - 200

Insert - checked

Updated -checked

Filter conditions - Contact type is one of Email, Self-service

 

Advance

Script in original post.

 

u_office is a string value field on the sys_user table.

After you questioned that u_office i questioned myself as to why i put that there...

I was looking for the wrong thing there. should of been looking for the caller_id in that first query. Changing from

grUser1.addQuery('sys_id',current.u_office);

to

grUser1.addQuery('sys_id',current.caller_id);

fixed the issue. Went code blind. Thanks for the help.

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);