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
Yoni LegacyYoni Legacy 

Insert Account List Item error

Hi Experts,

I created a class to insert Account List Item Record for every Affiliation child record. 

Here is the code, and it's working in a single record, but when I executed the code manual in developer console I got an error message: 
Error message

Here is my code below: I think I need to use map but I'm just new and still confused on using map.
 
public with sharing class Eisai_InsertAccListItem_cls {
    
    List<Affiliation_vod__c> affChildRecsList;
    List<Account_List_vod__c> acctListInDb;
    List<Account_List_Item_vod__c> newAccListItem = new List<Account_List_Item_vod__c>();
    Set<Id>usersIdSet = new Set<Id>();
    
    public Eisai_InsertAccListItem_cls(){
        
        affChildRecsList = new List<Affiliation_vod__c>([SELECT Id, From_Account_vod__c, To_Account_Value__c, OwnerId
                                                         FROM Affiliation_vod__c 
                                                         WHERE Parent_vod__c = False
                                                         AND OwnerId IN :ActiveUsers()]);
        
            System.debug('Child Affiliation Records count ' + affChildRecsList.size()); 
        
        
        acctListInDb = new List<Account_List_vod__c>([Select Id, Name, OwnerId FROM Account_List_vod__c 
                                                      WHERE OwnerId IN :ActiveUsers()]);
        
            System.debug('Account List Records count ' + acctListInDb.size());
        
        
        
        for(Affiliation_vod__c affChild : affChildRecsList){
            for(Account_List_vod__c acctList : acctListInDb){
               if((acctList.Name.substring(3) == affChild.To_Account_Value__c)
                  && (affChild.OwnerId == acctList.OwnerId)){
                       Account_List_Item_vod__c AccListItem = new Account_List_Item_vod__c();
                       AccListItem.Account_List_vod__c = acctList.Id;
                       AccListItem.Account_vod__c = affChild.From_Account_vod__c;
                       newAccListItem.add(AccListItem);
                 }
             }
         }
   
        
        try{        
            insert newAccListItem ;
            System.debug('New Account List Item Records: ' + newAccListItem);
        }catch(DMLException e){
            System.debug('exeption catch ' + e.getMessage()); 
        }
        
        
    }//End of Constructor   
    
    public Set<Id> ActiveUsers(){
        
       List<User> usersIdList;
        
        usersIdList = new List<User>([SELECT Id
                                      FROM User
                                      WHERE (Profile_Name_vod__c LIKE '%Eisai_Epilepsy%' 
                                      OR Profile_Name_vod__c LIKE '%Eisai_PrimaryCare%') 
                                      AND IsActive = TRUE]); 
        
        for(User users : usersIdList){
            usersIdSet.add(users.Id);  
        }
        return usersIdSet;
    }
    
} //End of Class

Thanks for the help in advance
Nitin Wader 21Nitin Wader 21
It seems no. of account records are more than 50,000 ?.. this is exceeding governer limit.

In your nested For loop you need to bulkify your code.

 
Nitin Wader 21Nitin Wader 21
Your because of so many for loops in single Apex class.. execution is exceeding time limit of 10,000 seconds. Please modify your code so that execution time can be reduced. 

Or execution time is high because no. of records which are coming in execution context are more hence total time is exceeding limit.

Please optimize and bulkify your code.\

Thanks,
Yoni LegacyYoni Legacy
Hi Nithin,

Can you give me a sample or to convert using MAP if possible?

Thanks, I'm just very new in apex coding actually this is the first class which I created.

Marion
Nitin Wader 21Nitin Wader 21
Check below code.

In your original code , call to get user ID list is redundant. Which make 2 SOQL calls to user object. 
Active user list population is one time activity and can be done in main object , this will save us from 2 SOQL calls.

See below code, test and let me know. 
Thanks,
public with sharing class Eisai_InsertAccListItem_cls {
    
    List<Affiliation_vod__c> affChildRecsList;  
    List<Account_List_vod__c> acctListInDb;
    List<Account_List_Item_vod__c> newAccListItem = new List<Account_List_Item_vod__c>();
    Set<Id>usersIdSet = new Set<Id>();
    List<User> usersIdList;
    
    public Eisai_InsertAccListItem_cls(){
        
              
        usersIdList = new List<User>([SELECT Id
                                      FROM User
                                      WHERE (Profile_Name_vod__c LIKE '%Eisai_Epilepsy%' 
                                      OR Profile_Name_vod__c LIKE '%Eisai_PrimaryCare%') 
                                      AND IsActive = TRUE]); 
        
        affChildRecsList = new List<Affiliation_vod__c>([SELECT Id, From_Account_vod__c, To_Account_Value__c, OwnerId
                                                         FROM Affiliation_vod__c 
                                                         WHERE Parent_vod__c = False
                                                         AND OwnerId IN :usersIdList); 

        
            System.debug('Child Affiliation Records count ' + affChildRecsList.size()); 
      
        
        acctListInDb = new List<Account_List_vod__c>([Select Id, Name, OwnerId FROM Account_List_vod__c 
                                                      WHERE OwnerId IN :usersIdList);
     
            System.debug('Account List Records count ' + acctListInDb.size());
        
        
        
        for(Affiliation_vod__c affChild : affChildRecsList){
            for(Account_List_vod__c acctList : acctListInDb){
               if((acctList.Name.substring(3) == affChild.To_Account_Value__c)
                  && (affChild.OwnerId == acctList.OwnerId)){
                       Account_List_Item_vod__c AccListItem = new Account_List_Item_vod__c();
                       AccListItem.Account_List_vod__c = acctList.Id;
                       AccListItem.Account_vod__c = affChild.From_Account_vod__c;
                       newAccListItem.add(AccListItem);
                 }
             }
         }
   
        
        try{        
            insert newAccListItem ;
            System.debug('New Account List Item Records: ' + newAccListItem);
        }catch(DMLException e){
            System.debug('exeption catch ' + e.getMessage()); 
        }
        
        
    }//End of Constructor