Update sys_user field to true when source contains a specific OU

tlindof
Kilo Expert

I have an OU call consultant that we store consultant ad accounts in. I have also created a Consultant true/false field on the sys_user table. I am trying to mark the consultant field to true when the source field contains OU=Consultant. I have crated this business rule to make the change.

 

Before

        insert/update

 

var target = new GlideRecord("sys_user");

target.addQuery("consultant","CONTAINS","OU=Consultant");

target.query();

while (target.next()){

target.consultant = "true";

target.update();

}

 

Am I missing something?

1 ACCEPTED SOLUTION

I forgot to wrap it in a function.



checkConsultant();



function checkConsultant(){



if(current.u_consultant)  


return;  




if(!current.u_consultant)  


current.u_consultant = true;  


}




That error goes away. However I would *really* suggest taking care of this in the LDAP transform map if at all possible.


View solution in original post

5 REPLIES 5

prdelong
Kilo Guru

Consultant is a custom field I'm guessing, so it should be 'target.u_consultant = true;' You shouldn't need to do a glide record query in the first place though, as the system knows which user record is updating/inserting.   If you wanted to keep the business rule, it could look something like this:



When: Before


Insert/Update: True



Condition: current.source.toLowerCase().indexOf('consultant') > -1



if(current.u_consultant)


return;



if(!current.u_consultant)


current.u_consultant = true;




However I would look to populate that via the LDAP import. Just add this script:



var source = source.u_source.toString().toLowerCase();



//can also use indexOf('consultant')


if (source.indexOf('ou=consultant') > -1){


target.u_consultant = TRUE;


}



If you want to avoid adding that as a script to run every time the import insert/updates, you can just set up a new LDAP OU definition and transform map for the Consultants OU.


Thank you for the response! I used the code you provided for the business rule.


I have this in the condition field: current.source.toLowerCase().indexOf('consultant') > -1


I have this in the script field:



if(current.u_consultant)  


return;  


 


if(!current.u_consultant)  


current.u_consultant = true;  





I am getting this error when saving.


Error MessageJavaScript parse error at line (2) column (6) problem = invalid return






I forgot to wrap it in a function.



checkConsultant();



function checkConsultant(){



if(current.u_consultant)  


return;  




if(!current.u_consultant)  


current.u_consultant = true;  


}




That error goes away. However I would *really* suggest taking care of this in the LDAP transform map if at all possible.


That worked thank you.



I agree that this should be running on the ldap transform map. I was hoping the business rule could address the current records that have already been through the map.