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
wcraigwcraig 

Map not covered even with test Class that asserts that it is functioning properly.

I have a simple trigger on opportunity that populates a primary contact on the opportunity from opportunity contact roles.

The test class is getting 19/20 coverage - with the last missing line being a Map.put used to populate a map used to save opportunity id, contact id. The test class tests that this is working properly - I'm not sure what I may be missing to get coverage.

 

The Trigger:

// Built by Will Craig 4.25.2019
//
// Trigger for initiating all classes for Opportunity.

trigger Opportunity on Opportunity (before insert, before update, after insert, after update) {

    // Routing for before triggers.
    if(Trigger.isBefore){
        
        if(Trigger.isUpdate){

            opportunityPrimaryContact.PrimaryContactProcessor(Trigger.new, Trigger.oldMap);

        }else if(Trigger.isInsert){

            opportunityPrimaryContact.PrimaryContactProcessor(Trigger.new);

        }

    } else if(Trigger.isAfter){

        // After trigger responses

    }

}

The Class:

public class opportunityPrimaryContact {
    
    // Method for handling inserts
    public static void PrimaryContactProcessor(List<Opportunity> Opportunities) {

        // Create a map for use when updating Primary_Contact__c on Opportunties
        Map<Id,Id> pocrMap = new Map<Id,Id>();
        
        // Create a set of OpportunityIds for use in the OCR query.
        Set<Id> oppidSet = new Set<Id>();
        for(Opportunity opp : Opportunities){
            oppidSet.add(opp.id);
        }

        // Retrieve primary OCRs and populate a map for retrieving them by OpportunityId
        List<OpportunityContactRole> ocrs = [SELECT ContactId,OpportunityId FROM OpportunityContactRole WHERE OpportunityId IN: oppidSet AND IsPrimary = true];
        for(OpportunityContactRole ocr : ocrs){
            pocrMap.put(ocr.OpportunityId,ocr.ContactId);
        }

        // Iterate through opportunities and set Primary_Contact__c, leveraging pocrMap.
        for(Opportunity opp : Opportunities){
            opp.Primary_Contact__c = pocrMap.get(opp.Id);
        }

        // Updates not required in before triggers.

    }

    // Method for handling updates (via overload)
    public static void PrimaryContactProcessor(List<Opportunity> Opportunities, Map<ID,sObject> TriggerOldMap) {

        // Create a map for use when updating Primary_Contact__c on Opportunties
        Map<Id,Id> pocrMap = new Map<Id,Id>();
        
        // Create a set of OpportunityIds for use in the OCR query.
        Set<Id> oppidSet = new Set<Id>();
        for(Opportunity opp : Opportunities){
            oppidSet.add(opp.id);
        }

        // Retrieve primary OCRs and populate a map for retrieving them by OpportunityId
        List<OpportunityContactRole> ocrs = [SELECT ContactId,OpportunityId FROM OpportunityContactRole WHERE OpportunityId IN: oppidSet AND IsPrimary = true];
        for(OpportunityContactRole ocr : ocrs){
   // line below is what is not covered.         pocrMap.put(ocr.OpportunityId,ocr.ContactId);
        }


        // Iterate through opportunities and set Primary_Contact__c, leveraging pocrMap.
        for(Opportunity opp : Opportunities){
            opp.Primary_Contact__c = pocrMap.get(opp.Id);
        }

        // Updates not required in before triggers.

    }

}
 

Test Class:

@IsTest
public class opportunityPrimaryContact_TEST {
    
