function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sampath pallisampath palli 

Getting Error "Incompatible element type Id for collection of Account"

Hai All

I have an requirement that when i enter account name in text box click on submit it display all the related contact records 
Please check my code

public class conbyacc {
    public String accname{set;get;}
    public list<Account> acclist{set;get;}
    public list<Contact> conlist{set;get;}
    public set<Account> accids{set;get;}
    public list<Contact> getcons(){
        acclist.clear();
        conlist.clear();
        accids.clear();
        acclist=[select Id,name from Account where name=:accname];
        for(Integer i=0; i<acclist.size(); i++){
            accids.add(acclist[i].Id);  // Getting Error
        }
        conlist=[select name,accountid from Contact where accountid IN:accids];
        return conlist;
        
    }

}


Thanks In Advance
Sampath Palli
Best Answer chosen by sampath palli
Manish BhatiManish Bhati
You have made Set of Account type but you are using it to add Account ID's:-
public set<Account> accids{set;get;}

Better use the following:-
public set<Id> accids{set;get;}


 

All Answers

Manish BhatiManish Bhati
You have made Set of Account type but you are using it to add Account ID's:-
public set<Account> accids{set;get;}

Better use the following:-
public set<Id> accids{set;get;}


 
This was selected as the best answer
Nikx1505Nikx1505
 public set<Account> accids{set;get;}
This set is a collection of Accounts and you are adding just ID's to it,
accids.add(acclist[i].Id); 
you will need to add the entire object
accids.add(acclist[i]); 
or change the collection type to ID instead of Account.
sampath pallisampath palli
Thanks Manish
sampath pallisampath palli
Thanks Nilesh
sampath pallisampath palli
Hai

I write a Vf page and click on button i am getting error please check this image
User-added image
Thanks In Advance
Manish BhatiManish Bhati
Possibly your accname is null in the below query:-
acclist=[select Id,name from Account where name=:accname];
Nikx1505Nikx1505
acclist = new List<Account>();
initialize your list and then assign it to your query.
sampath pallisampath palli
I tried but it's getting Same issue
Manish BhatiManish Bhati
Check from system.debug what is the name coming into Apex code, if it's null let me know.
Also share your VF page code where you are setting the accname.
 
public class conbyacc {
    public String accname{set;get;}
    public list<Account> acclist{set;get;}
    public list<Contact> conlist{set;get;}
    public set<Id> accids{set;get;}
    public list<Contact> getcons(){
        acclist.clear();
        conlist.clear();
        accids.clear();
        ​System.debug(accname);
        acclist=[select Id,name from Account where name=:accname];
        for(Integer i=0; i<acclist.size(); i++){
            accids.add(acclist[i].Id);  // Getting Error
        }

        conlist=[select name,accountid from Contact where accountid IN:accids];
        return conlist;
        
    }

}

 
sampath pallisampath palli
Please check my Vf Page Code:

<apex:page controller="conbyacc">
    <apex:form>
    <apex:inputText value="{!accname}"/>
        <apex:commandButton value="GetRecords" action="{!getcons}" reRender="one"/>
        <apex:outputPanel id="one">
        <apex:pageBlock>
        <apex:pageBlockTable value="{!conlist}" var="c">
            <apex:column headerValue="Name">{!c.name}</apex:column>
            </apex:pageBlockTable>
    </apex:pageBlock>
            </apex:outputPanel>
    </apex:form>

</apex:page>
Manish BhatiManish Bhati
Use below apex:-
public class conbyacc {
    public String accname{set;get;}
    public list<Account> acclist{set;get;}
    public list<Contact> conlist{set;get;}
    public set<Id> accids{set;get;}
    public void getcons(){
        String accountName = '\''+accname+'\'';
        acclist=[select Id,name from Account where name=:accountName];
        for(Integer i=0; i<acclist.size(); i++){
            accids.add(acclist[i].Id);  // Getting Error
        }

        conlist=[select name,accountid from Contact where accountid IN:accids];
        
        
    }

}

 
shravan gourishravan gouri
Hi Sampath

Please update ur controller class with below code. i have tried in my org and everything working great
 
public class conbyacc {
    public String accname{set;get;}
    public list<Account> acclist{set;get;}
    public list<Contact> conlist{set;get;}
    public set<Id> accids{set;get;}
    
    public conbyacc(){
    accids = new set<id>();
    
    } 
    
    
    public void getcons(){
    
        //String accountName = '\''+accname+'\'';
        system.debug(accname);
        acclist=[select Id,name from Account where name like :accname];
        system.debug(acclist);
        
        for(Integer i=0; i<acclist.size(); i++)
        {
            accids.add(acclist[i].Id);  // Getting Error
        }

        conlist=[select name,accountid from Contact where accountid IN:accids];
        
        
    }

}

Please let me know if u still face any issues