Skip to main content

I have created a test class that bulk inserts the records in vf page in queues.But my records are not inserting .It gives me errors

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccountAndContact: execution of BeforeInsert  caused by: System.NullPointerException: Attempt to de-reference a null object  Trigger.UpdateAccountAndContact: line 67, column 1: []

 

Above same error in my three methods-testAutoSetCallbackDate, testManuallySettingCallbackDate , testOpportunityFinder

 

System.NullPointerException: Attempt to de-reference a null object

 

and i have inserted system.debug statements to verify till where code reaches and i find out it is not inserting the records i bulk .

 

debugs.png

Means it is not displaying my 3rd debug 

 insert new Opportunity[]{              op1,op2, op3, op4, op5, op6, op7, op8, op9, op10, op12,op11     };         system.debug('opportunity insertion ends '); 

 

here is my complete test class

@IsTest

public with sharing class TestOpportunityFinderv2 {

@testSetup static void setupData(){

List<Trigger_Handler__c> lstTriggerhandler= new List<Trigger_Handler__c>();

lstTriggerhandler.add(new Trigger_Handler__c(Name='AfterTrigger',isActive__c = false));

lstTriggerhandler.add(new Trigger_Handler__c(Name='BeforeTrigger',isActive__c = true));

lstTriggerhandler.add(new Trigger_Handler__c(Name='CallBatch',isActive__c = true));

lstTriggerhandler.add(new Trigger_Handler__c(Name='DuplicateOppMerge',isActive__c = false));

insert lstTriggerhandler;

system.debug('after triggers');

Opportunity op1 = new Opportunity( Name='Op1', CloseDate = Date.newInstance(2016, 08, 3), Stage_Count__c = 0, StageName = 'New' ,Lead_Status__c='Following',Call_Back_Date__c = Date.newInstance(2016, 08, 2), Call_back_time__c = Time.newInstance( 3, 3, 3, 0 ) , In_Queue__c = false);

Opportunity op2 = new Opportunity( Name='Op2', CloseDate = Date.today(), Call_back_Date__c = Date.today(), Stage_Count__c = 5, StageName = 'New' ,Lead_Status__c='Following');

Opportunity op3 = new Opportunity( Name='Op3', CloseDate = Date.today(), Call_back_Date__c = Date.today(), Stage_Count__c = 1, StageName = 'New',Lead_Status__c='Following' );

Opportunity op4 = new Opportunity( Name='Op4', CloseDate = Date.today(), Call_back_Date__c = Date.today() - 3, Stage_Count__c = 1, StageName = 'New',Lead_Status__c='Following' );

Opportunity op5 = new Opportunity( Name='Op5', CloseDate = Date.today(), Last_Call__c = Date.today() - 5, Opportunity_Score__c = 100, Stage_Count__c = 7, StageName = 'New',Lead_Status__c='Following' );

Opportunity op6 = new Opportunity( Name='Op6', CloseDate = Date.today(), Call_back_Date__c = Date.today() - 3, Stage_Count__c = 5, StageName = 'New',Lead_Status__c='Following' );

Opportunity op7 = new Opportunity( Name='Op7', CloseDate = Date.today(), Call_back_Date__c = Date.today() - 2, Stage_Count__c = 5, StageName = 'New',Lead_Status__c='Following' );

Opportunity op8 = new Opportunity( Name='Op8', CloseDate = Date.today(), Last_Call__c = Date.today() - 10, Stage_Count__c = 4, StageName = 'New',Lead_Status__c='Following' );

Opportunity op9 = new Opportunity( Name='Op9', CloseDate = Date.today(), Last_Call__c = Date.today() - 8, Stage_Count__c = 6, StageName = 'New',Lead_Status__c='Following' );

Opportunity op10 = new Opportunity( Name='Op10', CloseDate = Date.today(), Last_Call__c = Date.today() - 7, Stage_Count__c = 8, StageName = 'New',Lead_Status__c='Following');

Opportunity op12 = new Opportunity( Name='Op10', CloseDate = Date.today(), Last_Call__c = Date.today() - 7, Stage_Count__c = 8, StageName = 'New',Lead_Status__c='Following',Call_back_time__c=System.now().time(),Call_back_date__c=system.today() -2 );

Opportunity op11 = new Opportunity( Name='Op11', CloseDate = Date.today(), Last_Call__c = Date.today() - 7, In_Queue__c = true, StageName = 'Following >>> Send to Queue',Lead_Status__c='Following' );

system.debug('opportunity insertion begins ');//debug statement displayed

insert new Opportunity[]{

op1,op2, op3, op4, op5, op6, op7, op8, op9, op10, op12,op11

};

system.debug('opportunity insertion ends '); //not displayed

}

private static Opportunity fetchNextOpportunity()

{

system.debug(' insidefetchNextopp');

Id op_id = OpportunityFinderV2.findNextOpportunity();

if( op_id == null ) return null;

system.debug('id '+op_id );

return [SELECT Name FROM Opportunity WHERE Id = :op_id];

}

private static Opportunity closeAndFetch( Opportunity op )

{

op.StageName = 'No - Can Get it Cheaper';

update op;

system.debug('inside fetch nect opp');

return fetchNextOpportunity();

}

@IsTest

public static void testOpportunityFinder()

{

Test.startTest();

Opportunity op = new Opportunity();

op = fetchNextOpportunity();

//System.assertEquals( 'Op1', op.Name );

//op = fetchNextOpportunity();

//System.assertEquals( 'Op1', op.Name );

/*for( Integer x = 2; x < 10; x++ ){

op = closeAndFetch( op );

System.debug('Fetched: ' + op.Name );

// System.assertEquals( 'Op' + x, op.Name );

}*/

Test.stopTest();

}

@IsTest

public static void testAutoSetCallbackDate(){

OpportunityFinderV2 objfin = new OpportunityFinderV2();

objfin.fake();

// Opportunity op1 = new Opportunity( Name='Op1', CloseDate = Date.today(), Stage_Count__c = 0, StageName = 'New' );

Opportunity op1 = new Opportunity( Name='Op1', CloseDate = Date.newInstance(2016, 08, 3), Stage_Count__c = 0, StageName = 'New' ,Lead_Status__c='Following',Call_Back_Date__c = Date.newInstance(2016, 08, 2), Call_back_time__c =Time.newInstance( 3, 3, 3, 0 ) , In_Queue__c = false);

insert op1;

Test.startTest();

OpportunityCallViewExtensionV2 ext = new OpportunityCallViewExtensionV2( new ApexPages.StandardController( op1 ));

ext.LogAndNext();

ext.fake();

System.assertEquals(

OpportunityCallViewExtensionV2.AddWorkDays( Date.today(), 3),

[SELECT Call_back_Date__c FROM Opportunity WHERE Id = :op1.Id].Call_back_date__c

);

Test.stopTest();

}

@IsTest

private static void testManuallySettingCallbackDate(){

Opportunity op1 = new Opportunity( Name='Op1', CloseDate = Date.today(), Stage_Count__c = 0, StageName = 'New' );

insert op1;

Test.startTest();

OpportunityCallViewExtensionV2 ext = new OpportunityCallViewExtensionV2( new ApexPages.StandardController( op1 ));

ext.NextCallbackProxy.Call_back_date__c = Date.newInstance( 1991, 03, 04 );

ext.NextCallbackProxy.Call_back_time__c = time.newInstance(1, 1, 1, 1);

ext.LogAndNext();

System.assertEquals(

Date.newInstance( 1991, 03, 04 ),

[SELECT Call_back_Date__c FROM Opportunity WHERE Id = :op1.Id].Call_back_date__c

);

Test.stopTest();

}

}

