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
Vishnu7700Vishnu7700 

Apex CPU Time out Exception

Hi,
While checking the product duplicate i'm faceing Apex CPU Time out limit Exception.Here is the code
public list<BD_Identifier__c> LegacyproductBDlistinputcheck(list<BD_Identifier__c> bdlist)//bdllist contains list of products
    {
        set<string> dupcodeinput = new set<string>();
        system.debug('Class duplicate count:'+bdlist.size());//Size was 456 records
        for(BD_Identifier__c b: bdlist)
        {
            system.debug('Class duplicate:'+dupcodeinput.size());
            if (dupcodeinput.size()>0)
            {
                while (dupcodeinput.contains(b.SPM_BrokerDealer__C+b.SPM_Legacy_Product_Code__c))//After processing 64 records time out error was shown
                {
                    
                    if(b.SPM_BrokerDealer__c=='FSC')
                    {
                        string code =b.SPM_Legacy_Product_Code__c.right(1);
                        string productcode= b.SPM_Legacy_Product_Code__c;
                        if(code.isNumeric())
                        {
                            Integer i;
                            if(b.SPM_Legacy_Product_Code__c.right(2).isNumeric())
                            {
                                string s=b.SPM_Legacy_Product_Code__c.right(2);
                                Integer c,d;
                                c=Integer.valueOf(s.right(1));
                                d=Integer.valueOf(s.left(1));
                                if(d==c-1){
                                    code=b.SPM_Legacy_Product_Code__c.right(1); 
                                }
                                else{code=b.SPM_Legacy_Product_Code__c.right(2);}
                            }
                            i=Integer.valueOf(code)+1;
                            b.SPM_Legacy_Product_Code__c=((productcode+String.valueOf(i)).right(7));
                        }
                        else
                        {
                            b.SPM_Legacy_Product_Code__c=((productcode+String.valueOf(1)).right(7));
                        }
                    }
                    else if (b.SPM_BrokerDealer__c=='RAA' || b.SPM_BrokerDealer__c=='SPF' || b.SPM_BrokerDealer__c=='WFS')
                    {
                        string code =b.SPM_Legacy_Product_Code__c.right(1);
                        string productcode= b.SPM_Legacy_Product_Code__c;
                        if(code.isNumeric())
                        {
                            Integer i;
                            if(b.SPM_Legacy_Product_Code__c.right(2).isNumeric())
                            {
                                string s=b.SPM_Legacy_Product_Code__c.right(2);
                                integer c,d;
                                c=Integer.valueOf(s.right(1));
                                d=Integer.valueOf(s.left(1));
                                if(d==c-1){
                                    code=b.SPM_Legacy_Product_Code__c.right(1); 
                                }
                                else{code=b.SPM_Legacy_Product_Code__c.right(2);}
                            }
                            i=Integer.valueOf(code)+1;
                            b.SPM_Legacy_Product_Code__c=(b.SPM_Legacy_Sponsor_Code__c +(productcode+String.valueOf(i)).right(6));
                        }
                        else
                        {
                            b.SPM_Legacy_Product_Code__c=(b.SPM_Legacy_Sponsor_Code__c +(productcode+String.valueOf(1)).right(6));
                        }
                    }
                }
                dupcodeinput.add(b.SPM_BrokerDealer__C+b.SPM_Legacy_Product_Code__c);
            }
            else {dupcodeinput.add(b.SPM_BrokerDealer__C+b.SPM_Legacy_Product_Code__c);}
        }
        //system.debug('class remove duplicate'+bdlist);
        return bdlist;
    }
Blake TanonBlake Tanon
How many records are you updating at once?
Vishnu7700Vishnu7700
Hi Blake,
Record processed are 500 to 1000
Blake TanonBlake Tanon
CPU time outs are hard to track down - it's probably not related solely to thsi code but a lot of other things running also.  Try to only run no more than 200 records at a time.
Vishnu7700Vishnu7700
You recommand batch class? or can we split while condition?
while (dupcodeinput.contains(b.SPM_BrokerDealer__C+b.SPM_Legacy_Product_Code__c))
Blake TanonBlake Tanon
While is a dangerous thing to use in salesforce.  I would move it to a batch.
Vishnu7700Vishnu7700
Can you help me out by moveing code to batch class? And how to call this batch from class?