Hi guys, here we are going to show, how to display related contact & opportunity records of Account using checkbox functionality dynamically.
Here we used actionsupport for this requirement, dynamically we can edit records for the selected account.
Apex Controller
public class Acc_con_Opp_Details1 {
//list of collection of the wrapper class object
public list<accountwrapper> actwrap {set;get;}
//list of collection of Account,contact and opportunity objects
public list<Account> accounts {set;get;}
public list<Account> acts {set;get;}
public list<opportunity> opts {set;get;}
public list<opportunity> sopts {set;get;}
public list<contact> cnts {set;get;}
public list<contact> snts {set;get;}
public string searchstring {get;set;}//used in searching account records
public boolean oppbox {set;get;}//used as check box for opportunity
public boolean conbox {set;get;}//used as check box for contact
public boolean flag1 {set;get;}//used in account page block
public boolean flag2 {set;get;}//used in contact page block
public boolean flag3 {set;get;}//used in opportunity page block
public boolean checkallflag {set;get;}//used for header check box
public Boolean oppError {set;get;}//used to display opportunity error
public Boolean conError {set;get;}//used to display contacts error
public Boolean oppboxError {set;get;}//used to dispaly opportunity error
public Boolean conboxError {set;get;}//used to display contacts error
public list<String> AlphaList {get; set;}//collection of alphabets
public String AlphaFilter {get; set;}//used to filter alphabets
private String Query;
//this variables are used for pagination purpose
private integer totalRecs = 0;//stores no.of total records
private integer index = 0;//used for tracking offset
private integer blockSize =5;//for setting size of page
private String sortDirection = 'ASC';
private String sortExp = 'Name';
public Map<String, String> mapAlpha{get; set;}
//in this constructor we are setting values to boolean variables
public Acc_con_Opp_Details1()
{
flag1=true;
flag2=false;
flag3=false;
oppError=false;
conError=false;
oppboxError=false;
conboxError=false;
checkallflag = false;
totalRecs = [select count() from Account];//returns total no.of account records
getactwrap();//calling getactwrap method.
AlphaList = new list<String> {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Other', 'All'};
if (apexpages.currentpage().getparameters().get('alpha') == null) {
AlphaFilter = 'All';
} else {
AlphaFilter = apexpages.currentpage().getparameters().get('alpha');
}
}
public String sortExpression
{
get
{
return sortExp;
}
set
{
//if the column is clicked on then switch between Ascending and Descending modes
if (value == sortExp)
sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';
else
sortDirection = 'ASC';
sortExp = value;
}
}
public String getSortDirection()
{
//if not column is selected
if (sortExpression == null || sortExpression == '')
return 'ASC';
else
return sortDirection;
}
public void setSortDirection(String value)
{
sortDirection = value;
}
//this method displays first five records
public void beginning()
{
flag2 = false;
flag3 = false;
oppbox=false;
conbox=false;
conboxError=false;
oppboxError=false;
checkallflag=false;
index = 0;
getactwrap();
}
//this method displays prevoius records
public void previous()
{
flag2 = false;
flag3 = false;
oppbox=false;
conbox=false;
conboxError=false;
oppboxError=false;
checkallflag=false;
index = index-blockSize;
getactwrap();
}
//this method displays next records
public void next()
{
flag2 = false;
flag3 = false;
oppbox=false;
conbox=false;
conboxError=false;
oppboxError=false;
checkallflag=false;
index = index+blockSize;
getactwrap();
}
//this method displays last remaining records
public void end()
{
flag2 = false;
flag3 = false;
oppbox=false;
conbox=false;
conboxError=false;
oppboxError=false;
checkallflag=false;
index = totalrecs - math.mod(totalRecs,blockSize);
getactwrap();
}
//this variable is used to enable or disable first and previous buttons
public boolean prev{get
{
if(index == 0)
return true;
else
return false;
} set;}
//this variable is used to enable or disable next and last buttons
public boolean nxt{get
{
if((index + blockSize) > totalRecs)
return true;
else
return false;
} set;}
//used to display opportunities and contacts w.r.t selected accounts
public void submit()
{
acts=new list<Account>();
List<Boolean> acflagList = new List<Boolean>();
for(accountwrapper aw:actwrap)
{
if(aw.acflag){
acts.add(aw.acc);
acflagList.add(aw.acflag);
}
}
if(acflagList.isEmpty()) {
if(oppbox)
oppboxError=true;
if(conbox)
conboxError=true;
flag2=false;
flag3=false;
oppError=false;
conError=false;
}
else
{
oppboxError=false;
conboxError=false;
}
//if we select contact check box,then it displays contacts for selected accounts
if(conbox)
{
snts=[select id,lastName,Department,account.name from contact where accountid IN:acts];
if(snts.size()>0)
{
flag3=true;
conError=false;
}
else
{
for(accountwrapper aw : actwrap) {
if(aw.acflag) {
flag3=true;
conError = true;
}
//ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'contact records are not found for selected Accounts.'));
}
}
} else {
flag3 = false;
}
//if we select opportunity check box,then it displays opportunities for selected accounts
if(oppbox)
{
opts=[select id,name,stageName,leadsource,account.name from opportunity where accountId IN:acts];
if(opts.size() >0)
{
flag2=true;
oppError=false;
}
else
{
for(accountwrapper aw : actwrap) {
if(aw.acflag) {
flag2=true;
oppError = true;
}
//ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'opportunity records are not found for selected Accounts.'));
}
}
} else {
flag2 = false;
}
}
//it hides contacts and dispalys opportunitites on uncheck of contact check box
//This method uses a simple SOQL query to return a List of Accounts
public void getactwrap() {
String QueryIf = '';
string sortFullExp = sortExpression + ' ' + sortDirection;
if (AlphaFilter == null || AlphaFilter.trim().length() == 0) {
AlphaFilter = 'All';
}
Query = 'SELECT Id, Name, phone ' + ' FROM Account';
if (AlphaFilter == 'Other') {
QueryIf = QueryFor(QueryIf, '(' + 'Name' + ' < \'A\' OR ' +
'Name' + ' > \'Z\') AND (NOT ' +
'Name' + ' LIKE \'Z%\') ');
} else if (AlphaFilter != 'All') {
QueryIf = QueryFor(QueryIf, '(' + 'Name' + ' LIKE \'' + String.escapeSingleQuotes(AlphaFilter) + '%\')' );
}
Query += QueryIf;
Query += ' ORDER BY ' + sortFullExp + ' LIMIT : blockSize OFFSET : index';
accounts = Database.Query(Query);
actwrap=new list<accountwrapper>();
// As each Account is processed we create a new accountwrapper object and add it to the wrapper class(accountwrapper) list.
for(account a:accounts)
{
actwrap.add(new accountwrapper(a));
}
flag2=false;
flag3=false;
oppbox=false;
conbox=false;
conboxError=false;
oppboxError=false;
checkallflag=false;
}
public String QueryFor(String Q, String C) {
if (Q == '') {
return ' WHERE ' + C;
} else {
return Q + ' AND ' + C;
}
}
public void Hideandshowopp()
{
if(oppbox)
{
submit();
} else {
flag2 = false;
oppboxError=false;
}
}
// it hides opportunities and dispalys contacts on uncheck of opportunities check box
public void HideandshowCon()
{
if(conbox)
{
submit();
} else {
flag3 = false;
conboxError=false;
}
}
public void Hideandshowoppcon() {
Hideandshowopp();
Hideandshowcon();
}
// this method uses dml operation to edit the existing opportunities values or to insert new oppt values
public void saveopps()
{
list<opportunity> opps=new list<opportunity>();
opps.addAll(opts);
upsert opps;
}
//his method uses dml operation to edit the existing contact values or to insert new contact values
public void savecons()
{
cnts = new list<contact>();
cnts.addall(snts);
upsert cnts;
}
//used for searching account records dynamically
public void search(){
string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20';
accounts= Database.query(searchquery);
system.debug('accounts'+searchquery);
}
//This is wrapper class which is collection of other class instances
//here wrapper class contains both the standard salesforce object Account and a Boolean value acflag
public class accountwrapper{
public account acc{set;get;}
public boolean acflag{set;get;}
//In this contructor When we create a new accountwrapper object we pass a Account that is set to the acc property.
// We also set the boolean value to false
public accountwrapper(account a){
acc=a;
acflag=false;
}
}
}
Vf page
<apex:page controller="Acc_con_Opp_Details1" showHeader="false" docType="html-5.0">
<style type="text/css">
a.alpha-select{
font-weight: bold;
text-decoration: none;
background-color: #C6E1FF;
color: #000000 !important;
}
</style>
<script>
function checkallflag(allCheckboxes){
var container = allCheckboxes;
while (container.tagName != "TABLE") {
container = container.parentNode;
}
var inputs = container.getElementsByTagName("input");
var checked = allCheckboxes.checked;
for (var i = 0; i < inputs.length; i++) {
var input = inputs.item(i);
if (input.type == "checkbox") {
if (input != allCheckboxes) {
input.checked = checked;
}
}
}
CallSubmitMethod();
}
</script>
<apex:form >
<apex:actionFunction name="CallSubmitMethod" action="{!submit}"/>
<!--This block dispalying account record details-->
<apex:outputPanel id="par">
<apex:pageblock id="par1" rendered="true" title="Related Opp & Con details display on selection of checkbox">
Enter:<apex:inputText value="{!searchstring}" style="margin-left:5px;" />
<apex:commandButton value="Search records" action="{!search}" style="margin-left:10px;"/>
<br/><br/> <apex:repeat value="{!AlphaList}" var="a">
<apex:commandLink value="{!a}" action="{!getactwrap}" rerender="par1" styleClass="alpha-link{!if(AlphaFilter=a,' alpha-select','')}">
<apex:param name="AlphaFilter" value="{!a}" assignTo="{!AlphaFilter}" />
<!--<apex:actionSupport event="onchange" action="{!Reset}"/>-->
</apex:commandLink>
<apex:outputText > | </apex:outputText>
</apex:repeat>
<apex:pageMessages ></apex:pageMessages>
<apex:pageblocktable value="{!actwrap}" var="a">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox onclick="checkallflag(this)" value="{!checkallflag}"/>
</apex:facet>
<!--<apex:facet name="header">Select</apex:facet>-->
<apex:inputCheckbox value="{!a.acflag}">
<apex:actionSupport event="onchange" action="{!Hideandshowoppcon}" reRender="par"/>
</apex:inputCheckbox>
</apex:column>
<!-- displays id,name and phone number for accounts-->
<!-- <apex:column value="{!a.acc.id}" width="30%"/> -->
<apex:column width="50%">
<apex:facet name="header">
<apex:commandLink action="{!getactwrap}" value="Name{!IF(sortExpression=='name',IF(sortDirection='ASC','▼','▲'),'▼')}">
<apex:param value="name" name="column" assignTo="{!sortExpression}"></apex:param>
</apex:commandLink>
</apex:facet>
<apex:outputLabel >{!a.acc.Name}</apex:outputLabel>
</apex:column>
<apex:column value="{!a.acc.phone}" width="50%"/>
</apex:pageblocktable>
<!-- this buttons are used to paginate account records-->
<apex:pageblockButtons location="top">
<!--displays first five records-->
<apex:commandButton value="first" action="{!beginning}" disabled="{!prev}" style="height:22px;width:57px;"/>
<!--displays previous records-->
<apex:commandButton value="previous" action="{!previous}" disabled="{!prev}" style="height:22px;width:57px;"/>
<!--displays previous next records-->
<apex:commandButton value="next" action="{!next}" disabled="{!nxt}" style="height:22px;width:57px;"/>
<!-- displays last records-->
<apex:commandButton value="last" action="{!end}" disabled="{!nxt}" style="height:23px;width:57px;"/>
</apex:pageblockButtons>
<!-- check boxs for opportunities and contacts-->
<center> <apex:inputCheckbox value="{!oppbox}">
<apex:actionSupport event="onchange" action="{!Hideandshowopp}"/>
</apex:inputCheckbox>Opportunities
<apex:inputCheckbox value="{!conbox}">
<apex:actionSupport event="onchange" action="{!HideandshowCon}"/>
</apex:inputCheckbox>Contacts
<!-- <apex:commandButton value="submit" action="{!submit}"/>-->
</center>
<apex:pageMessage rendered="{!oppboxError}" severity="error" strength="1" summary="Choose atleast one account record to display opportunities" />
<apex:pageMessage rendered="{!conboxError}" severity="error" strength="1" summary="Choose atleast one account record to display contacts" />
</apex:pageblock>
<!--this block displays opportunity details-->
<apex:pageblock rendered="{!flag2}">
<apex:pageblocktable value="{!opts}" var="o">
<apex:column >
<apex:facet name="header">Opportunity Account Name</apex:facet>
<apex:commandlink value="{!o.account.name}" Action="{!URLFOR($Action.opportunity.edit,o.Id)}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Opportunity Name</apex:facet>
<apex:inputtext value="{!o.name}" />
</apex:column>
<apex:column >
<apex:facet name="header">Opportunity Stage</apex:facet>
<apex:inputtext value="{!o.stagename}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Opportunity Leadsource</apex:facet>
</apex:column>
</apex:pageblocktable>
<apex:pageMessage rendered="{!oppError}" severity="error" strength="1" summary="No Opportunites Found" />
<apex:commandButton value="Save Opportunities" action="{!saveopps}" rendered="{!opts.size !=0}"/>
</apex:pageblock>
<!--this block is used for displaying contact details -->
<apex:pageblock rendered="{!flag3}">
<apex:pageblocktable value="{!snts}" var="c">
<apex:column >
<apex:facet name="header">Contact Account Name</apex:facet>
<apex:commandlink value="{!c.account.name}" Action="{!URLFOR($Action.contact.edit,c)}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Contact Lastname</apex:facet>
<apex:inputtext value="{!c.lastname}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Contact Department</apex:facet>
<apex:inputtext value="{!c.Department}"/>
</apex:column>
</apex:pageblocktable>
<apex:pageMessage rendered="{!conError}" severity="error" strength="1" summary="No Contacts Found" />
<apex:commandButton value="Save Contacts" action="{!savecons}" rendered="{!snts.size !=0}"/>
</apex:pageblock>
</apex:outputPanel>
</apex:form>
</apex:page>
Output