+ Start a Discussion
DTCFDTCF 

Technique to avod "Too many script statements"? when populating lookup field

 

I have an object A with a lookup field to another object B.  There are approximately 500 records of object A in the database. I want to populate that lookup field in those existing records with the appropriate record relationship to Object B. Object B already has a lookup field to object A, so I need to check to see if Object B looks up to Object A, and if it does, I need to associate that record to Object A.

 

 

This is already inside an @future call. I'm getting the too many script statements error. Any advice would be most appreciated!

 

  @future
    public static void addCurrentRPPA() {
        
        
        Active_Project__c[] projects = [select id,name from Active_Project__c];
        
        Active_Project__c[] updateProjects = new Active_Project__c[0];
        
        for (Active_Project__c proj:projects) {
            
            String projId = proj.id;
            
            
            Rate_Profile_Project_Assignment__c rp;
            
            for (Rate_Profile_Project_Assignment__c rate:rateAssignments) {
            
            if (rate.Projects__c==proj.id) {
                rp = rate;
            }            
            
            
            
        }
        
            if (rp!=null) {
            
            proj.rate_profile_project_assignment__c = rp.id;
            updateProjects.add(proj);
            }
            
        }
        
        update updateprojects;
        
    }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Shailesh DeshpandeShailesh Deshpande

use maps....that will avoid for loops...therby reducing the no. of script statements....

All Answers

Shailesh DeshpandeShailesh Deshpande

use maps....that will avoid for loops...therby reducing the no. of script statements....

This was selected as the best answer
symantecAPsymantecAP

 

public class ProductPriceBook1 {



    public class productInfo {
    
        public Product2 prod{get; set;}
        public List<PriceBook2> priceBooks {get; Set;}
        public productInfo() {
        priceBooks = new List<PriceBook2>();
        
        }
        
    }
    
    public List<ProductInfo> products {get; set;}
    
    public ProductPriceBook1() {
        products = new List<productInfo >();
       
        List<Product2> prods = [Select Name, Description, Family, id from Product2 limit 1000];
        
        List<Id>prodIds = new List<Id>();
        for (Product2 p : prods)
                prodIds.add(p.id);
       
        List<PricebookEntry> pEntries = [Select id, product2Id, pricebook2id from PricebookEntry where product2Id in :prodIds ];
          List<id> pbIds = new List<Id>();
        for (PriceBookEntry pe : pEntries)
                   pbIds.add(pe.pricebook2Id);
        Map<Id, Pricebook2> priceBooks = new Map<id, Pricebook2>([Select id, name from PriceBook2 where id in :pbIds ]);
        
        
        for (Product2 prod : prods ) {
            
            ProductInfo pInfo = new ProductInfo();
            pInfo.prod = prod;
            for (PricebookEntry pe : pEntries)  {
                if (pe.product2Id == prod.id) {
                    pInfo.pricebooks.add(priceBooks.get(pe.pricebook2id));
                }
            }
            products.add(pInfo );
            
        }
        
        
        
        
    }
}

 

I am getting the same error please help me 

 

 

System.LimitException: Too many script statements: 200001