function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Afzaal HassanAfzaal Hassan 

Anonymous Apex class to update object field with User name

Hello
I have an object called Service Territory(ST). The ST has a Name field ad it also has a Lookup field called User (User__c) which looks up to the User object. I just realized for all our ST records, the User__c field is blank. So I decided to run a simple code in anonymous apex window to update these fields. Basically, I want the User__c field to be the value of the User name. So in other words, if the ST name is John Doe, then it will look up to the John Doe User and update User__c with the John Doe record value. This seems simple but for some reason my code is not running. Can somone one please help?
Map<Id, ServiceTerritory> stMap = new Map<Id,ServiceTerritory>([select Id, Name from ServiceTerritory where User__c = null]);

List<User> userList = [select Id, Name from User where Name in :stMap.values()];
Set<ServiceTerritory> srToUpdate = new Set<ServiceTerritory>();
ServiceTerritory sname;
for (User u : userList){
    sname = stMap.get(u.Name);
    for(st: stMap){
    st.User__c = sname
    srToUpdate.add(st);    
    }


List<ServiceTerritory> dumbList = new List<Serviceterritory>();
dumbList.addAll(srToUpdate);
update dumbList;
Tad Aalgaard 3Tad Aalgaard 3
This won't work.

stMap.get(u.Name);

The key is a Salesforce record Id, not a text name.  You need to iterate through the stMap results and map the name to the user record Id.

But you will have a problem with your approach.  What if you have two John Does?  Which one do you use if you're using text to match to a user record?

 
Afzaal HassanAfzaal Hassan
@tad aalgard do you have code samples for what you are saying? I agree with you that id needs to be sued but not sure how to sue and what exactly i need to loop through, users or the service territory?
 
Tad Aalgaard 3Tad Aalgaard 3
Map<String,Id> emailToLeadId = new Map<String,Id>();  // map email to Lead Id

List<Lead> leadList= [select Id, email from Lead];
for(Lead l : leadList){
   emailToLeadId.put(l.email,l.Id);
}
Warning, the above is coding on the fly and was not tested, but it the basics of what you need to do. It does basically the same thing but with email instead of name.  I could have fixed your code but I think it's better that you take and example and re-engineer it for yourself as it's a better way of learning.
Afzaal HassanAfzaal Hassan
 @Tad Aalgaard I'm sorry but this was not exactly what I was thinking of. So the service territory has a name field and a User__c field. The user__c is a lookup to User. The User__c would be the id corresponding to the ST name. So i need to update User__c = user.id somehow where id is the id for whatever st.name is (so if the st name is john doe, there will definitely be a user record correcpsonding to st name)
Afzaal HassanAfzaal Hassan
@Tad aalgaard I didnt understand your explanation 
 
Tad Aalgaard 3Tad Aalgaard 3
@Tad Aalgaard I'm sorry but this was not exactly what I was thinking of. So the service territory has a name field and a User__c field. The user__c is a lookup to User. The User__c would be the id corresponding to the ST name. So i need to update User__c = user.id somehow where id is the id for whatever st.name is (so if the st name is john doe, there will definitely be a user record correcpsonding to st name)
What you posted is what I understood to be what you wanted.

If you use the example code I provided and put it into your code and just replace the object (Lead) and field name (Email) with ServiceTerritory and Name it will work.  emailToLeadId is the equivalent of your stMap variable.

You should refactor my naming to make it make sense to whomever looks at your code later.


 
Afzaal HassanAfzaal Hassan
Thanks @Tad Aaalgaard