    @IsTest
    private static void create_TEST() {

        Test.StartTest();

        User u = new User();
        u.ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id;
        u.LastName = 'last';
        u.Email = 'test@test.com ';
        u.Username = 'test@test.com.' + System.currentTimeMillis();
        u.CompanyName = 'TEST';
        u.Title = 'title';
        u.Alias = 'alias';
        u.TimeZoneSidKey = 'America/Los_Angeles';
        u.EmailEncodingKey = 'UTF-8';
        u.LanguageLocaleKey = 'en_US';
        u.LocaleSidKey = 'en_US';

        Insert u;

        Account acc = new Account();
        acc.Name = 'Test Account';
        acc.ownerid = u.id;

        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Target';
        opp.CloseDate = Date.Today();
        opp.ownerid = u.id;

        Insert opp;

        Contact c1 = new Contact();
        c1.Firstname = 'Test1First';
        c1.Lastname = 'Test1Last';
        c1.AccountId = acc.id;

        Insert c1;

        OpportunityContactRole ocr1 = new OpportunityContactRole();
        ocr1.ContactId = c1.id;
        ocr1.OpportunityId = opp.id;
        ocr1.IsPrimary = true;
        ocr1.Role = 'Decision Maker';

        Insert ocr1;

        Contact c2 = new Contact();
        c2.Firstname = 'Test2First';
        c2.Lastname = 'Test2Last';
        c2.AccountId = acc.id;

        Insert c2;

        OpportunityContactRole ocr2 = new OpportunityContactRole();
        ocr2.ContactId = c2.id;
        ocr2.OpportunityId = opp.id;
        ocr2.IsPrimary = false;
        ocr2.Role = 'Decision Maker';
        
        Insert ocr2;

        Update opp;
        
        Test.StopTest();

        OpportunityContactRole ocr = [SELECT Id,
                                        ContactId,Contact.Name,OpportunityId 
                                        FROM OpportunityContactRole 
                                        WHERE OpportunityId =: opp.id 
                                        AND IsPrimary = true
                                        ];

        

        Opportunity oppupdated = [SELECT Id,Primary_Contact__c from Opportunity WHERE ID =: opp.id];

        System.debug(ocr);
        System.debug(opp.Primary_Contact__c);
        System.assertEquals(ocr.ContactId,oppupdated.Primary_Contact__c);
        
    }

}
Best Answer chosen by wcraig
Raj VakatiRaj Vakati
This is what will say from this trigger .. 
  1. We dnt need before insert or after insert trigger  for this case because logically you can't insert the opportunity line items immediately after insert  from UI .. process may be like below only once update the opportunity them get the OLI and update the values 
  2. Here is the test class with 100 % 
@IsTest
public class opportunityPrimaryContact_TEST {
    
    @IsTest
    private static void create_TEST() {
        
        Test.StartTest();
        
        User u = new User();
        u.ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id;
        u.LastName = 'last';
        u.Email = 'test@test.com ';
        u.Username = 'test@test.com.' + System.currentTimeMillis();
        u.CompanyName = 'TEST';
        u.Title = 'title';
        u.Alias = 'alias';
        u.TimeZoneSidKey = 'America/Los_Angeles';
        u.EmailEncodingKey = 'UTF-8';
        u.LanguageLocaleKey = 'en_US';
        u.LocaleSidKey = 'en_US';
        
        Insert u;
        
        Account acc = new Account();
        acc.Name = 'Test Account';
        acc.ownerid = u.id;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Target';
        opp.CloseDate = Date.Today();
        opp.ownerid = u.id;
        
        Insert opp;
        
        Contact c1 = new Contact();
        c1.Firstname = 'Test1First';
        c1.Lastname = 'Test1Last';
        c1.AccountId = acc.id;
        
        Insert c1;
        
        OpportunityContactRole ocr1 = new OpportunityContactRole();
        ocr1.ContactId = c1.id;
        ocr1.OpportunityId = opp.id;
        ocr1.IsPrimary = true;
        ocr1.Role = 'Decision Maker';
        
        Insert ocr1;
        
        Contact c2 = new Contact();
        c2.Firstname = 'Test2First';
        c2.Lastname = 'Test2Last';
        c2.AccountId = acc.id;
        
        Insert c2;
        
        OpportunityContactRole ocr2 = new OpportunityContactRole();
        ocr2.ContactId = c2.id;
        ocr2.OpportunityId = opp.id;
        ocr2.IsPrimary = false;
        ocr2.Role = 'Decision Maker';
        
        Insert ocr2;
      
        
        Update opp;
        
        Test.StopTest();
        
        OpportunityContactRole ocr = [SELECT Id,
                                      ContactId,Contact.Name,OpportunityId 
                                      FROM OpportunityContactRole 
                                      WHERE OpportunityId =: opp.id 
                                      AND IsPrimary = true
                                     ];
        
        
        
        List<Opportunity> oppupdated = [SELECT Id,Primary_Contact__c from Opportunity ];
        opportunityPrimaryContact.PrimaryContactProcessor(oppupdated);
        
    }
    
}

​​​​​​​

All Answers

wcraigwcraig
public class opportunityPrimaryContact {
    
    // Method for handling inserts
    public static void PrimaryContactProcessor(List<Opportunity> Opportunities) {

        // Create a map for use when updating Primary_Contact__c on Opportunties
        Map<Id,Id> pocrMap = new Map<Id,Id>();
        
        // Create a set of OpportunityIds for use in the OCR query.
        Set<Id> oppidSet = new Set<Id>();
        for(Opportunity opp : Opportunities){
            oppidSet.add(opp.id);
        }

        // Retrieve primary OCRs and populate a map for retrieving them by OpportunityId
        List<OpportunityContactRole> ocrs = [SELECT ContactId,OpportunityId FROM OpportunityContactRole WHERE OpportunityId IN: oppidSet AND IsPrimary = true];
        for(OpportunityContactRole ocr : ocrs){
            pocrMap.put(ocr.OpportunityId,ocr.ContactId);
        }

        // Iterate through opportunities and set Primary_Contact__c, leveraging pocrMap.
        for(Opportunity opp : Opportunities){
            opp.Primary_Contact__c = pocrMap.get(opp.Id);
        }

        // Updates not required in before triggers.

    }

