i want to update owner id field of custom object(Consultant_Weekly_Stats__c) from the custom text field(Consultant__c).when i add owner name in the text field then owner id field is changed, i try with this code, but getting error that "consultantSet variable does not exist". Please help me out. Thanks in advance.
trigger CopyOwnerNameConsultant on Consultant_Weekly_Stats__c(after insert)
{
set<string> userNames = new Set<string>();
Map<ID, ID> UserMap = new Map<ID, ID>();
list<Consultant_Weekly_Stats__c> consultantSet= new list<Consultant_Weekly_Stats__c>();
for(Consultant_Weekly_Stats__c obj : trigger.new){
consultantSet.add(obj.Consultant__c);
}
List<User> users = new List<User>([Select id, Name from user where Name =:consultantSet]);
for(User obj : users){
UserMap.put(obj.name,users.id);
}
List<Consultant_Weekly_Stats__c> cc= new list<Consultant_Weekly_Stats__c>();
for(Consultant_Weekly_Stats__c con:trigger.new)
{
system.debug('cc=============='+cc);
if(UserMap.ContainsKey(con.Consultant__c)){
cc.OwnerID=UserMap.get(con.Consultant__c);
}
if(!cc.isEmpty())
{
update cc;
}
}
}
cc
is a list what is used as singleton in linecc.OwnerId=UserMap.get(con.consultant__c);
- You need to addcon
to listcc' . Better name for
cc` would beconUpdList
- so when you reference it in code, you know you need to use a list method