Map custom field to location with background script

Brian S5
Kilo Sage

Background: We currently use Azure SSO. We used to use LDAP with mappings and a script to set the location based on the source path of the user. 

Something along the lines of if (source.indexOf('OU=Something') > 0){
location='4e54ff271b840e50d5699938b04bcb70'; //Something location

 

But with Azure the same setup doesn't work anymore so I created a new field on sys_user called u_opdn. I am custom mapping the Azure on premise device name to this new field u_opdn which resembles the same structure as source in our LDAP synchronization. 

 

This is the beginning of the background script Im trying to run, and it doesnt give any errors. Its finishes with 

*** Script: Processing user: Test.User@example.com, u_opdn: CN=User\, Test,OU=xxx,OU=xxx,OU=xx,DC=xx,DC=xxx,DC=local, location: e794ad7437d0200044e0bfc8bcbe5d7e

but that end user's location wasn't changed with the sys_id reference. 

 

Is there anyone out there (Ive tried chatGPT for 2 days, full disclosure) that can help me write a background script to correct these locations ? I can provide the start of my script, its pretty long parsing through all the OU Container names but all the location sys_id's are corrrect. u_opdn is a string field, location is a reference field to cmn_location

 

// Get the sys_user table
var userGR = new GlideRecord('sys_user');
userGR.addNotNullQuery('u_opdn'); // Filter out users where u_opdn is not empty
userGR.query();

// Iterate over each user record
while (userGR.next()) {
    var u_opdn = userGR.getValue('u_opdn');
    var l = userGR.getValue('location');

// Log user information for debugging
    gs.info('Processing user: ' + userGR.getValue('user_name') + ', u_opdn: ' + u_opdn + ', location: ' + l);

if (u_opdn){
if (u_opdn.indexOf('dc=xx,dc=xxx,dc=xxx') > 0) {
    if (u_opdn.indexOf('OU=OU1') > 0) {
 gs.log('Updating location for user: ' + userGR.getValue('user_name') + ' to: ' + userGR.location);
        userGR.location ='4e54ff271b840e50d5699938b04bcb70';
    } else if (u_opdn.indexOf('OU=OU2') > 0) {
 gs.log('Updating location for user: ' + userGR.getValue('user_name') + ' to: ' + userGR.location);
        userGR.location ='8c39234f1b8f651001300dc1604bcb11';

 

 

Please help, and thank you in advance. 

4 REPLIES 4

AshishKM
Kilo Patron
Kilo Patron

Hi @Brian S5 , 

Please share the complete script for debug.

 

-Thanks,
AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Here is the entire code, where OU containers were replaced with x's for security

 

 

// Get the sys_user table
var userGR = new GlideRecord('sys_user');
userGR.addNotNullQuery('u_opdn'); // Filter out users where u_opdn is not empty
userGR.query();

// Iterate over each user record
while (userGR.next()) {
    var u_opdn = userGR.getValue('u_opdn');
    var l = userGR.getValue('location');

// Log user information for debugging
    gs.log('Processing user: ' + userGR.getValue('user_name') + ', u_opdn: ' + u_opdn + ', location: ' + l);

if (u_opdn){
if (u_opdn.indexOf('dc=xx,dc=xx,dc=local') > 0) {
    if (u_opdn.indexOf('OU=xxxx') > 0) {
        userGR.location ='4e54ff271b840e50d5699938b04bcb70';
    } else if (u_opdn.indexOf('OU=xxxx') > 0) {
        userGR.location ='8c39234f1b8f651001300dc1604bcb11';
    } else if (u_opdn.indexOf('OU=xxxx') > 0) {
        userGR.location ='d28bbc956f099500cc43abcf9f3ee42c';
    } else if (u_opdn.indexOf('OU=xxxx') > 0) {
        userGR.location ='3969e7be6fe1b9008b8052a03f3ee436';
    } else if (u_opdn.indexOf('OU=xxxx') > 0) {
        userGR.location ='43dbf8956f099500cc43abcf9f3ee436';
    } else if (u_opdn.indexOf('OU=xxxx') > 0) {
        userGR.location ='aac2b9a0db3927c0df1ac9fb0b9619e5';
    }
      else if (u_opdn.indexOf('OU=xxxx') > 0){
	userGR.location='96cc74d56f099500cc43abcf9f3ee4be'; 
	}
     else if (u_opdn.indexOf('OU=xxxx') > 0){
	userGR.location='4eecb0956f099500cc43abcf9f3ee40f'; 
	}
	else if (u_opdn.indexOf('OU=xxxx') > 0){
	userGR.location='eb6db4d56f099500cc43abcf9f3ee498'; 
	}
	else if (u_opdn.indexOf('OU=xxxx') > 0){
	userGR.location='a68df0d56f099500cc43abcf9f3ee478';
	}
	else if (u_opdn.indexOf('OU=xxxx') > 0){
	userGR.location='b19df0d56f099500cc43abcf9f3ee47d';
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='9dadbc956f099500cc43abcf9f3ee4e2';
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='e0bd3c956f099500cc43abcf9f3ee4cb'; 
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='308ba7fe6fe1b9008b8052a03f3ee417'; 
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='ad20211c6f393d00cc43abcf9f3ee453'; 	
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='4a9b27fe6fe1b9008b8052a03f3ee4fc'; 	
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='c400211c6f393d00cc43abcf9f3ee427'; 	
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0 && u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='eacba3fe6fe1b9008b8052a03f3ee4ca'; 
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0 && u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='e5db27fe6fe1b9008b8052a03f3ee4b4'; 
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0 && u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='d19bbfbb1b27f19001300dc1604bcbaf'; 
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0 && u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='3cfb67fe6fe1b9008b8052a03f3ee4b3'; 
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0 && u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='302c6bfe6fe1b9008b8052a03f3ee4fc'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0 && u_opdn.indexOf('OU= xxxxx') > 0){
	userGR.location='a14c6bfe6fe1b9008b8052a03f3ee40a'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0 && u_opdn.indexOf('OU= xxxxx') > 0){
	userGR.location='302c6bfe6fe1b9008b8052a03f3ee4fc'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
	userGR.location='4007f3f26f25b9008b8052a03f3ee4d4'; 
	}
        else if (u_opdn.indexOf('OU=xxxxx') > 0){
	userGR.location='1913a8916f099500cc43abcf9f3ee4ef'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
	userGR.location='1a3c49b037d0200044e0bfc8bcbe5dbd'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
	userGR.location='f04bf0d56f099500cc43abcf9f3ee40b'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
	userGR.location='9e3c34d56f099500cc43abcf9f3ee4ea'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
	userGR.location='4cabbc956f099500cc43abcf9f3ee432'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
	userGR.location='54bb34d56f099500cc43abcf9f3ee40c'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
	userGR.location='bc2b263d1317d200ad73bd122244b0cf'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0 || u_opdn.indexOf(‘OU=xxxxx') > 0){
	userGR.location='bffbb4956f099500cc43abcf9f3ee461'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='749cb8956f099500cc43abcf9f3ee4d5'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='6ce5a1a41b7d3450e8fbc848624bcb4e'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='5c94956713f43600ad73bd122244b047'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='963d34d56f099500cc43abcf9f3ee469'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='435db4d56f099500cc43abcf9f3ee493'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='fd1d74d56f099500cc43abcf9f3ee436'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='fd2d34d56f099500cc43abcf9f3ee462'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='a0dd74d56f099500cc43abcf9f3ee43c'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='f4edf4d56f099500cc43abcf9f3ee482'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='d45c74956f099500cc43abcf9f3ee44f'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='77fdbc956f099500cc43abcf9f3ee446'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='530ebc956f099500cc43abcf9f3ee44b'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='6b30b5e7dbd0e01076a464b4da96196d'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='9766e9a41b7d3450e8fbc848624bcb96'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='312ef4d56f099500cc43abcf9f3ee4ca'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='4879e1f16f0d9500cc43abcf9f3ee49b'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='76ccc5976f0df1008b8052a03f3ee40d'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='a44ef4d56f099500cc43abcf9f3ee472'; 
}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='bf95527b0fcb520071ecdb0be1050eb7'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='e15ca5f56fa8d100cc43abcf9f3ee4de'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0 && l==''){
		userGR.location='7979c1056f8d3100cc43abcf9f3ee403'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location=l; 
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0 && u_opdn.indexOf('OU=xxxxx') > 0){
		userGR.location='8e3e85f037d0200044e0bfc8bcbe5d14'; 
	}
	else if (u_opdn.indexOf('OU=xxxxx') > 0 && u_opdn.indexOf('OU=xxxxx') > 0){
		userGR.location='6cb5615c6f393d00cc43abcf9f3ee47c'; 
    }
}
if (u_opdn.indexOf('dc=xx,dc=xx,dc=local') > 0) {
    if (u_opdn.indexOf('OU= xxxxx') > 0) {
        userGR.location = 'e0bd3c956f099500cc43abcf9f3ee4cb';
    } else if (u_opdn.indexOf('OU= xxxxx') > 0) {
        userGR.location = 'a44ef4d56f099500cc43abcf9f3ee472';
    } else if (u_opdn.indexOf('OU= xxxxx') > 0) {
        userGR.location = 'a68df0d56f099500cc43abcf9f3ee478';
    }
      else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='b19df0d56f099500cc43abcf9f3ee47d'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='9e3c34d56f099500cc43abcf9f3ee4ea'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0 || u_opdn.indexOf('OU=xxxxx') > 0){
		userGR.location='bffbb4956f099500cc43abcf9f3ee461'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='fd2d34d56f099500cc43abcf9f3ee462'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='a0dd74d56f099500cc43abcf9f3ee43c'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='312ef4d56f099500cc43abcf9f3ee4ca'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='148242d56fcd31008b8052a03f3ee4d5'; 
	}
	else 
		userGR.location='e15ca5f56fa8d100cc43abcf9f3ee4de'; 
	}
if (u_opdn.indexOf('dc=xx,dc=xx,dc=local') > 0) {
    if (u_opdn.indexOf('OU= xxxxx') > 0) {
        userGR.location = '123c49b037d0200044e0bfc8bcbe5dad';
    } else if (u_opdn.indexOf('OU= xxxxx') > 0) {
        userGR.location = '3c262e4a6f2cd100cc43abcf9f3ee461';
    } else if (u_opdn.indexOf('OU= xxxxx') > 0) {
        userGR.location = 'e0262e4a6f2cd100cc43abcf9f3ee44e';
    }
      else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='a25ce5f56fa8d100cc43abcf9f3ee464'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='18de3a046ff115008b8052a03f3ee410'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='0c26ea4a6f2cd100cc43abcf9f3ee4da'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='d8262e4a6f2cd100cc43abcf9f3ee414'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='4026ea4a6f2cd100cc43abcf9f3ee4cf'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='bc262e4a6f2cd100cc43abcf9f3ee465'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='a23c49b037d0200044e0bfc8bcbe5dda'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='e0cef2446ff115008b8052a03f3ee49d'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='35266e4a6f2cd100cc43abcf9f3ee451'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='209eb2446ff115008b8052a03f3ee4ea';
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='86266e4a6f2cd100cc43abcf9f3ee45a'; 
	}
	else if (u_opdn.indexOf('OU= xxxxx') > 0){
		userGR.location='8a266e4a6f2cd100cc43abcf9f3ee454'; 
	}
	else 
		userGR.location='14b96db16f0d9500cc43abcf9f3ee4eb';
	}
userGR.update();
}

}

 

 

Hi Ashish,

 

Were you able to take a look at the code provided ? Any help would be appreciated. 

Hi @Brian S5 , 

Checked the code, found one typo error as below , replace this inverted quote with single quote. The begin and closing quote are not same. 

 

AshishKMishra_0-1707862199309.png

 

Rest, I checked all open and close parenthesis, all are well formed.

 

You are getting the gs.log() [ line no :12 ] output because it's before the while loop and typo error at below this line. Place this gs.log() line just before the userGR.update(); then run the script again, compilation error expected.

 

AshishKMishra_1-1707863223334.png

 

 -Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution