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
SFDC Admin & AnalystSFDC Admin & Analyst 

Problem in Rendered based on list size and invoke using Action Function

Hi,

 

I have a VF page and Controller class. I have two page blocks. First page block loads all the data and second pageblock must be rendered if any record in first page block is selected. The rendered attribute is not working based on the list size.

 

Here is the code

 

<apex:page standardController="Contact" extensions="ContactCodeExtension" id="pg">

<apex:form id="frm">
<apex:outputPanel id="errorPnl">
<apex:pageMessages />
</apex:outputPanel>
<!------------------------------------------------------------- ACTION FUNCTIONS ---------------------------------------------------------------------------->

<apex:actionFunction action="{!populateContactCode}" name="populateContactCodeAF" reRender="pbResult,errorPnl" status="actionStatus" >
</apex:actionFunction>

<apex:actionFunction action="{!addtoSelectedList}" name="addtoSelectedListAF" reRender="pbResult2,errorPnl" status="actionStatus"/ >
<apex:actionFunction action="{!addtoTEst}" name="addtoTEstAF" reRender="pbResult2" status="actionStatus"/>
<!------------------------------------------------------------- ACTION FUNCTIONS ---------------------------------------------------------------------------->

<apex:pageBlock rendered="{!if(contactId != null,true,false)}" id="pb">

<apex:pageBlockButtons id="pgButtons">

<apex:commandButton value="Cancel" action="{!cancel}" />


<apex:actionStatus id="actionStatus" startText="Please wait....">
<apex:facet name="start">&nbsp;&nbsp;<apex:image value="/img/indicator.gif"/></apex:facet>
<apex:facet name="stop"></apex:facet>
</apex:actionStatus>

</apex:pageBlockButtons>


<h1 style="font-weight:bold;font-size:20px">Adding Contact Codes for {!con.Name}.</h1>

<br/>
<br/>
<apex:pageBlocksection title="Contact Codes" columns="1" id="pbResult">
<apex:outputPanel rendered="{!if(wrapperList.size>0,true,false)}">
<table><tr><td>
<div style="overflow:auto; height:470px;width:870px;">
<apex:pageBlockTable id="resultTable" value="{!wrapperList}" var="wrapper" rendered="{!if(wrapperList.size>0,true,false)}">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox id="allSelected" onclick="checkAll(this,'checkdone');addtoTEstAF();" />
</apex:facet>
<apex:inputCheckbox value="{!wrapper.isSelected}" id="checkdone" onclick="addtoSelectedListAF();"/>


</apex:column>

<apex:column >
<apex:facet name="header">
<apex:outputPanel >
cade444
</apex:outputPanel>
</apex:facet>
<apex:outputField value="{!wrapper.ccDesc.Description__c}" />
</apex:column>
<apex:column >
<apex:facet name="header">
code
</apex:facet>
<apex:outputField value="{!wrapper.ccDesc.Description__c}" />
</apex:column>
</apex:pageBlockTable>
</div> </td></tr></table>
</apex:outputPanel>
<apex:outputPanel rendered="{!If(wrapperList.size = 0,true,false)}" id="errMsg">
<span style="color:red;">No Records found</span>
</apex:outputPanel>


</apex:pageblocksection>
</apex:pageBlock>
<apex:pageBlock rendered="{!If(backupContactCodes.size>0,true,false)}" id="pbResult2">
<apex:pageblocksection title="Selected Contact Codes" columns="1" >
<apex:outputpanel id="ProductUnitsInformation" >
<table><tr><td>
<div style="overflow:auto; height:470px;width:870px;">
<apex:pageBlockTable id="resultTable2" value="{!backupContactCodes}" var="wrapper2">

<apex:column >
<apex:facet name="header">
<apex:outputPanel >

Code Description

</apex:outputPanel>
</apex:facet>
<apex:outputField value="{!wrapper2.Description__c}" title="Code Description"/>
</apex:column>
</apex:pageBlockTable>
</div> </td></tr></table>
</apex:outputpanel>


</apex:pageBlocksection>
</apex:pageBlock>


<!-- // @Sashi Singh -- 09/19/2013 -- RRM Request Number -- REQ-00002156 -->
<input type="hidden" id="oldCodeType" value="" />

