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 

need help with test for opportunity contact trigger

Trigger that is inserting the contact ID of an associated contact via Opportunity contact role into custom field on opportunity
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';
        }
    }
}

Base test 
@isTest
public class BaseTest {

    public static Account newAccount(String name) {
        return new Account(
                Name = name,
                BillingStreet = 'Some street',
                BillingCity = 'Some City',
                Email__c = 'steve@example.com',
                Phone = '0123456789',
                Industry = 'Agriculture'
        );
    }

    public static Contact newContact(String firstName, String lastName, String accountId) {
        return new Contact(
                FirstName = firstName,
                LastName = lastName,
                Phone = '0123456789',
                Email = 'steve@example.com',
                AccountId = accountId
        );
    }

    public static Opportunity newOpportunity(String Oppname, String accountId) {
        return new Opportunity(
                AccountId = accountId,
                Name = Oppname,
                StageName = 'Appointment Booked',
                LeadSource = 'Test',
                Lead_Source_Specific__c = 'Other',
                CloseDate = Date.newInstance(2018, 12, 09)
        );
    }

}

Test class
@IsTest
public class UpdateOppConvertContactTriggerTest {

    @IsTest
    static void testOppInsert() {
        Account testAccount = BaseTest.newAccount('name');
        insert testAccount;

        Contact testContact = BaseTest.newContact('first', 'last', testAccount.Id);
        insert testContact;

        Opportunity testOpportunity = BaseTest.newOpportunity('oppname', testAccount.Id);
        insert testOpportunity;

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

        update testOpportunity;

        System.assertEquals(testContact.Id, testRole.ContactId);
        System.assertEquals(testContact.Id, testOpportunity.Opportunity_Contact__c);
    }
}

I am getting 100% coverage without the System.assertEquals at the end. However, it keeps failing with ID in the field being Null. How do i find out why my trigger is not firing? is the update method correct that should fire the trigger? 

much appeciated
v varaprasadv varaprasad
Hi,

Please check once below code : 
@IsTest
public class UpdateOppConvertContactTriggerTest {

    @IsTest
    static void testOppInsert() {
        Account testAccount = BaseTest.newAccount('name');
        insert testAccount;

        Contact testContact = BaseTest.newContact('first', 'last', testAccount.Id);
        insert testContact;

        Opportunity testOpportunity = BaseTest.newOpportunity('oppname', testAccount.Id);
        insert testOpportunity;

        OpportunityContactRole testRole = new OpportunityContactRole();
        testRole.OpportunityId = testOpportunity.Id;
        testRole.ContactId = testContact.Id;
        insert testRole;
		
		OpportunityContactRole oppct = [select id,OpportunityId,ContactId from OpportunityContactRole where id =: testRole.id];

        testOpportunity.Opportunity_Contact__c = oppct.ContactId;
		update testOpportunity;

        System.assertEquals(testContact.Id, testRole.ContactId);
        System.assertEquals(testContact.Id, testOpportunity.Opportunity_Contact__c);
    }
}


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com