You need to sign in to do that
Don't have an account?
![salesforcerrr salesforcerrr](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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.
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.
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.
You have missed the insert part of OpportunityContactRole in the test class.
Best Regards
Sampath
All Answers
You have missed the insert part of OpportunityContactRole in the test class.
Best Regards
Sampath