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
Vladimir BessonovVladimir Bessonov 

Avoiding Apex CPU - HOW?

I am trying to solve the puzzle how to avoid CPU time error

my code is below. 
on the internet a lot of recommendations and examples with maps. 
But I am creating new records and need to iterate over quantities to create records. 

so what I shall do? 
 
public with sharing class BatchCreateNonSerializedParts implements Database.Batchable<sObject> {
    String ProjectConfigID;
    String ProjectID;
    Integer VehicleFleetStartNumber;
    ComponentProjectConfiguration__c ProjectConfig;

    List<NonSerializedPart__c> NonSerPartsConfig;
    List<project_Non_Serialized_Part__c> NonSerParts = new List<project_Non_Serialized_Part__c>();
    List<Acceptance__c> NonSerPartAcceptances = new List<Acceptance__c>();  


    public BatchCreateNonSerializedParts(String ProjectConfigID, String ProjectID) {
        this.ProjectConfigID = ProjectConfigID;
        this.ProjectID = ProjectID;

        ProjectConfig = [Select ID, Name, Vehicle_fleet_start_number__c from ComponentProjectConfiguration__c where ID =: ProjectConfigID];
        VehicleFleetStartNumber = Integer.valueOf(ProjectConfig.Vehicle_fleet_start_number__c);

        NonSerPartsConfig = [Select ID, Name, Number_of_units__c, Quantity__c, Unit_of_measurement__c, Material_Number__c, Supplier_Part_Number__c, Supplier__c from NonSerializedPart__c where Project__c =: ProjectConfigID];
        

        for (NonSerializedPart__c part : NonSerPartsConfig) {
        Double Total = part.Number_of_units__c * part.Quantity__c;
        project_Non_Serialized_Part__c NewPart = New project_Non_Serialized_Part__c(Name = part.Name, Project__c =ProjectID,
        Material_Number__c = part.Material_Number__c, Supplier__c = part.Supplier__c, 
        Supplier_Part_Number__c = part.Supplier_Part_Number__c, 
        Total_Quantity__c = Total ,Unit_of_measurement__c = part.Unit_of_measurement__c,
        Number_of_units__c = part.Number_of_units__c, unit_Quantity__c = part.Quantity__c);

        NonSerParts.add(NewPart);
        
        }
       // insert NonSerParts; 

        Database.SaveResult[] srList = Database.insert(NonSerParts, false);
            
        // Iterate through each returned result
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted NonPartsSets: ');
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('NonPartsSets fields that affected this error: ' + err.getFields());
                }

                return;
            }
        }

     // Create Acceptance object    

        for(project_Non_Serialized_Part__c part : NonSerParts){ 
                
            for(Integer i=0;i<part.Number_of_units__c;i++){
                    Acceptance__c newAcceptance = new Acceptance__c(Non_Serialized_Part__c = part.id, Quantity__c = part.unit_Quantity__c,
                    Unit_of_measurement__c = part.Unit_of_measurement__c, Acceptance_Non_Serialized_Part__c = part.Name, Status__c = 'NOT SENT');  
                    NonSerPartAcceptances.add(newAcceptance);
                }
        
        }

    }
    
    public Iterable<SObject> start(Database.BatchableContext bc) {

       return NonSerPartAcceptances;
    }
   
    public static void execute(Database.BatchableContext bc, List<Acceptance__c> NonSerPartAcceptances) {

        for(Acceptance__c a : NonSerPartAcceptances){ 
            insert a;
        }   
    }
    public void finish(Database.BatchableContext bc){

        
        System.debug('Succesfully inserted Non Serialized parts and Acceptances');
    }


}

another batch class 
 
