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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2022 03:40 PM
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.
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);
Any help would be super appreciated. Thank you!
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2022 06:36 PM
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)){
BR,
Amish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2022 02:17 AM
Hello
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'
};
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2022 04:11 AM
Hi
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);
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
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2022 05:32 AM
Hi
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