0

I try to load 200 data and run test class for my trigger.my test class fail due to APEX CPU TIME Limit .I read on static variable but it is work on bulk upload and if I am using Trigger framework that consist many method should i create one static variable to determine each method has run or not?

The trigger is fire onBefore insert

 public static void updateOpportunityCampaign (List<Opportunity> newOppList) 
  {

      String strToCompare ='NonSense -';
      for (Opportunity opp :newOppList)
       {
        if(String.isNotBlank(opp.Campaign.Name))
        {
            String campaignName = opp.Campaign.Name ;
            system.debug('@@@ campaignName = '+campaignName);
            if (campaignName.contains(strToCompare))
             {
                opp.CampaignId =null;

            }
        }
      }

  }

Here is my test class

static testMethod void  testUpdateOpportunityCampaign()
    {   
        Test.startTest();
        List<Account> lstAccount =[Select Name,Id from Account where Name ='Account 1'];

        system.assertEquals(1,lstAccount.size());

        //retrieve campaign
        List<Campaign> lsCampaign = [Select Id,Name from Campaign where Name like 'ABC -%' Limit 200]; 

        system.assertEquals(200,lsCampaign.size());

        List<Opportunity> lsOpportunity = new List<Opportunity>();

        //insert Opportunity and tie it with campaign to test insertion.
        for(Integer i=0;i<lsCampaign.size();i++)
        {
            String opoName = 'Opp Campaign '+(i+1);

            if(i <=66)
            {
                Opportunity op = new Opportunity(Name=opoName, CampaignId=lsCampaign[i].Id, StageName='Create',
                                                 CloseDate=Date.TODAY(), amount=0,AccountId=lstAccount.get(0).Id); 

                lsOpportunity.add(op);
            }
            else if(i >67 && i <=132)
            {
                Opportunity op = new Opportunity(Name=opoName, CampaignId=lsCampaign[i].Id, StageName='Create',
                                                 CloseDate=Date.TODAY(), amount=0,AccountId=lstAccount.get(0).Id,
                                               );

                lsOpportunity.add(op);

            }
            else {

                Opportunity op = new Opportunity(Name=opoName, CampaignId=lsCampaign[i].Id, StageName='Create',
                                                 CloseDate=Date.TODAY(), amount=0,AccountId=lstAccount.get(0).Id,
                                               );

                lsOpportunity.add(op);
            }

        }

        system.debug('@@@@ lsOpportunity '+ lsOpportunity.size());

        insert lsOpportunity;

        List<Opportunity> listInsertedOpp =[select Id ,Name from Opportunity where Name like 'Opp Campaign%'];

        system.assertEquals(true,listInsertedOpp.size()>0);

        listInsertedOpp =[select Id ,Name from Opportunity where Name like 'Opp Campaign%' and CampaignId != null];

        system.assertEquals(0,listInsertedOpp.size());

        Test.stopTest();

    }

Debug log snippet

05:59:35.559 (84559508274)|TESTING_LIMITS
05:59:35.559 (84559508274)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 22 out of 100
  Number of query rows: 404 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 4 out of 150
  Number of DML rows: 600 out of 10000
  Maximum CPU time: 24210 out of 10000 ******* CLOSE TO LIMIT
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

05:59:35.559 (84559508274)|LIMIT_USAGE_FOR_NS|rh2|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

05:59:35.559 (84559508274)|CUMULATIVE_LIMIT_USAGE_END

05:59:36.480 (85480988704)|FATAL_ERROR|System.LimitException: Apex CPU time limit exceeded

(rh2)
1

You can try to separate the transaction by placing Test.startTest and Test.stopTest after creating the data.

Like this way in your test class:

//create test data before this
    Test.startTest();

    insert lsOpportunity;

    List<Opportunity> listInsertedOpp =[select Id ,Name from Opportunity where Name like 'Opp Campaign%'];

    system.assertEquals(true,listInsertedOpp.size()>0);

    listInsertedOpp =[select Id ,Name from Opportunity where Name like 'Opp Campaign%' and CampaignId != null];

    system.assertEquals(0,listInsertedOpp.size());

    Test.stopTest();
  • Unfortunately not working.=( – unidha Jun 28 '16 at 6:23
  • @unidha Not working or you are getting the CPU Timeout?? – Mr.Frodo Jun 28 '16 at 6:24
  • I put the code as you mentioned but still throwing CPU exception. – unidha Jun 28 '16 at 6:26
  • @unidha Only this code doesn't seem able to throw CPU time out.There is something else in the behind which is consuming the CPU cycles.Investigate your debug log.Paste screenshot if it is possible. – Mr.Frodo Jun 28 '16 at 6:31
  • update with debug log, i investigate rh2, it is Rollup Helper managed package. – unidha Jun 28 '16 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.