<script type="text/javascript">
function checkAll(cb,cbid){
var allchecked = cb.checked;
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++){
if(inputElem[i].id.indexOf(cbid)!=-1){
inputElem[i].checked = cb.checked;
}
}

document.getElementById("allSelected").checked = allchecked;

addtoSelectedListAF();
}
function closeWindow(){
//window.opener.parent.location.href = '/{!contactId}';
window.self.close();
return false;

}
function testingactionfunction(){

addtoSelectedListAF();
}
function changeUrl(url) {
location.href = url;
window.reload();
}



</script>


</apex:form>

</apex:page>

___________________________________________________________________________________________


public with sharing class ContactCodeExtension {

public List<ContactCodeDescWrapper> wrapperList {set;get;}

public String contactId {set;get;}
public Contact con {set;get;}



private Set<Id> selectedCodeDescs;

Public static List<ContactCodeDescription__c> backupContactCodes { set; get;}
public string selectedCCDesc {get; set;}
Public static Boolean renderSetbackupCC = false;
Public static Boolean isPageLoad {get;set;}
public List<ContactCodeDescWrapper> selectedWrapperList {set;get;}
public set<Id> setId {set;get;}

//****************************************************************************
//Constructor
//***************************************************************************/
public ContactCodeExtension(ApexPages.StandardController stdController){
contactId = Apexpages.currentPage().getParameters().get('contactId');
init();

}




//////////////////////////////// Private Methods ////////////////////////////
//****************************************************************************
// Method for initialization
///***************************************************************************/
private void init(){

//
system.debug('***renderSetbackupCC1'+renderSetbackupCC);
//renderSetbackupCC = false;
system.debug('***renderSetbackupCC2'+renderSetbackupCC);
isPageLoad = true;
System.Debug('***renderSetbackupCC'+renderSetbackupCC);
backupContactCodes = new List<ContactCodeDescription__c>();
////

wrapperList = new List<ContactCodeDescWrapper>();
selectedWrapperList = new List<ContactCodeDescWrapper>();
setId = new set<Id>();


selectedCodeDescs = new Set<Id>();

if(contactId != null){

populateContactCode();
getContact();
system.debug('***renderSetbackupCC3'+renderSetbackupCC);
addtoSelectedList();
addtoTEst();
system.debug('***renderSetbackupCC6'+renderSetbackupCC);
}else{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Please associate Contact Codes directly from the Contact object.'));
}
}
private void getContact(){
for(Contact contact : [select Id,name,Email,Phone,MobilePhone from Contact where Id =:contactId limit 1]){
con = contact;
}
}

public void backupSelection(){
selectedCodeDescs.clear();
for(ContactCodeDescWrapper wrapper : wrapperList){
if(wrapper.isSelected){
selectedCodeDescs.add(wrapper.ccDesc.Id);
}
}

System.Debug('***wrapperList'+wrapperList);
}

//Cancel
Public void cancel(){


}

//****************************************************************************
// Method to populate contact code
///***************************************************************************/
public void populateContactCode(){
String query ='Select Description__c From ContactCodeDescription__c';


backupSelection();

System.Debug('***selectedCodeDescs populateContactCode'+selectedCodeDescs);
System.Debug('***wrapperList populateContactCode'+wrapperList);

for(Id ccDesc : selectedCodeDescs)
setId.add(ccDesc);

//selectedWrapperList.add(new ContactCodeDescWrapper(ccDesc,selectedCodeDescs.contains(ccDesc.Id))) ;

Map<ContactCodeDescription__c,boolean> ccDescStatus = new Map<ContactCodeDescription__c,boolean>();

for(ContactCodeDescWrapper wrapper : wrapperList){
ccDescStatus.put(wrapper.ccDesc,wrapper.isSelected);
//If(selectedWrapperList.contains(wrapper))
//selectedWrapperList.put(wrapper.ccDesc, wrapper.isSelected);

}

for(ContactCodeDescWrapper wrapper : selectedWrapperList ){
If(ccDescStatus.containskey(wrapper.ccDesc) != null){
If(ccDescStatus.containskey(wrapper.ccDesc)){
If(wrapper.isSelected != ccDescStatus.get(wrapper.ccDesc))
wrapper.isSelected= ccDescStatus.get(wrapper.ccDesc);
}
}


}

wrapperList.clear();
for(ContactCodeDescription__c ccDesc : Database.query(query)){
wrapperList.add(new ContactCodeDescWrapper(ccDesc,selectedCodeDescs.contains(ccDesc.Id)));
system.Debug('***isPageLoad'+isPageLoad);
If(isPageLoad != null && isPageLoad ){

selectedWrapperList.add(new ContactCodeDescWrapper(ccDesc,selectedCodeDescs.contains(ccDesc.Id)));
}

}
isPageLoad = false;
for(ContactCodeDescWrapper wrapper : wrapperList){

If(setId.contains(wrapper.ccDesc.Id)){
wrapper.isSelected = true;
}
}

System.debug('wrapperList 1 ***' + wrapperList.size());


}

public void addtoSelectedList(){

//wrapperList = new List<ContactCodeDescWrapper>();
backupContactCodes = new List<ContactCodeDescription__c>();

System.Debug('***wrapperList'+wrapperList);
//searchedWrapperList

System.Debug('***wrapperList.size'+wrapperList.size());

System.Debug('***backupContactCodes'+backupContactCodes);
//

Map<ContactCodeDescription__c,boolean> ccDescStatus = new Map<ContactCodeDescription__c,boolean>();

for(ContactCodeDescWrapper wrapper : wrapperList){
ccDescStatus.put(wrapper.ccDesc,wrapper.isSelected);
//If(selectedWrapperList.contains(wrapper))
//selectedWrapperList.put(wrapper.ccDesc, wrapper.isSelected);

}

for(ContactCodeDescWrapper wrapper : selectedWrapperList ){
If(ccDescStatus.containskey(wrapper.ccDesc)){
If(wrapper.isSelected != ccDescStatus.get(wrapper.ccDesc))
wrapper.isSelected= ccDescStatus.get(wrapper.ccDesc);

}


}

system.Debug('***selectedWrapperList'+selectedWrapperList);
For( ContactCodeDescWrapper wrapper : selectedWrapperList ){

If(wrapper.isSelected){
backupContactCodes.add(wrapper.ccDesc);
//rendered="{!renderSetbackupCC}"
system.debug('***renderSetbackupCC5'+renderSetbackupCC);
renderSetbackupCC = true;
}

}
system.debug('***renderSetbackupCC6'+renderSetbackupCC);
If(backupContactCodes.size() > 0)
renderSetbackupCC = true;

System.Debug('***wrapperList'+wrapperList);
System.Debug('***backupContactCodes'+backupContactCodes);

System.Debug('***renderSetbackupCC '+renderSetbackupCC );

System.Debug('***renderSetbackupCC '+renderSetbackupCC );

System.Debug('***selectedCCDesc'+selectedCCDesc);

}
public void addtoTEst(){

system.Debug('***isPageLoadALLL'+isPageLoad);

If(isPageLoad){
For( ContactCodeDescWrapper wrapper : wrapperList ){

//If(wrapper.isSelected)
backupContactCodes.add(wrapper.ccDesc);
//rendered="{!renderSetbackupCC}"

//renderSetbackupCC = true;

}
}
}

//****************************************************************************
//Wrapper Class for ContactCodeDescription__c
//***************************************************************************/
public class ContactCodeDescWrapper{
public ContactCodeDescription__c ccDesc {set;get;}
public boolean isSelected {set;get;}
//Constructor
public ContactCodeDescWrapper(ContactCodeDescription__c ccDesc,Boolean isSelected){
this.isSelected = isSelected;
this.ccDesc = ccDesc;

}
}
}

 

Please let me know. How to fix this.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
SFDC Admin & AnalystSFDC Admin & Analyst
        <apex:pageBlocksection title="sample" columns="1" id="pbResult">  
         
        <apex:outputPanel rendered="{!if(wrapperList.size>0,true,false)}">
            <table><tr><td>
            <div style="overflow:auto; height:auto;max-height:470px;width:870px;">
            <apex:pageBlockTable id="resultTable" value="{!wrapperList}" var="wrapper" rendered="{!if(wrapperList.size>0,true,false)}">



Always rerender the outer block(pageBlocksection here) and rendered the inner blocks (output panel and page block table)

Also, rerender an output panel if the wrapperlist size is zero.