0

I am not familiar in development side.am getting loop variable must be type like that. How can I resolve above the issue please can any one help me for this issue.I mention below err line in for loop..

public class dependentcon
  {
    public string cont{get;set;}
    public string acc{get;set;}


    public dependentcon
    (ApexPages.StandardController controller)
    {

    }
    public List<SelectOption> getaccounts()
    {
            List<SelectOption> optionList = new List<SelectOption>();
            List<account> acc=[select id,name from account order by Name];

            optionList.add(new SelectOption('','- None -'));

            for (account fc :acc )
            {
            optionList.add(new SelectOption(fc.id,fc.name));
            }
            return optionList;     
        }

    public List<SelectOption> getcontacts() 
  {


    List<SelectOption> optionList = new List<SelectOption>();

        Map<Id,Account> accts = new Map<Id,Account>([select name,(select name from contacts) from account]);
            Set<Id> accountIds = accts.keySet();
        system.debug('Find the value------>'+accts);
        optionList.add(new SelectOption('','- None -'));

      for(account cn :accountIds ) //Err line
      {
       optionList.add(new SelectOption(cn.id,cn.name));
      }
      return optionList;     

      }
    }
0

Change the method getcontacts(). It will work fine.

public List<SelectOption> getcontacts()
{
    List<SelectOption> optionList = new List<SelectOption>();
    Map<Id,Account> accts = new Map<Id,Account>([select name,(select name from contacts) from account]);
    system.debug('Find the value------>'+accts);
    optionList.add(new SelectOption('','- None -'));

    for(account cn : accts.values()) //Err line
    {
        optionList.add(new SelectOption(cn.id,cn.name));
    }
    return optionList;     
}
2
  • Go for the answer by @sfdcfox – Abbas Mar 12 '15 at 5:34
  • Pleasure. But try to adopt the anser by @sfdcfox. That answer is much more optimized solution to address your problem. – Abbas Mar 12 '15 at 6:11
1

You took the key set from the map (of type ID), and tried to iterate over it as if it were accounts. The error is telling you the word "account" should be "Id". It looks like you were trying to list all contacts by account, which would look more like this:

for(Account ac: accts.values()) {
    for(Contact ct: ac.Contacts) {
        optionList.add(new SelectOption(ct.Id, ct.Name));
    }
}

You really don't need the map, though, and you could have done this as a linear query on contacts sorted by account ID/name (whatever your intent is).

1
  • Hi sfdcfox thank u for your response. I was heard about you and you are one of the best solutioner in this community really me also want to become a good developer after some month like you. please help my career. I make some changes like what you have to changes in my code,now am getting some other err like this: Non-void method might not return a value or might have statement after a return statement in same line.. – Nikitha Mar 12 '15 at 5:48

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.