0

So I am trying to create a picklist of contact names from a specific account (listing available installers) to be assigned as a specific field selection in other accounts. Over time, of course, installer contacts will be added and removed, but it will always be one specific account hosting the contacts.

I thought I would create an Apex trigger to compile that list, but I'm in a bit over my head. Here's what I have so far:

trigger Installers on Contact (after update, after insert) 
    {    
       String names='';
       Integer ok=1;
       Set<id> accIdList =  '001G0000022epuD';
       for(Contact con : Trigger.new)
       {
          if(con.accountid!=null)
           accIdList.add(con.accountid);
       }

       List<Account> accUpdateList = new List<Account>();
       for(Account acc : [Select id, name, Contact_Name__c, (Select Id, name From Contacts) From Account Where Id In : accIdList])
       {
           ok=1;
           for(Contact con : acc.contacts)
           {    

              if(con.name!=null)
              {
               if(ok==1)
               {
                names = con.name;
                ok=0; 
               }

               else
                {
                names = names  + ',' + con.name;
                }

             }

           }
         acc.Contact_Name__c = names;
         accUpdateList.add(acc);
       }    
       update accUpdateList;
}

I'm receiving an error: Illegal assignment from String to Set. Perhaps somebody can help me over the hump here?

Thanks

0

The error can be fixed by changing this line:

Set<id> accIdList =  '001G0000022epuD';

to this:

Set<id> accIdList =  new Set<id>{'001G0000022epuD'};

The Sets documentation provides more explanation.

I'm not entirely clear what you want to do with the comma separated values; often such a list of values would be created when needed in a controller. Also if the number of Contacts can grow very large, a lookup may make more sense than a picklist.

| improve this answer | |
  • Thanks! I do not expect the number of contacts to exceed 50, but a lookup may make more sense. I'm very newbie for salesforce programming and may need to stumble one step at a time... – Dan N. Sep 24 '16 at 16:47
  • Keith C: by the way, you're right, a lookup is a much better (and easier) solution for what I wish to do. Thank you again! – Dan N. Sep 24 '16 at 16:59
  • @DanN. No problem - takes a while to figure out the various choices available and what they are good for. – Keith C Sep 24 '16 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.