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
salesforcerrrsalesforcerrr 

Trigger and Test Class with NullPointer Exception - Opportunity Contact Role

Hi, I have writted a trigger that grabs the Contact ID from a contact related to the opportunity via OpportunityContactRole and inserts this ID in a custom lookup field Opportunity_Contact__c. 
 
trigger UpdateOppConvertContactTrigger on Opportunity (before update) {

    List<OpportunityContactRole> ContactRoles = [SELECT OpportunityId, ContactId
    FROM OpportunityContactRole];

    Map<String, OpportunityContactRole> OppIdToContactMap = new Map<String, OpportunityContactRole>();
    for (OpportunityContactRole ConRole : ContactRoles) {
        OppIdToContactMap.put(ConRole.OpportunityId, ConRole);
    }

    for (Opportunity opp : Trigger.new) {
        if (opp.Opportunity_Contact__c == NULL) {
            OpportunityContactRole newContact = OppIdToContactMap.get(opp.Id);
            opp.Opportunity_Contact__c = newContact.ContactId;
            opp.Type = 'New Customer';
        }
    }
}
Now I am finishing up the work on the test class which just inserts the stuff and then does an update to the Opportunity: 
 
@IsTest
public class UpdateOppConvertContactTriggerTest extends BaseTest {

    @IsTest
    static void testOppInsert() {
            Account testAccount = new Account();
            testAccount.Name = 'name';
        testAccount.BillingStreet = 'Some street';
        testAccount.BillingCity = 'Some City';
        testAccount.Email__c = 'steve@example.com';
        testAccount.Phone = '0123456789';
        testAccount.Industry = 'Agriculture';
        insert testAccount;

        Contact testContact = new Contact();
        testContact.FirstName = 'firstName';
        testContact.LastName = 'lastName';
        testContact.Phone = '0123456789';
        testContact.Email = 'steve@example.com';
        testContact.AccountId = testAccount.Id;
        insert testContact;

            Opportunity testOpportunity = new Opportunity();
            testOpportunity.AccountId = testAccount.Id;
            testOpportunity.Name = 'Test';
            testOpportunity.StageName = 'Appointment Booked';
            testOpportunity.LeadSource = 'Other';
            testOpportunity.Lead_Source_Specific__c = 'Other';
             testOpportunity.CloseDate = Date.newInstance(2018, 12, 09);
            insert testOpportunity;

        OpportunityContactRole testRole = new OpportunityContactRole();
        testRole.OpportunityId = testOpportunity.Id;
        testRole.ContactId = testContact.Id;

        Test.startTest();
        Database.update(testOpportunity);
        Test.stopTest();

    }
}
I continue to get: 
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateOppConvertContactTrigger: execution of AfterUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object

No idea why / if error is in test or trigger? 

Much appreciated. 
Best Answer chosen by salesforcerrr
Sampath SuranjiSampath Suranji
Hi,
You have missed the insert part of OpportunityContactRole in the test class.
OpportunityContactRole testRole = new OpportunityContactRole();
  testRole.OpportunityId = testOpportunity.Id;
  testRole.ContactId = testContact.Id;
  insert testRole;

Best Regards
Sampath

 

All Answers

GhanshyamChoudhariGhanshyamChoudhari
@IsTest
public class UpdateOppConvertContactTriggerTest extends BaseTest {

    @IsTest
    static void testOppInsert() {
            Account testAccount = new Account();
            testAccount.Name = 'name';
        testAccount.BillingStreet = 'Some street';
        testAccount.BillingCity = 'Some City';
        testAccount.Email__c = 'steve@example.com';
        testAccount.Phone = '0123456789';
        testAccount.Industry = 'Agriculture';
        insert testAccount;

        Contact testContact = new Contact();
        testContact.FirstName = 'firstName';
        testContact.LastName = 'lastName';
        testContact.Phone = '0123456789';
        testContact.Email = 'steve@example.com';
        testContact.AccountId = testAccount.Id;
        insert testContact;

            Opportunity testOpportunity = new Opportunity();
            testOpportunity.AccountId = testAccount.Id;
            testOpportunity.Name = 'Test';
            testOpportunity.StageName = 'Appointment Booked';
            testOpportunity.LeadSource = 'Other';
            testOpportunity.Lead_Source_Specific__c = 'Other';
             testOpportunity.CloseDate = Date.newInstance(2018, 12, 09);
            insert testOpportunity;

        OpportunityContactRole testRole = new OpportunityContactRole();
        testRole.OpportunityId = testOpportunity.Id;
        testRole.ContactId = testContact.Id;
		 insert testRole;
		 OpportunityContactRole oppc =[select id,IsPrimary from OpportunityContactRole where id= :testRole.id]};
		oppc.IsPrimary =true;
        Test.startTest();
        Database.update(oppc);
        Test.stopTest();

    }
}

 
Sampath SuranjiSampath Suranji
Hi,
You have missed the insert part of OpportunityContactRole in the test class.
OpportunityContactRole testRole = new OpportunityContactRole();
  testRole.OpportunityId = testOpportunity.Id;
  testRole.ContactId = testContact.Id;
  insert testRole;

Best Regards
Sampath

 
This was selected as the best answer