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
FinneyFinney 

Help Please - System.LimitException: Too many SOQL queries: 101

Hi , 

I am getting an error when trying to deploy a class to production.

The error is on line 10 of the trigger

Please find the trigger posted here.

This is the Trigger
trigger OTC_TrgPopulateKeyContactDetails on Contract (before insert,before update) {

    List<Id> accountIds = new List<Id>(); 
    List<Id> contactIds = new List<Id>();
    for(Contract cont : Trigger.New){
        accountIds.add(cont.AccountId);
        if(cont.Key_Contact__c != null)
            contactIds.add(cont.Key_Contact__c);
    }
    Map<Id,Account> accMap = new Map<Id,Account>([Select Id,Name,PersonEmail,Passport__pc,OTC_E_ID_Upload__pc,PersonMobilePhone,Phone,IsPersonAccount,FirstName,LastName,(Select Id,Email,MobilePhone,FirstName,LastName,Phone,Passport__c,OTC_E_ID_Upload__c from Contacts where OTC_Key_Contact__c = true) from Account where Id = :accountIds]);
    Map<Id,Contact> conMap = new Map<Id,Contact>();
    if(contactIds.size() > 0) conMap = new Map<Id,Contact>([Select Id,Email,MobilePhone,FirstName,LastName,Phone,Passport__c,OTC_E_ID_Upload__c from Contact where Id = :contactIds]);
    
    for(Contract cont : Trigger.New){
        if(cont.Key_Contact__c != null && conMap.containsKey(cont.Key_Contact__c)){
            Contact con = conMap.get(cont.Key_Contact__c);
            cont.KeyContact_Email_id__c = con.Email;
            cont.KeyContact_MobileNumber__c = con.MobilePhone;
            cont.OTC_Key_Contact_Name__c = con.FirstName == null ? con.LastName : con.FirstName+' '+con.LastName;
            cont.KeyContact_Office__c = con.Phone;
            cont.E_ID_only_for_Jobs__c = con.OTC_E_ID_Upload__c;
            cont.OTC_Passport_Copy_Person_Signing_Cheque__c = con.Passport__c; 
        } else{
            if(accMap.get(cont.AccountId).IsPersonAccount == true){
                cont.KeyContact_Email_id__c = accMap.get(cont.AccountId).PersonEmail;
                cont.KeyContact_MobileNumber__c = accMap.get(cont.AccountId).PersonMobilePhone; 
                cont.OTC_Key_Contact_Name__c = accMap.get(cont.AccountId).Name;
                cont.KeyContact_Office__c = accMap.get(cont.AccountId).Phone;
                cont.OTC_Passport_Copy_Person_Signing_Cheque__c = accMap.get(cont.AccountId).Passport__pc;
                cont.E_ID_only_for_Jobs__c = accMap.get(cont.AccountId).OTC_E_ID_Upload__pc;
            }else if(accMap.get(cont.AccountId).Contacts.size()>0){
                cont.KeyContact_Email_id__c = accMap.get(cont.AccountId).Contacts[0].Email;
                cont.KeyContact_MobileNumber__c = accMap.get(cont.AccountId).Contacts[0].MobilePhone;
                cont.OTC_Key_Contact_Name__c = accMap.get(cont.AccountId).Contacts[0].FirstName == null ? accMap.get(cont.AccountId).Contacts[0].LastName : accMap.get(cont.AccountId).Contacts[0].FirstName+' '+accMap.get(cont.AccountId).Contacts[0].LastName;
                cont.KeyContact_Office__c = accMap.get(cont.AccountId).Contacts[0].Phone;
                cont.E_ID_only_for_Jobs__c = accMap.get(cont.AccountId).Contacts[0].OTC_E_ID_Upload__c;
                cont.OTC_Passport_Copy_Person_Signing_Cheque__c = accMap.get(cont.AccountId).Contacts[0].Passport__c;
            }
        }
    }

}
I have posted the class as a reply

Thanks
Best Answer chosen by Finney
Jyothy KiranJyothy Kiran
Hi Fenni,

Try to call query insert and update correctly inside test.starttest and test.stoptest
Because test.starttest and test.stoptest method will avoid governor limit exception in test class..


Othervice split the test class into multiple test methods....

 

All Answers