    // Method for handling updates (via overload)
    public static void PrimaryContactProcessor(List<Opportunity> Opportunities, Map<ID,sObject> TriggerOldMap) {

        // Create a map for use when updating Primary_Contact__c on Opportunties
        Map<Id,Id> pocrMap = new Map<Id,Id>();
        
        // Create a set of OpportunityIds for use in the OCR query.
        Set<Id> oppidSet = new Set<Id>();
        for(Opportunity opp : Opportunities){
            oppidSet.add(opp.id);
        }

        // Retrieve primary OCRs and populate a map for retrieving them by OpportunityId
        List<OpportunityContactRole> ocrs = [SELECT ContactId,OpportunityId
                                            FROM OpportunityContactRole 
                                            WHERE OpportunityId IN: oppidSet 
                                            AND IsPrimary = true];
        for(OpportunityContactRole ocr : ocrs){
        // *****line below is what is not covered. *******
         pocrMap.put(ocr.OpportunityId,ocr.ContactId);
        }


        // Iterate through opportunities and set Primary_Contact__c, leveraging pocrMap.
        for(Opportunity opp : Opportunities){
            opp.Primary_Contact__c = pocrMap.get(opp.Id);
        }

        // Updates not required in before triggers.

    }

}
Sorry - fixed some formatting issues.
 
Raj VakatiRaj Vakati
This is what will say from this trigger .. 
  1. We dnt need before insert or after insert trigger  for this case because logically you can't insert the opportunity line items immediately after insert  from UI .. process may be like below only once update the opportunity them get the OLI and update the values 
  2. Here is the test class with 100 % 
@IsTest
public class opportunityPrimaryContact_TEST {
    
    @IsTest
    private static void create_TEST() {
        
        Test.StartTest();
        
        User u = new User();
        u.ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id;
        u.LastName = 'last';
        u.Email = 'test@test.com ';
        u.Username = 'test@test.com.' + System.currentTimeMillis();
        u.CompanyName = 'TEST';
        u.Title = 'title';
        u.Alias = 'alias';
        u.TimeZoneSidKey = 'America/Los_Angeles';
        u.EmailEncodingKey = 'UTF-8';
        u.LanguageLocaleKey = 'en_US';
        u.LocaleSidKey = 'en_US';
        
        Insert u;
        
        Account acc = new Account();
        acc.Name = 'Test Account';
        acc.ownerid = u.id;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opportunity';
        opp.StageName = 'Target';
        opp.CloseDate = Date.Today();
        opp.ownerid = u.id;
        
        Insert opp;
        
        Contact c1 = new Contact();
        c1.Firstname = 'Test1First';
        c1.Lastname = 'Test1Last';
        c1.AccountId = acc.id;
        
        Insert c1;
        
        OpportunityContactRole ocr1 = new OpportunityContactRole();
        ocr1.ContactId = c1.id;
        ocr1.OpportunityId = opp.id;
        ocr1.IsPrimary = true;
        ocr1.Role = 'Decision Maker';
        
        Insert ocr1;
        
        Contact c2 = new Contact();
        c2.Firstname = 'Test2First';
        c2.Lastname = 'Test2Last';
        c2.AccountId = acc.id;
        
        Insert c2;
        
        OpportunityContactRole ocr2 = new OpportunityContactRole();
        ocr2.ContactId = c2.id;
        ocr2.OpportunityId = opp.id;
        ocr2.IsPrimary = false;
        ocr2.Role = 'Decision Maker';
        
        Insert ocr2;
      
        
        Update opp;
        
        Test.StopTest();
        
        OpportunityContactRole ocr = [SELECT Id,
                                      ContactId,Contact.Name,OpportunityId 
                                      FROM OpportunityContactRole 
                                      WHERE OpportunityId =: opp.id 
                                      AND IsPrimary = true
                                     ];
        
        
        
        List<Opportunity> oppupdated = [SELECT Id,Primary_Contact__c from Opportunity ];
        opportunityPrimaryContact.PrimaryContactProcessor(oppupdated);
        
    }
    
}

​​​​​​​
This was selected as the best answer
Raj VakatiRaj Vakati
Is its working ?
wcraigwcraig
TY! Sorry about delayed response