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
Ty WhitfieldTy Whitfield 

Governor Limit Question

I have a Visualforce page using a StandardController with an extension.  Everything works fine but when there are more than 100 items, and I hit the Save (updateSave()) button, I initialliy received the  "Apex governor limit warning".  I then changed the updateSave() to what is below and I then received Too many future calls: 51.

VFP
<apex:page standardController="Asset" docType="html-5.0"   lightningStylesheets="true" recordSetVar="assets" extensions="AssetUpdateController" tabStyle="Asset">
  <!-- Begin Default Content REMOVE THIS -->
<apex:form >
    <apex:pageBlock >
      <apex:pageMessages />
      <center>
       <apex:selectList value="{!productVersionItem}"   size="1" id="typeSelect">               
                  <apex:selectOptions value="{!dropProductVersionItems}"/>
                   <apex:actionSupport event="onchange"  action="{!rePopulateAzzets}"  >
                   </apex:actionsupport>                   
                </apex:selectList>
                </center><br />
      <apex:pageBlockButtons >
        <apex:commandButton value="Mark Listed Items as Inactive" 
                            action="{!groupMarkInactive}"/>
                             <apex:commandButton id="saveBtn" value="Save" action="{!updateSave}" />

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

      </apex:pageBlockButtons>
      <apex:pageBlockTable value="{!azzets}" 
                           var="ass">
       
        <apex:column headerValue="Asset Name">
     
          <apex:outputField value="{!ass.Name}"/>
        </apex:column>
        <apex:column headerValue="Quantity">
          <apex:outputField value="{!ass.Quantity}"/>
        </apex:column>
        
         <apex:column headerValue="PakSize">
          <apex:outputField value="{!ass.PakSize__c}"/>
        </apex:column>
         <apex:column headerValue="Product Family">
          <apex:outputField value="{!ass.ProductFamily}"/>
        </apex:column>
         <apex:column headerValue="Serial Number">
          <apex:outputField value="{!ass.SerialNumber}"/>
        </apex:column>
         <apex:column headerValue="Satus">
          <apex:inputField value="{!ass.Status}"/>
        </apex:column>
         <apex:column headerValue="Price">
          <apex:outputField value="{!ass.Price}"/>
        </apex:column>
         <apex:column headerValue="Purchase Date">
          <apex:outputField value="{!ass.PurchaseDate}"/>
        </apex:column>
      </apex:pageBlockTable>      
    </apex:pageBlock>
  </apex:form>
  <!-- End Default Content REMOVE THIS -->
</apex:page>

Controller
public  class AssetUpdateController {
      // TestAssetUpdateController
    @AuraEnabled
    public List<Asset> azzets{get;set;}   
    public String AcctId {get;set;} 
    public String productFamilyId {get;set;} 
    Public String productVersionItem{get;set;}
    Public List<SelectOption> dropProductVersionItems {get;set;}
    
    public AssetUpdateController(ApexPages.StandardSetController controller) {
        AcctId = System.currentPageReference().getParameters().get('Id');
        productFamilyId= System.currentPageReference().getParameters().get('PFId');
        getAzzets();
        
    }
    
    
    public List<Asset> getAzzets() {
        
        azzets = [Select Name, PurchaseDate, Price, Status, SerialNumber, ProductFamily, PakSize__c, Quantity 
                  From Asset where Account.id = :AcctId order by ProductFamily ASC];  
        
        dropProductVersionItems = new List<SelectOption>();
        dropProductVersionItems.add(new SelectOption('Filter By Version','Filter By Version'));
        
        //  List <Asset> productFamily = [SELECT ProductFamily  From Asset where Account.id = :AcctId Group by ProductFamily order by ProductFamily DESC];
        
        
        integer i = 0;
        while(i <  azzets.size())
        { 
            if (i==0) {
                dropProductVersionItems.add(new SelectOption(azzets[i].ProductFamily,azzets[i].ProductFamily));
                
            }else{
                if( (azzets[i].ProductFamily != azzets[i-1].ProductFamily))
                {
                    dropProductVersionItems.add(new SelectOption(azzets[i].ProductFamily,azzets[i].ProductFamily));
                }
                
            }
            
            i++;
        }           
        return azzets;    
        
    }
    
    public void rePopulateAzzets() {
        
        azzets = [Select Name, PurchaseDate, Price, Status, SerialNumber, ProductFamily, PakSize__c, Quantity 
                  From Asset where Account.id = :AcctId and ProductFamily = :productVersionItem];  
        
        
        
    }
    
    public void groupMarkInactive() {
        
        Integer i =0;
        while(i <  azzets.size())
        {
            azzets[i].Status = 'Inactive';
            i++;
        }               
        
    }
    
    public Pagereference updateSave() {  
        PageReference newpage = new Pagereference('/lightning/r/'+AcctId + '/related/Assets/view');
        try{
           // new code to work around Govenor Limit
            for(List<Asset> azzetsList : azzets)
            {
                for(Asset aAsset : azzetsList)
                {

                }
                Database.update(azzets);
            }

         // end new code to eliminate Governor Limit

        // Code below works but will cause Governor limit error if too many 
            // azzets.save();
           
            
        }catch(exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,e.getMessage()));
        }             
        
        newpage.setRedirect(true);        
        return newpage;
        
    }
    
    public Pagereference cancel(){
        
        PageReference newpage = new Pagereference('/'+AcctId);
        
        newpage.setRedirect(true);
        
        return newpage;
        
    }
}

​​​​​​​
NehaDabas16NehaDabas16
Well, you have used a DML inside loop, you are bound to hit limits.
Check line no. 79 "Database.update(azzets);". Take this out of loop, and in fact you can remove those loops as there is no logic inside them.

If this helps mark as resolved.
Ty WhitfieldTy Whitfield
If I'm understanding your response correctly, I'm still receiving the same error with this code for the updateSave()
public Pagereference updateSave() {  
        PageReference newpage = new Pagereference('/lightning/r/'+AcctId + '/related/Assets/view');
        try{      
             Database.update(azzets);       
            
        }catch(exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,e.getMessage()));
        }             
        
        newpage.setRedirect(true);        
        return newpage;
        
    }

 
Ty WhitfieldTy Whitfield
Just reiterate, the error message that I'm receiving now is  "Too many future calls: 51".  There are a  total number of items 64 items but then I filter to selected of 56.