1

My test class is

public static testMethod void loanSetDelegatedApprovers() {

          list<User> userList = new list<User>();
    list<Approval_Delegate__c> adList = new list<Approval_Delegate__c>();

    User userObj;

    Approval_Delegate__c adObj;
    Integer i;
    Integer j;
    Profile p = [SELECT Id FROM Profile WHERE Name='Super User'];

    for(i=1;i<5;i++) {
        userObj = new User(
                            Alias = 'App_' + i
                            , LastName = 'App_' + i
                            , Email = 'approver_' + i + '@testorg.com'
                            , UserName = 'approver_' + i + '@testorg.com'
                            , EmailEncodingKey = 'UTF-8'
                            , LanguageLocaleKey = 'en_US' 
                            , LocaleSidKey = 'en_US'
                            , ProfileId = p.Id 
                            , TimeZoneSidKey = 'America/Los_Angeles'
                            , RACF_ID__c = i + 'RAC'
                            , User_Type__c = 'Commercial'
                        );

        userList.add(userObj);

        for(j=1;j<11;j++) {
            userObj = new User(
                                Alias = 'Ap_' + i + '_D' + j
                                , LastName = 'Ap_' + i + '_D' + j
                                , Email = 'approver_' + i + '_Delegate_' + j + '@testorg.com'
                                , UserName = 'approver_' + i + '_Delegate_' + j + '@testorg.com'
                                , EmailEncodingKey = 'UTF-8'
                                , LanguageLocaleKey = 'en_US' 
                                , LocaleSidKey = 'en_US'
                                , ProfileId = p.Id 
                                , TimeZoneSidKey = 'America/Los_Angeles'
                                , RACF_ID__c = i + 'RAC' + j
                                , User_Type__c = 'Commercial'
                            );
            userList.add(userObj);
        }
    }    

    User u1 = userList[0] ; 

System.runAs(u1){
  userList.remove(0); 
  insert userList;
}
    userList.add(0,u1) ;  

    for(i=0;i<4;i++) {
        adObj = new Approval_Delegate__c();
        j = i + i*10;
        adObj.Approver__c = userList[j].id;

        System.RunAs(userList[j]) {
            insert adObj;
        }

        adlist.add(adObj);
    }

    //insert adList;                        

    System.RunAs(userList[0]) {
      list<id> tempIdList = new list<id>();
      LOS_Trigger_Management__c lCus= new LOS_Trigger_Management__c();
      lCus.Name='systemDebugStatements';
      lCus.Enabled__c=false;
      insert lCus;       
      System.assertNotEquals(lCus,null);
    }   

    Test.startTest();
    list<opportunity> oppyList = new list<opportunity>();
    list<LLC_BI__Loan__c> loanList = new list<LLC_BI__Loan__c>();
    Opportunity oppyObj;
    LLC_BI__Loan__c loanObj;

    RecordType rtObj = [SELECT id FROM RecordType WHERE name = 'Opportunity' and SObjectType = 'Opportunity']; //'012S00000000hIx'

    //added by pulkit
    System.RunAs(userList[1])
    {
     NAICS_Description__c naics = new NAICS_Description__c();
    naics.Name = 'testNaics';
    naics.NAICS_Code__c = '123456';
    insert naics;

    Account accObj = new Account();
    accObj.name = 'TestAccount';
    accObj.Name = 'testEntity';
    accObj.BillingCountry = 'India';
    accObj.BillingStreet = 'testStreet';
    accObj.BillingCity = 'Delhi';
    accObj.COMPASS_NAICS_Code__c = '123456';
    accObj.COMPASS_NAICS_Description__c = naics.Id;
    accObj.SSN__c = '111111';
    accObj.Tax_ID__c = '213123199';
    accObj.Social_Tin_Exception__c = 'No Exception';
    accObj.ShippingCountry = 'India';
    accObj.ShippingStreet = 'testStreet';
    accObj.ShippingCity = 'Delhi';
    accObj.TOE__c = 123;
    accObj.Bank_Number__c = '80';
    accObj.Obligor__c = '123456';
    insert accObj;        

    oppyObj = new Opportunity();
    oppyObj.name = 'Test';
    oppyObj.recordTypeId = rtObj.id;
    oppyObj.closeDate = date.today();
    oppyObj.Initial_Application_Source__c = 'Not Applicable';
    oppyObj.StageName = '1 - 10% - Identify Opportunity';
    oppyObj.accountId = accObj.id;
    oppyObj.STI_Product_Line__c = 'Commercial';
    oppyObj.STI_Product_Type__c = 'Secured RE';
    oppyObj.STI_Product__c = 'Line of Credit';
    oppyList.add(oppyObj);

    oppyObj = new Opportunity();
    oppyObj.name = 'Test';
    oppyObj.recordTypeId = rtObj.id;
    oppyObj.closeDate = date.today();
    oppyObj.Initial_Application_Source__c = 'Not Applicable';
    oppyObj.StageName = '1 - 10% - Identify Opportunity';
    oppyObj.accountId = accObj.id;
    oppyObj.STI_Product_Line__c = 'Commercial';
    oppyObj.STI_Product_Type__c = 'Secured RE';
    oppyObj.STI_Product__c = 'Line of Credit';
    oppyList.add(oppyObj);

    oppyObj = new Opportunity();
    oppyObj.name = 'Test';
    oppyObj.recordTypeId = rtObj.id;
    oppyObj.closeDate = date.today();
    oppyObj.Initial_Application_Source__c = 'Not Applicable';
    oppyObj.StageName = '1 - 10% - Identify Opportunity';
    oppyObj.accountId = accObj.id;
    oppyObj.STI_Product_Line__c = 'Commercial';
    oppyObj.STI_Product_Type__c = 'Secured RE';
    oppyObj.STI_Product__c = 'Line of Credit';
    oppyList.add(oppyObj);

    oppyObj = new Opportunity();
    oppyObj.name = 'Test';
    oppyObj.recordTypeId = rtObj.id;
    oppyObj.closeDate = date.today();
    oppyObj.Initial_Application_Source__c = 'Not Applicable';
    oppyObj.StageName = '1 - 10% - Identify Opportunity';
    oppyObj.accountId = accObj.id;
    oppyObj.STI_Product_Line__c = 'Commercial';
    oppyObj.STI_Product_Type__c = 'Secured RE';
    oppyObj.STI_Product__c = 'Line of Credit';      
    oppyList.add(oppyObj);

    insert oppyList;
    Test.stopTest();

    loanObj = new LLC_BI__Loan__c();
    loanObj.LLC_BI__Account__c = accObj.id;
    loanObj.name = 'Test Loan 1';
    loanObj.Binding_Non_Binding__c = '1';
    loanObj.New_Money__c = 123;
    loanObj.Current_Outstanding__c = 123;
    loanObj.Current_Approval_Status__c = 'First Approval';
    loanObj.Unused__c = 123;
    loanObj.LLC_BI__Account__c = accObj.Id;
    loanObj.Initial_Application_Date__c = Date.today();
    loanObj.Obligation_Number__c = '123456';
    loanObj.LLC_BI__Stage__c = '1 - 10% - Identify Opportunity';
    loanList.add(loanObj);

    loanObj = new LLC_BI__Loan__c();
    loanObj.LLC_BI__Account__c = accObj.id;
    loanObj.Initial_Application_Date__c = Date.today();
    loanObj.name = 'Test Loan 2';
    loanObj.Binding_Non_Binding__c = '1';
    loanObj.New_Money__c = 123;
    loanObj.Current_Outstanding__c = 123;
    loanObj.Unused__c = 123;
    loanObj.LLC_BI__Account__c = accObj.Id;
    loanObj.Obligation_Number__c = '123456';
    loanObj.LLC_BI__Stage__c = '1 - 10% - Identify Opportunity';
    loanList.add(loanObj);

    loanObj = new LLC_BI__Loan__c();
    loanObj.LLC_BI__Account__c = accObj.id;
    loanObj.Initial_Application_Date__c = Date.today();
    loanObj.name = 'Test Loan 3';
    loanObj.Binding_Non_Binding__c = '1';
    loanObj.New_Money__c = 123;
    loanObj.Current_Outstanding__c = 123;
    loanObj.Unused__c = 123;
    loanObj.LLC_BI__Account__c = accObj.Id;
    loanObj.Obligation_Number__c = '123456';
    loanObj.LLC_BI__Stage__c = '1 - 10% - Identify Opportunity';
    loanList.add(loanObj);

    loanObj = new LLC_BI__Loan__c();
    loanObj.LLC_BI__Account__c = accObj.id;
    loanObj.name = 'Test Loan 4';
    loanObj.Initial_Application_Date__c = Date.today();
    loanObj.Binding_Non_Binding__c = '1';
    loanObj.New_Money__c = 123;
    loanObj.Current_Outstanding__c = 123;
    loanObj.Unused__c = 123;
    loanObj.LLC_BI__Account__c = accObj.Id;
    loanObj.Obligation_Number__c = '123456';
    loanObj.LLC_BI__Stage__c = '1 - 10% - Identify Opportunity';
    loanList.add(loanObj);

    insert loanlist;

 /* oppyList[0].nCino_Loan__c = loanList[0].id;  //Commented for change Control           
    oppyList[1].nCino_Loan__c = loanList[1].id;
    oppyList[2].nCino_Loan__c = loanList[2].id;
    oppyList[3].nCino_Loan__c = loanList[3].id;*/ 


    oppyList[0].LLC_BI__Loan__c = loanList[0].id;             
    oppyList[1].LLC_BI__Loan__c = loanList[1].id;
    oppyList[2].LLC_BI__Loan__c = loanList[2].id;
    oppyList[3].LLC_BI__Loan__c = loanList[3].id;

    update oppyList;       

    loanList[0].LLC_BI__Stage__c = '5 - 90% - Accepted in Documentation';       
    loanList[1].LLC_BI__Stage__c = '1 - 10% - Identify Opportunity';
    loanList[2].LLC_BI__Stage__c = '2 – 25% - Prepare and Develop Proposal';
    loanList[3].LLC_BI__Stage__c = '3 - 50% - Develop and Deliver Solution';        


    loanList[0].Closing_Date__c = Date.newInstance(2015,07,05);
    loanList[1].Closing_Date__c = Date.newInstance(2015,07,01);
    loanList[2].Closing_Date__c = Date.newInstance(2015,07,02);
    loanList[3].Closing_Date__c = Date.newInstance(2015,07,03);

    loanList[0].SunTrust_Request_Amount__c = 100;               
    loanList[1].SunTrust_Request_Amount__c = 100;
    loanList[2].SunTrust_Request_Amount__c = 100;
    loanList[3].SunTrust_Request_Amount__c = 100;

    loanList[0].First_Approver__c = adList[0].Approver__c;

    loanList[1].Second_Approver__c = adList[1].Approver__c;
    loanList[1].Current_Approval_Status__c = 'Second Approval';

    loanList[2].Third_Approver__c = adList[2].Approver__c;
    loanList[2].Current_Approval_Status__c = 'Third Approval';

    loanList[3].Final_Approver_Assigned_To__c = adList[3].Approver__c;
    loanList[3].Current_Approval_Status__c = 'Final Approval';    

    System.runAs(userList[0]){
      update loanList;
    }


    i=0;

    for(Approval_Delegate__c temp : adList) {
        j = i + i*10;
        temp.Approver__c = userList[j].id;

        temp.First_Delegate__c = userList[j+1].id;
        temp.Second_Delegate__c = userList[j+2].id;
        temp.Third_Delegate__c = userList[j+3].id;
        temp.Fourth_Delegate__c = userList[j+4].id;
        temp.Fifth_Delegate__c = userList[j+5].id;
        temp.Sixth_Delegate__c = userList[j+6].id;
        temp.Seventh_Delegate__c = userList[j+7].id;
        temp.Eighth_Delegate__c = userList[j+8].id;
        temp.Ninth_Delegate__c = userList[j+9].id;
        temp.Tenth_Delegate__c = userList[j+10].id;

        System.RunAs(userList[j]) {
            update temp;
        }

        i = i+1;
    }

    //update adList;
    update loanList;

    LLC_BI__Product_Package__c creditPkgRecord = new LLC_BI__Product_Package__c ();
    creditPkgRecord.LLC_BI__Account__c = accObj.Id;
    //creditPkgRecord.First_Approver__c = adList[0].Approver__c;
    //creditPkgRecord.Second_Approver__c = adList[1].Approver__c;
    //creditPkgRecord.Third_Approver__c = adList[2].Approver__c;
    //creditPkgRecord.Final_Approver_Assigned_To__c = adList[3].Approver__c;
    creditPkgRecord.Current_Approval_Status__c = 'First Approval';    
    insert creditPkgRecord;

    creditPkgRecord.Current_Approval_Status__c = 'Second Approval';
    update creditPkgRecord;

    creditPkgRecord.Current_Approval_Status__c = 'Third Approval';
    update creditPkgRecord;

    creditPkgRecord.Current_Approval_Status__c = 'Final Approval';
    update creditPkgRecord;
 }  
}