public with sharing class BatchCreateSerializedParts implements Database.Batchable<sObject> {
        String ProjectConfigID;
        String ProjectID;
        Integer VehicleFleetStartNumber;
        ComponentProjectConfiguration__c ProjectConfig;
    
        List<SerializedPartsSet__c> SerPartsSetConfig;
        List<project_Serialized_Parts_Set__c> SerPartsSets = new List<project_Serialized_Parts_Set__c>();
        List<project_Serialized_Part__c> SerParts = new List<project_Serialized_Part__c>();
    
        public BatchCreateSerializedParts(String ProjectConfigID, String ProjectID) {
            //this.ProjectConfigID = ProjectConfigID;
            //this.ProjectID = ProjectID;
                // not Batch operations 
            ProjectConfig = [Select ID, Name, Vehicle_fleet_start_number__c from ComponentProjectConfiguration__c where ID =: ProjectConfigID];
            VehicleFleetStartNumber = Integer.valueOf(ProjectConfig.Vehicle_fleet_start_number__c);
            SerPartsSetConfig = [Select ID, Name, Quantity__c from SerializedPartsSet__c where Project__c =: ProjectConfigID];

            for (SerializedPartsSet__c partSet : SerPartsSetConfig) {
    
                //   Double Total = part.Number_of_units__c * part.Quantity__c;
                   project_Serialized_Parts_Set__c NewPartSet = New project_Serialized_Parts_Set__c(Name = partSet.Name, Project__c =ProjectID, Quantity__c = partSet.Quantity__c);
                     SerPartsSets.add(NewPartSet);
                   } 
             
           // try {insert SerPartsSets;} catch (system.Dmlexception e) { system.debug (e);}
            
                        // DML statement
            Database.SaveResult[] srList = Database.insert(SerPartsSets, false);
            
            // Iterate through each returned result
            for (Database.SaveResult sr : srList) {
                if (sr.isSuccess()) {
                    // Operation was successful, so get the ID of the record that was processed
                    System.debug('Successfully inserted SerPartsSets: ');
                }
                else {
                    // Operation failed, so get all errors                
                    for(Database.Error err : sr.getErrors()) {
                        System.debug('The following error has occurred.');                    
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        System.debug('SerPartsSets fields that affected this error: ' + err.getFields());
                    }

                    return;
                }
            }
            
            // iterate over Part Sets to create all Serialized parts

            for(project_Serialized_Parts_Set__c partSet: SerPartsSets){ 
                    // ITERATE PART SETS. PROPULSION & TRUCK    
    
               // SerializedPartsSet__c ConfPartSet = [Select ID, Name, Project__c from SerializedPartsSet__c where Project__c =: ProjectConfigID AND Name =: partSet.Name];
                //List <SerializedPart__c> SerPartsConfig = [Select ID, Name, Quantity__c, Material_Number__c, Supplier_Part_Number__c, Supplier__c from SerializedPart__c where SerializedPartSet__c =: ConfPartSet.ID];
                List <SerializedPart__c> SerPartsConfig = [Select ID, Name, Quantity__c, Material_Number__c, Supplier_Part_Number__c, Supplier__c from SerializedPart__c where SerializedPartSet__c =: [Select ID from SerializedPartsSet__c where Project__c =: ProjectConfigID AND Name =: partSet.Name].id ];
                    // ITERATE OVER SETS - for example 400
                //System.debug(SerPartsConfig);

                for(Integer i=0; i<partSet.Quantity__c;i++) {
        
                    for(SerializedPart__c part: SerPartsConfig){
        
                
                        if( part.Quantity__c == 1) {
                            system.debug('PART QUANTITY is 1:' + part.Quantity__c ); 
                            
                            project_Serialized_Part__c newPart = new project_Serialized_Part__c(
                            Name= part.Name, Quantity__c = part.Quantity__c , Material_Number__c = part.Material_Number__c, 
                            Set_Number__c = i + VehicleFleetStartNumber, Status__c = 'NOT SENT',
                            Supplier_Part_Number__c = part.Supplier_Part_Number__c, 
                            Supplier__c = part.Supplier__c, Serialized_Part_Set__c = partSet.ID); 
                        
                            SerParts.add(newPart);}
                            else {
                                system.debug('PART QUANTITY is > 1:' + part.Quantity__c );
                                for(Integer j=0; j<part.Quantity__c; j++) {
                                    project_Serialized_Part__c newPart = new project_Serialized_Part__c(
                                    Name= part.Name, Quantity__c = 1 , Material_Number__c = part.Material_Number__c, 
                                    Set_Number__c = i + VehicleFleetStartNumber, Status__c = 'NOT SENT',
                                    Supplier_Part_Number__c = part.Supplier_Part_Number__c, 
                                    Supplier__c = part.Supplier__c, Serialized_Part_Set__c = partSet.ID); 
                                
                                    SerParts.add(newPart);
                                }
                            }
        
                        }
        
                    }
                }         
        }
    
        public Iterable<SObject> start(Database.BatchableContext bc) {
            return SerParts;
        }
        
        public static void execute(Database.BatchableContext bc, List<project_Serialized_Part__c> SerParts) { 
            for(project_Serialized_Part__c p : SerParts){ 
                insert p;
            }
        }
    
        public void finish(Database.BatchableContext bc){
            System.debug('Succesfully inserted Serialized parts and Sets');
        }



    }

 
PriyaPriya (Salesforce Developers) 

Hi Vladimir,

Can you specify the error you are facing in this code? (Also mention line number if possible)

Regards,

Priya Ranjan