I think that is why it is giving null point exception as records not inserted.

Can anyone help how to insert record in bulk.

 

I think that is why it is giving null point exception as records not inserted.

Can anyone help how to insert record in bulk.

  

#Apex  #Test Classes  #Apex Test Classes  #VF  #Exception  #Sales Cloud

 

#Apex  #Test Classes  #Apex Test Classes  #VF  #Exception  #Sales Cloud

 

#Apex  #Test Classes  #Apex Test Classes  #VF  #Exception  #Sales Cloud

9 respuestas
  1. 2 ago 2021, 13:46

    Hi Shivani,  When test classes execute(with seeAllData = false, which is the default behavior), all data records in 

    the org will not be accessible. This includes custom settings. So, when the triggers executes from within 

    your test class execution, it is not able to find the custom setting record for "ParentOpportunity" 

    (since that record is not created as test data within your testsetup method).   Since the trigger code is not validating whether the object returned from the statement below is null 

    before trying to access its field (ParentOppTriggerIsActive.isActive__), the error is being displayed. Hence, 

    the recommendation to insert the custom setting record pertaining to the value "ParentOpportunity" in 

    the "@testsetup" method in your test class.  

    Trigger_Handler__c ParentOppTriggerIsActive = Trigger_Handler__c.getValues('ParentOpportunity');

    if(ParentOppTriggerIsActive.isActive__c && UpdateCheckboxHandler.isFirstTime && ((trigger.isAfter && trigger.isinsert) || (trigger.isafter && trigger.isupdate))){

     Regards,  Sharath

0/9000