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
er.rajhanschaubeyer.rajhanschaubey 

Apex Cpu Limit Exceeded. How to use my class as a batch to avoid this issue.

public with sharing class updateAssetController {

    private list<ASSETS__c> asset;
    private list<AUDIT__c> audit;
   private list<AggregateResult> aggResult;
    private Integer Total;
    private String str; 
    private list<answerData> answer;
    private map<String,list<answerData>> ansData ;
    private map<String,Map<String,list<answerData>>> containsAllData ;
    private Integer Totalcount=0;
    private list<answerData>  filAns;
 
   
    public updateAssetController(){
  
       
    }
   
    
    public void allRecord(){
    
       
        asset = [Select id,name from ASSETS__c];
       
        list<String> nameList = new list<String>{'MAKE__c','COUNTRY__c'} ;
           
        ansData = new Map<String,List<answerData>>();
       
        for(String fieldName :nameList){
        
         String query = 'SELECT count(Id) myCount,'+fieldName+',ASSETS__c ';
                query+=  ' FROM AUDIT__c  GROUP BY '+fieldName+',ASSETS__c';
        
         aggResult = Database.query(query);
         
          for(AggregateResult ar:aggResult){
          
             String question;
             Total =  (Integer)ar.get('myCount');
             str = (String)ar.get(fieldName);
           
             question = fieldName.replaceAll('__c','');
            
             if(!ansData.containsKey(question)){
               
                 answer =new List<answerData>();
                 answer.add(new answerData(Total,str,(String)ar.get('ASSETS__c')));
                 ansData.put((question),answer);
             }
             else{
             
                 list<answerData> existingvalue = ansData.get((question));
                 existingvalue.add(new answerData(Total,str,(String)ar.get('ASSETS__c')));
             }
          }
        }
       
        containsAllData = new Map<String,Map<String,List<answerData>>>();
               
        for(ASSETS__c au :asset){
        
         map<String,list<answerData>> filterData = new map<String,list<answerData>>();
        
            for (String s : ansData.keySet()){
            
                list<answerData>  filAns =new list<answerData>();
               
                for(answerData anData : ansData.get(s)){
                
                 if(anData.Auid == au.id){
                 
                  filAns.add(new answerData(anData.a,anData.result,anData.Auid));                        
                        filterData.put(s,filAns);
                 }
                }
            }
            containsAllData.put(au.id,filterData);
        }
       
        Set<ASSETS__c> allAsset =  new Set<ASSETS__c>();
        map<String,List<answerData>> relatedRecord = new map<String,list<answerdata>>();
       
        for (String str : containsAllData.keySet()){
        
            ASSETS__c a = new ASSETS__c(id=str);
            relatedRecord = containsAllData.get(str);
            for(String assign: relatedRecord.keyset()){
            
             if(Assign=='MAKE'){
                 Integer firstInt =0;
                    Integer second =0;
                    String firstValue;
                    String secondValue;
                    Totalcount=0;
                   
                    for(answerData st:relatedRecord.get(assign)){
                    
                     Totalcount +=st.a;
                        if(firstInt == 0|| firstInt<st.a){
                         if(st.result != null && st.a> firstInt){
                                firstInt = st.a;
                                firstValue = st.result;
                            }else if(st.result == null && st.a> firstInt){
                                second =st.a;
                                secondValue =st.result;
                            }
                           
                        }
                    }
                    if(firstInt > 1){
                     a.MAKE__c = firstValue;
                    }
                    else{
                     a.MAKE__c = secondValue;
                    }
             }
               
                else if(Assign=='COUNTRY'){
                 Integer firstInt =0;
                    Integer second =0;
                    String firstValue;
                    String secondValue;
                    Totalcount=0;
                   
                    for(answerData st:relatedRecord.get(assign)){
                    
                        Totalcount +=st.a;
                        if(firstInt == 0|| firstInt<st.a){
                         if(st.result != null && st.a> firstInt){
                                firstInt = st.a;
                                firstValue = st.result;
                            }else if(st.result == null && st.a> firstInt){
                                second =st.a;
                                secondValue =st.result;
                         }
                        }
                    }
                          
                    if(firstInt > 1){
                     a.COUNTRY__c = firstValue;
                    }
                    else{
                     a.COUNTRY__c = secondValue;
                    }
                }
               
            }
            allAsset.add(a);
        }
        list<ASSETS__c> assetRecord =new list<ASSETS__c>();
        assetRecord.addALL(allAsset);
      
       
        update assetRecord;
     
        
    }  
   
    public class answerData {
        
        public Integer a;
        public String Result; 
        private String Auid;    
      
        public answerData(Integer a, String Result,String AssetId) {
            this.a =a;
            this.Result=Result;
            auid = AssetId;
        }
    }
  
}
Arunkumar RArunkumar R
Hi ,

Since your coding contains SOQL query within the for loop. This type of codes takes too much of time to process your records. 

CPU Limits are,
Synchronous --> 10,000ms
Asynchronous --> 60,000ms

Please follow the below link for coding standards and write your query outside for loop.

https://developer.salesforce.com/page/Apex_Code_Best_Practices
David "w00t!" LiuDavid "w00t!" Liu
This chapter here will tell you everything you need to know about making your code more efficient! 
Especially in this case where you have a SOQL query in a loop!

http://www.sfdc99.com/beginner-tutorials/#governorLimits

Basically, you need to use Maps  =)