I tried editing the test.starttest and test.stoptest method but was facing the same error

@testSetup static void setup(){

LOS_Trigger_Management__c lCus= new LOS_Trigger_Management__c();
    lCus.Name='systemDebugStatements';
    lCus.Enabled__c=false;
    insert lCus;        
    System.assertNotEquals(lCus,null);  
    User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
    System.runAs(thisUser ){

    Profile p1 = [SELECT Id FROM Profile WHERE Name='SAD Underwriter'];

      UserRole r = [select id from UserRole where name = 'SAD Private'];
    //UserRole r2 = [select id from UserRole where name = 'CIB Teammate'];
    User testUser= new User(alias='neUser', email='newTestinguser@test.com', emailencodingkey='UTF-8', lastname='Testing', username='newTestinguser2@test.com',
                               EmployeeNumber='123456780',profileid=p1.id, languagelocalekey='en_US', localesidkey='en_US', timezonesidkey='America/Los_Angeles'
                               ,user_type__c='CIB',Product_Home_Center_updated__c=true,RACF_ID__c = '123045',userroleid  = r.id
                               );

    insert testUser;  

    Officer_Number__c offno1= new Officer_Number__c();
    offno1.name='Test';
    offno1.Officer_First_Name__c='test1';
    offno1.Officer_Number__c='1234';
    offno1.HO_Rel_Mgr_Type__c = 'A';
    insert offno1;

    Account entityRecord = new Account();
    entityRecord.Name = 'testEntity9';
    entityRecord.Account_Owner_Officer__c=offno1.id;
    entityRecord.BillingCountry = 'India';
    entityRecord.BillingStreet = 'testStreet2';
    entityRecord.BillingCity = 'testCity';
    entityRecord.BillingPostalCode='123475';

    entityRecord.ShippingCountry = 'India';
    entityRecord.ShippingStreet = 'testStreet2';
    entityRecord.ShippingCity = 'Delhi';
    entityRecord.ShippingPostalCode='123452';

    entityRecord.Obligor__c = '1234575';
    entityRecord.STExternalCommercialKey__c = '1234556';
    entityRecord.Bank_Number__c = '123467';

    insert entityRecord;

    Client_Coverage__c cov= new Client_Coverage__c();
    cov.Account__c  =entityRecord.id;
    cov.Team_Member__c=userinfo.getuserID();
    cov.Responsibility__c='Coverage Banker';
    insert cov;

    RecordType rtObj = [SELECT id FROM RecordType WHERE name = 'Asset Finance' and SObjectType = 'Opportunity']; //'012S00000000hIx'

    Opportunity oppyObj = new Opportunity();
    oppyObj.name = 'Test';
    oppyObj.recordTypeId = rtObj.id;
    oppyObj.closeDate = date.today();
    oppyObj.Initial_Application_Source__c = 'Not Applicable';
    oppyObj.StageName = '1 - Identify Opportunity';
    oppyObj.accountId = entityRecord.id;
    oppyObj.STI_Product_Line__c = 'Commercial';
    oppyObj.STI_Product_Type__c = 'Secured RE';
    oppyObj.STI_Product__c = 'Line of Credit';
    oppyObj.Opportunity_LOB__c='CIB';
    //oppyList.add(oppyObj);
    insert oppyObj;


    LOS_Trigger_Management__c CCus= new LOS_Trigger_Management__c();
    CCus.Name='CreditActionByPass';
    CCus.Enabled__c=true;
    insert CCus;

    System.assertNotEquals(CCus,null);

    //Inserting Credit Package
     LLC_BI__Product_Package__c creditPkgRecord1 = new LLC_BI__Product_Package__c ();
     creditPkgRecord1.LLC_BI__Account__c = entityRecord.Id;
     insert creditPkgRecord1;

     System.assertNotEquals(creditPkgRecord1,null);
    //}

    creditPkgRecord1.OwnerId=testUser.id;
    update creditPkgRecord1; 

    Cost_Center__c cc = new Cost_Center__c();
    cc.LOB__c='CIB';
    cc.Sub_LOB_ID__c='p';
    insert cc;


       LLC_BI__Loan__c highestFacilityRecord11= new LLC_BI__Loan__c();
    highestFacilityRecord11.Name = 'highestFacilityRecord';
    highestFacilityRecord11.LLC_BI__Account__c = entityRecord.Id;
    highestFacilityRecord11.Opportunity__c = oppyObj.id;
    highestFacilityRecord11.Opportunity_LOB__c='CIB';
    highestFacilityRecord11.Visible_only_to_Loan_Team__c=true;
    highestFacilityRecord11.Cost_Center__c=cc.id;
    highestFacilityRecord11.llc_bi__product_package__c=creditPkgRecord1.Id;
    highestFacilityRecord11.SunTrust_Request_Amount__c = 123;
    //highestFacilityRecord11.Initial_Application_Date__c = Date.today();
    highestFacilityRecord11.Customer_Requested_Amount__c = 1000;
    highestFacilityRecord11.Facility_Type__c = '1000';
    highestFacilityRecord11.Revolving_Non_Revolving__c = '1';
    highestFacilityRecord11.Short_Name__c = 'testShortName';
    highestFacilityRecord11.LLC_BI__Structure__c = 'Multi Level Future';
    highestFacilityRecord11.LLC_BI__Structure_Hierarchy__c = 'Main';
    highestFacilityRecord11.Collateral_Stage_Indicator_PreClosing__c = false;
    highestFacilityRecord11.Collateral_Stage_Indicator_UW__c = false;
    highestFacilityRecord11.LLC_BI__Stage__c  = 'Pre-Underwriting';
    highestFacilityRecord11.Closing_Hub__c = 'Atlanta';
    highestFacilityRecord11.LLC_BI__Close_Date_Hard__c = true;
    highestFacilityRecord11.Obligation_Number__c = '12345697';
    highestFacilityRecord11.LC_Cap_Amount__c = 10;
    highestFacilityRecord11.Doc_Prep_Method__c = '1';
    highestFacilityRecord11.Late_Charge_Code__c = '352';
    highestFacilityRecord11.Lending_State__c = 'NC';        
    highestFacilityRecord11.Binding_Non_Binding__c = 'Binding';
    highestFacilityRecord11.Effective_Date__c = system.today();
    //highestFacilityRecord11.Request_Type__c = '22';
    //highestFacilityRecord11.Renew_Review_Type__c='22';
    //highestFacilityRecord11.LLC_BI__Is_Modification__c=true;
    highestFacilityRecord11.SAD__c=true;

    insert highestFacilityRecord11;       

     System.assertNotEquals(highestFacilityRecord11,null);
   }  

Error is coming at line no 87 RecordType rtObj = [SELECT id FROM RecordType WHERE name = 'Opportunity' and SObjectType = 'Opportunity'];

  • 4
    What line is your error occurring on, and in what class? Also, dml inside of loops is bad practice. Also also, is it possible for you to break out the test data setup for this test method into a @testSetup annotated method? Not sure if it's intended or not, but you can completely remove the governor limit cost of test data setup by using a @testSetup method and using test.startTest() and test.stopTest() in it (while still allowing you to use start/stop test in your test method). – Derek F Aug 13 at 13:33
  • 2
    Also also also, the test you've provided to us seems to (at first glance) try to do waaaay too much work. Is it possible to break this test into several smaller tests? – Derek F Aug 13 at 13:34
  • What is it that you're actually trying to test here? Is it the loan approval process? If so, you may want to consider creating static test data an importing the data into your unit test. It appears that you may have too many triggers executing when all this data is created to get this deep into your schema to do this within limits unless you either a) import data or b) use mocks to create it. Import is much easier. – crmprogdev Aug 13 at 13:59
  • yes its the loan approval process we are testing and also there are 2 triggers executing in back. We often get this error for different test classes when we do run all tests – Eagerin Sf Aug 13 at 14:15
  • It looks like this is part of a test factory of records. Do you have multiple test setups? If you have a good test setup and run your queries within start.starttest() you should be fine. Also as another person said, if you have multiple tests, that can make it easier on you. – disasterlooms Aug 13 at 17:57

Your Answer

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

Browse other questions tagged or ask your own question.