You need to sign in to do that
Don't have an account?
Afzaal 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;
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;
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?
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.