+ Start a Discussion
Frank CarterFrank Carter 

apex method for wrapper class to improve

Hello,

I neeed help.
I created a vf page that show records. I have a method which puts the month of a date field of each record selected to the current month. I have noticed, however, that by selecting more than 40 records, the page gives an error on line 39 (update selectedBD). it takes a long time to process.
 
public class SearchWithWrapperC {
    
    public  Date StartDate {get;set;}
    public  Date EndDate {get;set;}
  
    
    public List<WrapperClass> listBD {get;set;}
   
    
    public void loadData() {
        listBD = new List<WrapperClass>();
        for(Billing_Detail__c  cr : [Select Id,SF_Opportunity_Id__c,Account_Name__c,Billing_Detail__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c   from Billing_Detail__c Where Billing_Status__c='Authorized for Billing'AND Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate  Order by Account_Name__c, Monthly_Forecast__c]){
            listBD.add(new WrapperClass (cr, false));
        }
    }
    
        public List<WrapperClass> getBilling() {
            if(listBD == null) {

        listBD = new List<WrapperClass>(); 
        for(Billing_Detail__c  cr : [Select Id,SF_Opportunity_Id__c,Account_Name__c,Billing_Detail__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c   from Billing_Detail__c Where Billing_Status__c='Authorized for Billing'AND Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate  Order by Monthly_Forecast__c ]){
            listBD.add(new WrapperClass (cr, false));
        }
        }       
         return listBD;
        }
    
        
   public PageReference processSelected() {
       Integer m = Date.Today().Month();
       List<Billing_Detail__c> selectedBD = new List<Billing_Detail__c>();
       for(WrapperClass bd: getBilling()){
           if(bd.check_box== true){
               selectedBD.add(bd.cs);
           } 
       }
        for(Billing_Detail__c cs: selectedBD) {
         cs.Monthly_Forecast__c = Date.newinstance(  cs.Monthly_Forecast__c.year() , m ,  cs.Monthly_Forecast__c.day() );
          update selectedBD;
            System.debug('The value is: ' + cs.Monthly_Forecast__c );   
       }
       
     
       return null;
       
   }
    
    


    
    public class WrapperClass {
        public Billing_Detail__c  cs {get; set;}
        public Boolean check_box {get; set;}
        
        public WrapperClass(Billing_Detail__c  c, Boolean check_box){
            this.cs = c;
            this.check_box = false;
        }
    }
}

I maybe implement logic the wrong way? there is better way?

Can Someone help me?


Thanks
Best Answer chosen by Frank Carter
Ahmed Ansari 13Ahmed Ansari 13
Hii  Frank Carter
I have updated Above Code I think it works for you :
 
public class SearchWithWrapperC {
    
    public  Date StartDate {get;set;}
    public  Date EndDate {get;set;}
  
    
    public List<WrapperClass> listBD {get;set;}
   
    
    public void loadData() {
        listBD = new List<WrapperClass>();
        for(Billing_Detail__c  cr : [Select Id,SF_Opportunity_Id__c,Account_Name__c,Billing_Detail__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c   from Billing_Detail__c Where Billing_Status__c='Authorized for Billing'AND Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate  Order by Account_Name__c, Monthly_Forecast__c]){
            listBD.add(new WrapperClass (cr, false));
        }
    }
    
        public List<WrapperClass> getBilling() {
            if(listBD == null) {

        listBD = new List<WrapperClass>(); 
        for(Billing_Detail__c  cr : [Select Id,SF_Opportunity_Id__c,Account_Name__c,Billing_Detail__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c   from Billing_Detail__c Where Billing_Status__c='Authorized for Billing'AND Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate  Order by Monthly_Forecast__c ]){
            listBD.add(new WrapperClass (cr, false));
        }
        }       
         return listBD;
        }
    
        
   public PageReference processSelected() {
       Integer m = Date.Today().Month();
       List<Billing_Detail__c> selectedBD = new List<Billing_Detail__c>();
       for(WrapperClass bd: getBilling()){
	        Billing_Detail__c billlingDetail ;
           if(bd.check_box== true){
		   
		        billlingDetail = new Billing_Detail__c(Id=bd.cs.Id);
		      billlingDetail.Monthly_Forecast__c = Date.newinstance(  bd.cs.Monthly_Forecast__c.year() , m ,  bd.cs.Monthly_Forecast__c.day() );
			   System.debug('The value is: ' + cs.Monthly_Forecast__c ); 
               selectedBD.add(billlingDetail);
           } 
       }
       
         
          update selectedBD;
             
       
       
     
       return null;
       
   }
    
    


    
    public class WrapperClass {
        public Billing_Detail__c  cs {get; set;}
        public Boolean check_box {get; set;}
        
        public WrapperClass(Billing_Detail__c  c, Boolean check_box){
            this.cs = c;
            this.check_box = false;
        }
    }
}

 

All Answers

Ahmed Ansari 13Ahmed Ansari 13
Hii  Frank Carter
I have updated Above Code I think it works for you :
 
public class SearchWithWrapperC {
    
