2

I have an account list when user selects an account its related contacts should come in pageblocktable.

public class assignment1 
{
public Account acc{get;set;}
public List<wrap> wrapList{get;set;}
public List<SelectOption> allAccounts{get;set;}

public assignment1()
{
    acc = new Account();
    wrapList = new List<wrap>();
    allAccounts = new List<SelectOption>();
    getAddAccounts();
}

public void getAddAccounts()
{
    List<Account> accList =[Select id, name From Account];
    for(Account acc : accList)
    {
        allAccounts.add(new SelectOption(acc.id, acc.name));
    }
}

public void search()
{
    wrapList.clear();
    for(Contact con : [Select id, name, firstname, lastname, email, Email_Sent__c From Contact where accountId =: acc.id])
    {
        wrapList.add(new wrap(con));
    }
}

public void sendEmail()
{
    List<String> emailList = new List<String>();
    for(wrap w : wrapList)
    {
        if(w.checkBox == true)
        {
            if(w.con.email != null)
            {
                emailList.add(w.con.email);
            }
        }
    }
        Messaging.singleEmailMessage msg = new Messaging.singleEmailMessage();
        msg.toAddresses = emailList;
        msg.subject = 'hello';
        msg.plainTextBody ='Hi there !!!!';
        List<Messaging.sendEmailResult> r = Messaging.sendEmail(new List<Messaging.singleEmailMessage>{msg});
}

public class wrap
{
    public contact con{get;set;}
    public boolean checkBox{get;set;}
    public wrap(Contact con)
    {
        this.con = con;
        checkBox = false;
    }
}
}

Visualforce Page

<apex:page controller="assignment1">
<apex:form >
    <apex:pageBlock >
        <apex:actionRegion >
            <apex:selectList value="{!acc}" size="1"> 
                <apex:selectOptions value="{!allAccounts}" />
                <apex:actionSupport rerender="pbs" action="{!search}"/>
            </apex:selectList>
        </apex:actionRegion>
        <apex:outputPanel id="pbs">
            <apex:pageBlockSection >
                <apex:pageBlockTable var="row" value="{!wrapList}">
                    <apex:column headerValue="Action">
                        <apex:inputCheckbox value="{!row.checkBox}"/>
                    </apex:column>
                    <apex:column value="{!row.con.firstname}"/>
                    <apex:column value="{!row.con.lastname}"/>
                    <apex:column value="{!row.con.email}"/>
                    <apex:column value="{!row.con.Email_Sent__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:outputPanel>
        <apex:commandButton action="{!sendEmail}" value="Send Email"/>
    </apex:pageBlock>
</apex:form>
</apex:page>
2
public Account acc{get;set;}

into

public Id acc{get;set;}

Change its type to string and then in your query instead of acc.Id use acc.

Because a select list always give us string and in your case you are binding it with account instance.

Also add a event="onchange" in your actionsupport tag.

  • thanks for the reply @tushar I have changed the code but still pagebloacktable is not getting refreshed – Anzar Sep 9 '16 at 7:13
  • @Anzar check my updated answer. – Tushar Sharma Sep 9 '16 at 7:16
  • 1
    Why not public Id acc{get;set;}? – Keith C Sep 9 '16 at 7:44
1

You need to change

public Account acc{get;set;} 

to

public String acc{get;set;}

Remove the line acc = new Account(); in constructor.

And in the SOQL query needs to be changed to

Select id, name, firstname, lastname, email, Email_Sent__c From Contact where accountId =: acc

Plus add a event attribute in apex:actionSupport to make the apex call when someone changes the select list selection

<apex:actionSupport event="onchange" rerender="pbs" action="{!search}"/>

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.