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
Deepak Chouhan 9Deepak Chouhan 9 

Update the contact active__c field status onchange apex:inputcheckbox

Hi Friends,
I have problem in my code. i have created a pageblockTable with pagination. it's work fine.
issue is i want to update all pageblocktable active__c(checkbox) fields onchange apex:inputcheckbox.
i was trying with constructor so all active__c fields changed but pagination not working.

pls help me out....

<apex:page standardController="Contact" extensions="ExtensionEWorkbookForm">
    <apex:form id="IdFrm" >
        <apex:pageBlock id="IdPB" title="Subscription Details">  
            <!--page block button for update or cancel the records--------->
            <apex:pageBlockButtons >
                <apex:commandButton action="{!updateList}" value="Update"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:messages />
            <apex:pageBlockSection title="Non-Subscriber Contacts" collapsible="false">
                <apex:inputcheckbox value="{!selectAllSubscribers}" label="Select All Subscribers">    
                <apex:actionSupport event="onchange" rerender="IdPBT" action="{!selectDeselectAllsubscribers}"/>    
            </apex:inputcheckbox>
            </apex:pageBlockSection>
            <!-----------------------------------Contact list record show in page block table-->
            <apex:pageblockTable id="IdPBT" value="{!contactList}" var="con">
                <apex:column >
                    <apex:facet name="header">Subscribe</apex:facet>
                    <apex:inputField value="{!con.Active__c}"/>    
                </apex:column>
                <apex:column >            
                    <apex:facet name="header">Name</apex:facet>
                    <apex:commandLink value="{!con.name}" action="{!URLFOR($Action.Contact.View,con.id)}"/>
                </apex:column>
                <apex:column >            
                    <apex:facet name="header">Mobile</apex:facet>
                    <apex:outputLabel value="{!con.mobilephone}"/>
                </apex:column>
                <apex:column >            
                    <apex:facet name="header">Email</apex:facet>
                    <apex:outputLabel value="{!con.email}"/>
                </apex:column>
                <apex:column >            
                    <apex:facet name="header">Phone</apex:facet>
                    <apex:outputLabel value="{!con.phone}"/>
                </apex:column>
                <apex:column >            
                    <apex:facet name="header">City</apex:facet>
                    <apex:outputLabel value="{!con.MailingCity}"/>
                </apex:column>
                <apex:column >            
                    <apex:facet name="header">State</apex:facet>
                    <apex:outputLabel value="{!con.MailingState}"/>
                </apex:column>                                
                <apex:column >            
                    <apex:facet name="header">Country</apex:facet>
                    <apex:outputLabel value="{!con.MailingCountry}"/>
                </apex:column>
            </apex:pageblockTable>                
        <!---------Navigation page button section------------------>
        <apex:commandButton value="First Page" rerender="IdFrm" action="{!firstPage}" disabled="{!previous}"/>
            <apex:commandButton value="Previous" rerender="IdFrm" action="{!previousPage}" disabled="{!previous}"/>
            <apex:commandButton value="Next" rerender="IdFrm" action="{!nextPage}" disabled="{!next}"/>
            <apex:commandButton value="Last Page" rerender="IdFrm" action="{!lastPage}" disabled="{!next}"/>
        </apex:pageBlock>            
    </apex:form>
</apex:page>

public with sharing class ExtensionEWorkbookForm {
    public List<Contact> contactListUpdate = new List<Contact>();
    private integer totalRecs = 0;
    private integer offsetSize = 0;
    private integer limitSize= 3;
    public Boolean selectAllSubscribers{get;set;}
    //Standard Controller Constructor
    public ExtensionEWorkbookForm (ApexPages.StandardController controller) {
        Initialization();
    }

    //Initializ data on page load
    public void Initialization(){
        totalRecs = [select count() from contact];                      
    }
    
    public List<Contact> getContactList(){
        contactListUpdate = Database.Query('Select Email,Active__c,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,MobilePhone,Name,Phone from Contact where Active__c=false limit :limitSize offset :offsetSize');
        return contactListUpdate ;
    }    
    
    public PageReference updateList() {
        PageReference pR= new PageReference('/003/o');
        if(contactListUpdate.size()>0){
            try{
                update contactListUpdate;
                return pR;
            }catch(Exception e){
               
                return null;
            }
        }else{
            return pR;
        }
    }    
    //
    public PageReference selectDeselectAllsubscribers(){    
         if(selectAllSubscribers){
             for(Contact c:contactListUpdate){
                c.Active__c=true;
             }
         }
         else{
                for(Contact c:contactListUpdate){
                c.Active__c=false;
             }
             
         }
         return null;
    }
    //Action Method for Datatabel First Page Btn  
    public void firstPage(){
        offsetSize = 0;
    }    
    
    //Action Method for Datatable Previous page Btn
    public void previousPage(){
        offsetSize = offsetSize - limitSize;
    }    
    
    //Action Method for Datatable Next Page Btn
    public void nextPage(){
        offsetSize = offsetSize + limitSize;
    }
    
    //Action Method for Datatable Last Page Btn
    public void lastPage(){
        offsetSize = totalRecs - limitSize;
    }
    
    //Property  for Datatable Previous Btn
    public boolean getprevious(){
        if(offsetSize == 0)
            return true;
        else
            return false;
    }
    
    //Property for Datatable Next Btn
    public boolean getnext(){
        if((offsetSize + limitSize) > totalRecs-1)
            return true;
        else
            return false;
    }
 
    
}






 
NagendraNagendra (Salesforce Developers) 
Hi Deepak,

First and foremost sincerely regret delayed reply.

Not sure this is addressing the error you are seeing but this looks wrong...

When the page is being re-rendered (after the selectDeselectAllsubscribers method has updated the in-memory Contact objects) the {!contactList} expression in the page causes this method to be called:
public List<Contact> getContactList(){
    contactListUpdate = Database.Query('Select Email,Active__c,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,MobilePhone,Name,Phone from Contact where Active__c=false limit :limitSize offset :offsetSize');
    return contactListUpdate ;
}
and it always requires the Active__c field from the database so losing the settings made by selectDeselectAllsubscribers.

One way to address that is to keep the set of selected Ids and apply that selection as the user pages forward and backward and use that selection in the final save:
private Set<Id> selecteds = new Set<Id>();

public PageReference selectDeselectAllsubscribers() {
     for (Contact c : contactListUpdate) {
         if (selectAllSubscribers) selecteds.add(c.Id);
         else selecteds.remove(c.Id);
     }
}

public List<Contact> getContactList(){
    contactListUpdate = [
            Select Id,Email,Active__c,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,MobilePhone,Name,Phone
            from Contact
            where Active__c = false
            limit :limitSize
            offset :offsetSize
            ];
    for (Contact c : contactListUpdate) {
        c.Active__c = selecteds.contains(c.Id);
    }
    return contactListUpdate;
}  

public PageReference updateList() {
    try {
        Contact[] updates = new Contact[] {};
        for (Id id : selecteds) {
            updates.add(new Contact(Id = Id, Active__c = true));
        }
        update updates;
    } catch(Exception e) {
        return null;
    }
    return new PageReference('/003/o');
}
Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue and becomes a proper solution which results in helping others who are really in need of it.

Best Regards,
Nagendra.P