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

AmishRanjan1
Tera Contributor

Hi Cory,

You may use below code in before insert/update business rule:

 

var locationRec = new GlideRecord("cmn_location");

if(locationRec.get("street",current.street)){

current.location = locationRec.sys_id;
 
}
 

BR,

Amish

Musab Rasheed
Tera Sage
Tera Sage

Hello @Cory Hitchings ,

Please write Script include instead of glide record as it is not a good practice to use gliderecord due to performance issue so use below script include and before BR to make it work, please find sample code and tweak it accordingly. Mark my answer as correct or hit like based on impact.

Before business rule: 

var gr =   new LocationName();

var loc = gr.getLocation(current.street);

current.location = loc;

 

Script include : Name : LocationName

var LocationName = Class.create();

LocationName.prototype = {

        initialize: function() {

        },

 

        getLocation: function(streetname) {

 

            var tableID = '';

            var grTable = new GlideRecord('cmn_location');

            grTable.addQuery('street', streetname);

            grTable.query();

             if (grTable.next()) {

                      tableID = grTable.sys_id;

            }

              return tableID;

           

        },

        type: 'LocationName'

 };

 

 

Please hit like and mark my response as correct if that helps
Regards,
Musab

shloke04
Kilo Patron

Hi @Cory Hitchings 

I see you have a street field on User Table and based on the street value you want to populate the Location field.

Please write a Before Update Business Rule on User Table and use the script below:

BR Details:

Table Name : User (sys_user)

Before Update

Condition: Street Changes

Script:

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

find_real_file.png

So when ever the street on User Table change it will fetch the respective location and update it for you.

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

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Cory Hitchings ,

 

You can use the below Business Rule.  

When to run :- Before Update

Condition : Street Changes

 

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

// Add your code here
var grLocation = new GlideRecord("cmn_location");
grLocation.addQuery("street", current.getValue('street'));
grLocation.query();
if (grLocation.next()) {
gs.addInfoMessage('grLocation.getValue("name")'+grLocation.getValue('name'));
current.setDisplayValue('location',grLocation.getValue('name'));

}


})(current, previous);   

 

Please mark my answer as helpful/correct if it resolves your query.

 

Regards,

Gunjan Kiratkar

Consultant - ServiceNow, Cloudaction

Rising Star 2022


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