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
mahesh1.396434879374128E12mahesh1.396434879374128E12 

System.LimitException: Apex CPU time limit exceeded

When i am tring to bulkify this trigger i am getting this error:

When a new case is created, the externalId value is equal to Product external Id. Th product id value shoul map to the Product__c Custom field in case.


The code:

trigger CreateReqMapProdId on Case (Before Insert)
{  
    list<string> lstExteralId = New list<string>();
    if(trigger.isInsert)
        {  
            for(Case ca : trigger.new)
            {
                lstExteralId.add(ca.ProductExteralId__c);
            }
            Map<Id, Product2> ProdMap = new Map<Id, Product2>([select id,PSCSID__c from Product2]);
                for(Case ca : trigger.new)
                        {  
                            for(Product2 prod : ProdMap.values())
                            {
                            If(ca.ProductExteralId__c != null)
                            {
                               If(ca.ProductExteralId__c == Prod.PSCSID__c)
                                {
                                    ca.Product__c = Prod.Id ;
                                }
                            }
                                                     
                        }
                   
        }
}}


Please help me.
 
Ankit AroraAnkit Arora
Now rather having script statement limit we've new limit which is CPU time limit. So what I can guess is either too many cases are inserted or there are too many Product records in your org. Once try using limit in your query and let me know if it throws any exception. If not then it's clearly the bulk data issue.
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Check this code
trigger CreateReqMapProdId on Case (Before Insert)

if(trigger.isInsert)

List<Product2> ProdMap = [select id,PSCSID__c from Product2 Where PSCSID__c!=null];
for(Case ca : trigger.new)
{
If(ca.ProductExteralId__c != null)
{
for(i=0;i<ProdMap.size();i++)
{
If(ca.ProductExteralId__c == ProdMap[i].PSCSID__c)
{
ca.Product__c = ProdMap[i].Id ;
}
}
}
}
}
}
mahesh1.396434879374128E12mahesh1.396434879374128E12
Hi Ankith,

too many Product records are iterating when i am using to get the data which is having ProductExteralId__c == PSCSID__c matching values.
mahesh1.396434879374128E12mahesh1.396434879374128E12
Hi Vidhyasagaran,

I have used this code.

It is also throwing the same error .

Please help in bulkfying the above code.
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
change this line:List<Product2> ProdMap = [select id,PSCSID__c from Product2 Where PSCSID__c!=null LIMIT 100];
 
and try
mahesh1.396434879374128E12mahesh1.396434879374128E12
Hi Vidyasagaram,

This is my latest Code: 

trigger CreateReqMapProdId on Case (Before Insert)
{  
    list<string> lstExteralId = New list<string>();
    if(trigger.isInsert)
        {  
            for(Case ca : trigger.new)
            {
                lstExteralId.add(ca.ProductExteralId__c);
            }
            Map<string, Product2> ProdMap = new Map<string, Product2>();
            list<Product2> Prodlst =[select id,PSCSID__C from product2 where PSCSID__C IN:lstExteralId];
            for(Product2 p :Prodlst)
            {
              ProdMap.put(p.PSCSID__C,p);
            }
                for(Case ca : trigger.new)
                        {  
                           
                            If(ca.ProductExteralId__c != null)
                            {
                                    ca.Product__c = (ProdMap.get(ca.ProductExteralId__c)).id ;
                            }
                        }
        }
}

I am facing the above Error . when i am inserting the records above 100 (Governor limits).


mahesh1.396434879374128E12mahesh1.396434879374128E12
Hi Vidyasagaram,

This is my latest Code: 

trigger CreateReqMapProdId on Case (Before Insert)
{  
    list<string> lstExteralId = New list<string>();
    if(trigger.isInsert)
        {  
            for(Case ca : trigger.new)
            {
                lstExteralId.add(ca.ProductExteralId__c);
            }
            Map<string, Product2> ProdMap = new Map<string, Product2>();
            list<Product2> Prodlst =[select id,PSCSID__C from product2 where PSCSID__C IN:lstExteralId];
            for(Product2 p :Prodlst)
            {
              ProdMap.put(p.PSCSID__C,p);
            }
                for(Case ca : trigger.new)
                        {  
                           
                            If(ca.ProductExteralId__c != null)
                            {
                                    ca.Product__c = (ProdMap.get(ca.ProductExteralId__c)).id ;
                            }
                        }
        }
}

I am facing the above Error . when i am inserting the records above 100 (Governor limits).


Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
try this
trigger CreateReqMapProdId on Case (Before Insert)

Set<id> lstExteralId = New set<id>();
for(Case ca : trigger.new)
{
lstExteralId.add(ca.ProductExteralId__c);
}
Map<id,Product2> ProdMap = new Map<id, Product2>([select id,PSCSID__C from product2 where PSCSID__C IN:lstExteralId]);
for(Case ca : trigger.new)

If(ca.ProductExteralId__c != null)
{
Product2 p = ProdMap.get(ca.ProductExteralId__c);
If(p.PSCSID__C!=null)
{
ca.Product__c = p.PSCSID__C ;
}
}
}
}