    public  Date StartDate {get;set;}
    public  Date EndDate {get;set;}
  
    
    public List<WrapperClass> listBD {get;set;}
   
    
    public void loadData() {
        listBD = new List<WrapperClass>();
        for(Billing_Detail__c  cr : [Select Id,SF_Opportunity_Id__c,Account_Name__c,Billing_Detail__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c   from Billing_Detail__c Where Billing_Status__c='Authorized for Billing'AND Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate  Order by Account_Name__c, Monthly_Forecast__c]){
            listBD.add(new WrapperClass (cr, false));
        }
    }
    
        public List<WrapperClass> getBilling() {
            if(listBD == null) {

        listBD = new List<WrapperClass>(); 
        for(Billing_Detail__c  cr : [Select Id,SF_Opportunity_Id__c,Account_Name__c,Billing_Detail__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c   from Billing_Detail__c Where Billing_Status__c='Authorized for Billing'AND Monthly_Forecast__c>=:StartDate AND Monthly_Forecast__c <= :EndDate  Order by Monthly_Forecast__c ]){
            listBD.add(new WrapperClass (cr, false));
        }
        }       
         return listBD;
        }
    
        
   public PageReference processSelected() {
       Integer m = Date.Today().Month();
       List<Billing_Detail__c> selectedBD = new List<Billing_Detail__c>();
       for(WrapperClass bd: getBilling()){
	        Billing_Detail__c billlingDetail ;
           if(bd.check_box== true){
		   
		        billlingDetail = new Billing_Detail__c(Id=bd.cs.Id);
		      billlingDetail.Monthly_Forecast__c = Date.newinstance(  bd.cs.Monthly_Forecast__c.year() , m ,  bd.cs.Monthly_Forecast__c.day() );
			   System.debug('The value is: ' + cs.Monthly_Forecast__c ); 
               selectedBD.add(billlingDetail);
           } 
       }
       
         
          update selectedBD;
             
       
       
     
       return null;
       
   }
    
    


    
    public class WrapperClass {
        public Billing_Detail__c  cs {get; set;}
        public Boolean check_box {get; set;}
        
        public WrapperClass(Billing_Detail__c  c, Boolean check_box){
            this.cs = c;
            this.check_box = false;
        }
    }
}

 
This was selected as the best answer
Frank CarterFrank Carter
Can you help me with th rerender function? 
<apex:page controller="SearchWithWrapperC" docType="html-5.0" id="pg">
        <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");                  
            for(var i=0; i<inputCheckBox.length; i++){          
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){                                     
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock title="Billing Details Search Page" >
            <br/>
            <br/>
            <apex:actionRegion >
                <center>
                    <apex:outputLabel value="Start Date"/>
                    <apex:input value="{!StartDate}" type="date" /><br/>
                    <apex:outputLabel value="End Date "/>
                    <apex:input value="{!EndDate}" type="date" /> 
                </center>
            </apex:actionRegion>  
            <br/>
            
            <div align="center" draggable="false" >            
                <apex:commandButton value="Search" action="{!loadData}"  style="text-align:left;" />               
                <apex:commandButton value="Moves to next month" style="text-align:left;" onclick="if(!confirm('If you click ok all the selected billing details will be paid for the current month. Are you sure? ')) return false;" action="{!processSelected}" reRender="pagination"/>  
            </div>        
        </apex:pageBlock>
        
        <apex:pageBlock id="pagination">
            <apex:variable var="listnotnull" value="{!listBD != null}" />
            <apex:pageBlockTable value="{!listBD}" var="b" >
                <apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')" rendered="{! listnotnull }"/>
                    </apex:facet>
                    <apex:inputCheckbox value="{!b.check_box}" id="inputId" />
                </apex:column>
                
                <apex:column value="{!b.cs.Name}"/>
                <apex:column value="{!b.cs.SF_Opportunity_Id__c}"/>
                <apex:column style="width:110px" value="{!b.cs.Account_Name__c}"/>
                <apex:column style="width:130px" value="{!b.cs.Billing_Detail__c}"/>
                <apex:column value="{!b.cs.Billing_Type__c}"/>
                <apex:column value="{!b.cs.billing_period__c}"/>    
                <apex:column value="{!b.cs.Monthly_Forecast__c}"/>
                <apex:column value="{!b.cs.End_of_Billing__c}"/>
                <apex:column value="{!b.cs.Amount__c}"/>
                <apex:column value="{!b.cs.Billing_Status__c}"/>   
                <apex:column >                   
                    <apex:outputLink title="" value="/{!b.cs.Id}/e?retURL=/apex/{!$CurrentPage.Name}" target="_blank" style="font-weight:bold">EDIT</apex:outputLink>
                </apex:column>    
            </apex:pageBlockTable>   
            
        </apex:pageBlock>        
    </apex:form>
</apex:page>

When the users click the Moves o the next buton the results doesn't update. I tried addding rerender with id Pagination but nothing.
Can you help me, please?

thanks
Ahmed Ansari 13Ahmed Ansari 13
give the Id atteribute  to the <apex:form >  i think it will wotk for You