Function to find user titles in sys_user table that do not exist in another table

ABouland
Kilo Guru

Our firm is getting ready to roll out a large number of new user titles in Active Directory, where our SN user data comes from.  I want to be able to easily find user records that have a title that is not on the published list of acceptable titles (around 300 unique titles).  I've created a new table: Title List (u_title_list) with a single field 'u_title'  and I have imported all of the acceptable titles to this table.

I've managed to create script includes to do similar sorts of things to find users with duplicate employee numbers and then referenced the script include in a report but in trying to construct something similar here, don't seem to be getting there. 


Currently just attempting to do this in a background script to see if I am returning anything.

Current script:

function checkTitle() {
var grUSER = new GlideRecord('sys_user'); 
var grTITLE = new GlideRecord('u_title_list');

grTITLE.query();

while(grTITLE.next()){    

      // grUSER.initialize();
       grUSER.addQuery('title', grTITLE.u_title);   

       grUSER.query();

      if(!grUSER.next()){
                       gs.print("Did not Find: " + grUSER.title);
}          
}
}

Not getting any results though.  Have tried a few variations of the above but no results with any of them.

Script include was simply the method I've used to do something similar before but am fine with any efficient method to get a list of outliers.  Any suggestions are much appreciated.

 

Thanks,

Andrew

1 ACCEPTED SOLUTION

Jon Barnes
Kilo Sage

I would use a database view with a left outer join for this. Basic steps for this: Create a new dB view.

Add sys_user table first and give it an order of 100. Set the prefix as ”usr”.

Add u_title_list table second with an order of 200. Make this one a left join by checking the left join box. If it isn’t on the form you can set it from the list view. Set the prefix on this table as “ttl”.

Set the where clause on the u_title_list table record as. ttl_u_title = usr_title.

Now, once that is added, you have created your functioning DB view. If you go to the new view and click Try It in the related links, you will see the tables joined together.

Finally you can use the filters to add a query like this: Title (ttl_u_title) Is Empty AND Title (usr_title) Is Not Empty. Let me know if that works for you.

View solution in original post

6 REPLIES 6

If you want to go the script route, I think Abhinay’s reply should address that. Thanks!

Abhinay Erra
Giga Sage

Here you go. You are querying the opposite way

 

function checkTitle() {
var grUSER = new GlideRecord('sys_user');
grUSER.query();
while(grUSER.next()){
var grTITLE = new GlideRecord('u_title_list');
grTITLE.addQuery('u_title', grUSER.title);
grTITLE.query();
if(!grTITLE.hasNext()){
gs.print("Did not Find: " + grUSER.title+" for "+grUSER.name);
}
}
}