FinneyFinney
The class is 
  1. @isTest
  2. Private class test_TrgBounceCount{
  3.     
  4.     static testMethod void OTC_myUnitTest(){
  5.         
  6.         
  7.         Profile p = [select id from profile where name='Dubizzle Standard User'];
  8.         
  9.         User u = new User(
  10.             IsActive=True, 
  11.             Alias= 'Dubiz',
  12.             email='ETest@testorg.com',
  13.             emailencodingkey='UTF-8',
  14.             lastname='Testing',
  15.             languagelocalekey='en_US',
  16.             localesidkey='en_US',
  17.             profileid = p.Id,
  18.             timezonesidkey='America/Los_Angeles',
  19.             username='eTes1@testorg.com',
  20.             CommunityNickname= 'Dub');
  21.        
  22.         insert u;
  23.         
  24.         
  25.         ID AccRecordTypeID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('OTC-Person Accounts').getRecordTypeId();
  26.         ID BusinessAccRecordTypeID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('OTC- Business Account').getRecordTypeId();
  27.         ID Contract_RecordTypeID = Schema.SObjectType.Contract.getRecordTypeInfosByName().get('Standard Listings').getRecordTypeId();
  28.         
  29.         Account ObjAccount = new Account(
  30.             LastName='Test Account',
  31.             RecordTypeID=AccRecordTypeID,
  32.             OTC_Blacklisted_Customer__c=false,
  33.             PersonMobilePhone='234567',
  34.             Phone='456',
  35.             PersonEmail='test@gmail.com',
  36.             Passport__pc=true,
  37.             OTC_E_ID_Upload__pc=true);
  38.         insert ObjAccount;
  39.         
  40.         Account ObjBAccount = new Account(Name='Business Account',RecordTypeID=BusinessAccRecordTypeID,OTC_Blacklisted_Customer__c=false,Phone='456');
  41.         insert ObjBAccount;
  42.         
  43.         ObjAccount.PersonMobilePhone= '3333';
  44.         ObjAccount.PersonEmail = 'abc@gmail.com';
  45.         ObjAccount.Phone = '222';
  46.         update ObjAccount;
  47.         
  48.         Contact ObjContact = new Contact(
  49.             LastName='hjk',
  50.             FirstName='dfg',
  51.             email='xyz@gmail.com',
  52.             AccountID=ObjBAccount.id,
  53.             OTC_Key_Contact__c=true,
  54.             MobilePhone='23456');
  55.         insert ObjContact;
  56.         
  57.         ObjContact.MObilePhone ='1111';
  58.         ObjContact.phone = '345';
  59.         ObjContact.Email = 'ssg@gmail.com';
  60.         ObjContact.FirstName='hh';
  61.         ObjContact.LastName='bbb';
  62.         update ObjContact;
  63.         
  64.         Product2 ObjProduct = new Product2(Name = 'Laptop', Family = 'Adaptation');
  65.         insert ObjProduct;
  66.         
  67.         Id pricebookId = Test.getStandardPricebookId();
  68.         
  69.         PricebookEntry standardPrice = new PricebookEntry(
  70.             Pricebook2Id = pricebookId, Product2Id = ObjProduct.Id,
  71.             UnitPrice = 10000, IsActive = true);
  72.         insert standardPrice;
  73.         Test.startTest();
  74.         //Contract ObjContract = [Select id from Contract where ContractNumber='00000103' limit 1];
  75.         Contract ObjContract = new Contract();
  76.         try{
  77.             ObjContract = new Contract(
  78.                 AccountID=ObjAccount.ID,
  79.                 RecordTypeID=Contract_RecordTypeID,StartDate=system.today(),ContractTerm=12,OTC_Price_Book__c=pricebookId);
  80.             system.debug('Account===>'+ObjContract.AccountID);
  81.             system.debug('OTC_Price_Book__c===>'+ObjContract.OTC_Price_Book__c);
  82.             system.debug('Contract==>'+ObjContract);
  83.             insert ObjContract;
  84.         }catch(Exception e){system.debug('Exception Occured-->'+e.getMessage());}
  85.     
  86.         ObjContract.Contract_Type__c='Renewal';
  87.         ObjContract.CurrencyIsoCode='AED';
  88.         ObjContract.StartDate=Date.newInstance(2017, 3, 1);
  89.         ObjContract.ContractTerm=10;
  90.         ObjContract.Market__c='Jobs - Other';
  91.         ObjContract.Status = 'In Process';
  92.         ObjContract.OTC_Passport_Copy_Person_Signing_Cheque__c=true;
  93.         ObjContract.E_ID_only_for_Jobs__c=true;
  94.         ObjContract.ContractTerm = 12;
  95.         ObjContract.StartDate = system.today();
  96.         ObjContract.KeyContact_Email_id__c=ObjAccount.PersonEmail;
  97.         ObjContract.KeyContact_MobileNumber__c=ObjAccount.PersonMobilePhone;
  98.         ObjContract.OTC_Key_Contact_Name__c=ObjAccount.LastName;
  99.         ObjContract.KeyContact_Office__c = ObjAccount.Phone;
  100.         ObjContract.OTC_Passport_Copy_Person_Signing_Cheque__c = ObjAccount.Passport__pc;
  101.         ObjContract.E_ID_only_for_Jobs__c = ObjAccount.OTC_E_ID_Upload__pc;
  102.             
  103.         update ObjContract;
  104.         
  105.         Contract ObjContract2= new Contract();
  106.         try{
  107.             ObjContract2= new Contract(
  108.                 AccountID=ObjAccount.ID,
  109.                 RecordTypeID=Contract_RecordTypeID,StartDate=system.today(),ContractTerm=12,OTC_Price_Book__c=pricebookId);
  110.             system.debug('Account===>'+ObjContract.AccountID);
  111.             system.debug('OTC_Price_Book__c===>'+ObjContract.OTC_Price_Book__c);
  112.             system.debug('Contract==>'+ObjContract);
  113.             insert ObjContract2;
  114.         }catch(Exception e){system.debug('Exception Occured-->'+e.getMessage());}
  115.         
  116.         ObjAccount.PersonMobilePhone= '3333';
  117.         update ObjAccount;
  118.         
  119.         ObjContact.phone = '222';
  120.         ObjContact.MobilePhone ='34565';
  121.         ObjContact.Email = 'dfghg@gmail.com';
  122.         update ObjContact;
  123.         
  124.         List<Contract_Products_OTC__c> ContractPrdList = new List<Contract_Products_OTC__c>();
  125.         Contract_Products_OTC__c ObjContractPrd1 = new Contract_Products_OTC__c(
  126.             Associated_Contract__c=ObjContract.id,
  127.             OTC_Start_Date__c=system.today(),
  128.             OTC_End_Date__c=system.today().addDays(90),
  129.             OTC_Product_Name__c=ObjProduct.id);  
  130.         ContractPrdList.add(ObjContractPrd1);
  131.         Contract_Products_OTC__c ObjContractPrd2 = new Contract_Products_OTC__c(OTC_Status__c='Upgrade-Pending Approval',OTC_Discount__c=5,Associated_Contract__c=ObjContract.id,OTC_Start_Date__c=system.today(),OTC_End_Date__c=system.today().addDays(90),OTC_Product_Name__c=ObjProduct.id);
  132.         ContractPrdList.add(ObjContractPrd2);
  133.         insert ContractPrdList;
  134.         
  135.         ObjContractPrd2.OTC_Referenced_Contract_Products__c=ObjContractPrd1.id;
  136.         ObjContractPrd2.OTC_Status__c = 'Upgrade';
  137.         ObjContractPrd2.OTC_Discount__c = 2;
  138.         update ObjContractPrd2;
  139.         
  140.         OTC_Contract_Product_Line_Items__c ObjLine1 = new OTC_Contract_Product_Line_Items__c(
  141.             Start_Date__c=date.newInstance(2017, 2, 1),
  142.             OTC_Contract_Product__c=ObjContractPrd2.id,
  143.             OTC_Contract__c=ObjContract.id,
  144.             cheque_no__c='a1829' ,
  145.             Cheque_Status__C='Bounced',
  146.             End_Date__c = date.newInstance(2017, 2, 8)
  147.         );
  148.         
  149.         insert ObjLine1;
  150.         
  151.         ObjLine1.Cheque_Status__c='Bounced';
  152.         update ObjLine1;
  153.         
  154.         ObjLine1.Cheque_Status__c='Cleared';
  155.         update ObjLine1;
  156.         Test.stopTest();
  157.         
  158.         
  159.     }
  160. }
Jyothy KiranJyothy Kiran
Hi Fenni,

Try to call query insert and update correctly inside test.starttest and test.stoptest
Because test.starttest and test.stoptest method will avoid governor limit exception in test class..


Othervice split the test class into multiple test methods....

 
This was selected as the best answer
FinneyFinney
Thanks Jyothy Kiran.