-1

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; 
                }
       }
    }
1
  • Variable naming will cause some other issues: (1) cc is a list what is used as singleton in line cc.OwnerId=UserMap.get(con.consultant__c); - You need to add con to list cc' . Better name for cc` would be conUpdList - so when you reference it in code, you know you need to use a list method
    – cropredy
    Sep 14, 2015 at 16:36

2 Answers 2

1

Here, consultantSet should be a list of IDs rather than list of Objects.

in the following line:
List<User> users = new List<User>([Select id, Name from user where Name =:consultantSet]);

try to use IN: instead of =:, as consultantSet is a list

So, it looks like
List<ID> consultantSet = new List<ID>(); List<User> users = new List<User>([Select id, Name from user where Name IN: consultantSet]);

-1

Got the solution of my Question thanks who suggest me. Here is the code.

trigger CopyOwnerNameConsultant on Consultant_Weekly_Stats__c(after insert)
{
    set<String> consultantSet = new set<String>();
        set<string> userNames = new Set<string>();
        Map<String, ID> UserMap = new Map<String, 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,obj.id);           
        }
      List<Consultant_Weekly_Stats__c> cc= new list<Consultant_Weekly_Stats__c>();
          set<ID> cwsid= new set<ID>();

        for(Consultant_Weekly_Stats__c con:trigger.new)
        {        
          cwsid.add(con.Id);              
        }  
       cc=[select id,Consultant__c from Consultant_Weekly_Stats__c where ID=:cwsid];
      for(Consultant_Weekly_Stats__c cws: cc){ 
             system.debug('UserMap=============='+UserMap);   
                if(UserMap.ContainsKey(cws.Consultant__c)){
                   system.debug('UserMap=============='+UserMap.get(cws.Consultant__c));    
                   cws.OwnerID=UserMap.get(cws.Consultant__c);  
                    //cc.add(cws);
             }

      }

     if(!cc.isEmpty())
            {
               update cc; 
            }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .