Set 'Location' on sys_user record from 'current.street address' reference

Cory Hitchings
Giga Guru

Hi all, 

Wondering if you may be able to help out here?  I am looking to create a Business Rule on  the sys_user table to auto populate a users Location depending on their street address. 

for example; if the address is Suite 4 16 Ave SW,  It will populate the name of the location to the location field on the sys_user table.  

find_real_file.png

 

find_real_file.png

 

I've tried the following script but doesn't seem to take affect: 

 

(function executeRule(current, previous /*null when async*/) {

var nm = current.street.getDisplayValue();
	
current.location = nm;
})(current, previous);


find_real_file.png

 

 

Any help would be super appreciated.  Thank you! 

7 REPLIES 7

Hi @Gunjan Kiratkar 

Using current.update in a After Business Rule is not at all a good practice and will suggest not to mention a bad practice approach.

I will suggest both you and @Cory Hitchings  to go with a Before Update Business Rule and use the script as suggested above.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Sure @shloke04 ,

 

I will update the above script.

Thanks for letting me know.

 

Regards,

Gunjan


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

@Gunjan Kiratkar Script is already updated and shared on this post above for reference. I am posting it again:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var getStreet = current.street.toString();
	var getLocation = fetchLocation(getStreet);
	current.location = getLocation; // This will set the location field based on Street
	
	function fetchLocation(Userstreet){
		var gr = new GlideRecord('cmn_location');
		gr.addQuery('street',Userstreet);
		gr.query();
		if(gr.next()){
			return gr.sys_id.toString();
		}
	}
		
	

})(current, previous);

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke