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
edward scott 10edward scott 10 

can i make changes to code when my coverage is below 75 percent

Hi,

I am trying to figure out two things. One if I have a class that is completely commented out to inactivate the class does that class still count against my code coverage. The second question is how do I make changes to code when my coverage is below 75 percent and I can't deploy any code. And I guess a third question can test code be deployed when the coverage is below 75 percent?

Thanks for your help,
Ed
v varaprasadv varaprasad
Hi Edward,

1.The commented code will not consider in the test class.No need to write for it.
2.If your class is working fine .try to improve coverage in test class only don't change the class.
3.If code coverage is below 75% we can't deploy it.

Hope this helps you!If you need help on test class please post your test class code.
If the above information is informative and helps you please mark it as the best answer.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
edward scott 10edward scott 10
Hi V. This is the trigger that is most likely causing our code coverage problem. Thanks for any help you can give me.
 
trigger OppCheckPrimaryContactChange on Opportunity (after update) {
    Set<Id> oppIds = new Set<Id>();
    for(Opportunity oppt : Trigger.new) {
        oppIds.add(oppt.Id);
    }
    
    List<Opportunity> oppList = [ SELECT Id, Primary_Contact__c, Technical_Contact__c FROM Opportunity WHERE Id IN :oppIds ];
    List<OpportunityContactRole> crList = [ SELECT OpportunityId, ContactId, Role, IsPrimary FROM OpportunityContactRole WHERE OpportunityId IN :oppIds AND IsDeleted = FALSE ];
    List<OpportunityContactRole> newContactRoleList = new List<OpportunityContactRole>();

    Boolean PrimaryContactLocated = FALSE;
    Boolean TechnicalContactLocated = FALSE;
    
    try {
        for(Opportunity opp : oppList) {
            
            PrimaryContactLocated = FALSE;
            TechnicalContactLocated = FALSE;

            if(opp.Primary_Contact__c != null || opp.Technical_Contact__c != null) {
                for(OpportunityContactRole cr : crList) {
                    if(opp.Primary_Contact__c != null && opp.Id == cr.OpportunityId && opp.Primary_Contact__c == cr.ContactId) {
                        PrimaryContactLocated = TRUE;
                        if(!cr.IsPrimary) {
                            cr.IsPrimary = TRUE;
                            update cr;
                        }
                    } else if(opp.Technical_Contact__c != null && opp.Id == cr.OpportunityId && opp.Technical_Contact__c == cr.ContactId) {
                        TechnicalContactLocated = TRUE;
                    }
                }
                if(!PrimaryContactLocated) {
                    OpportunityContactRole newCr = new OpportunityContactRole();
                    newCr.ContactId = opp.Primary_Contact__c;
                    newCr.OpportunityId = opp.Id;
                    newCr.Role = 'Decision Maker';
                    newCr.IsPrimary = TRUE;
                    newContactRoleList.add(newCr);
                }
                if(!TechnicalContactLocated && opp.Primary_Contact__c != opp.Technical_Contact__c) {
                    OpportunityContactRole newCr = new OpportunityContactRole();
                    newCr.ContactId = opp.Technical_Contact__c;
                    newCr.OpportunityId = opp.Id;
                    newCr.Role = 'Technical Buyer';
                    newContactRoleList.add(newCr);
                }
            }
        }

        insert newContactRoleList;
        
    } catch(DmlException e) {
        System.Debug('The following DmlException has occurred: ' + e.getMessage());
    } catch(Exception e) {
        System.Debug('The following General Exception has occurred: ' + e.getMessage());
    }    
}

It is a trigger to check to see if the current primary contact is attached to an opportunity. I was thinking I could replace this with a trigger but its not allowing me to even deactivate it because the code coverage is at 69 percent. If you could help me write a test for this that I can deploy it would help alot. Thanks.
v varaprasadv varaprasad
Hi Edward,

Please change your code like below : 
 
===========Trigger============
trigger OppCheckPrimaryContactChange on Opportunity (after update) {
     OppCheckPrimaryContactChange.updateOTP(Trigger.new);
        
    }

============Class=========

public class OppCheckPrimaryContactChange {
    
    public static void updateOTP(list<opportunity> opps){
        Set<Id> oppIds = new Set<Id>();
        for(Opportunity oppt : opps) {
            oppIds.add(oppt.Id);
        }
        
        List<Opportunity> oppList = [ SELECT Id, Primary_Contact__c, Technical_Contact__c FROM Opportunity WHERE Id IN :oppIds ];
        List<OpportunityContactRole> crList = [ SELECT OpportunityId, ContactId, Role, IsPrimary FROM OpportunityContactRole WHERE OpportunityId IN :oppIds AND IsDeleted = FALSE ];
        List<OpportunityContactRole> newContactRoleList = new List<OpportunityContactRole>();
        
        Boolean PrimaryContactLocated = FALSE;
        Boolean TechnicalContactLocated = FALSE;
        
        try {
            for(Opportunity opp : oppList) {
                
                PrimaryContactLocated = FALSE;
                TechnicalContactLocated = FALSE;
                
                if(opp.Primary_Contact__c != null || opp.Technical_Contact__c != null) {
                    for(OpportunityContactRole cr : crList) {
                        if(opp.Primary_Contact__c != null && opp.Id == cr.OpportunityId && opp.Primary_Contact__c == cr.ContactId) {
                            PrimaryContactLocated = TRUE;
                            if(!cr.IsPrimary) {
                                cr.IsPrimary = TRUE;
                                update cr;
                            }
                        } else if(opp.Technical_Contact__c != null && opp.Id == cr.OpportunityId && opp.Technical_Contact__c == cr.ContactId) {
                            TechnicalContactLocated = TRUE;
                        }
                    }
                    if(!PrimaryContactLocated) {
                        OpportunityContactRole newCr = new OpportunityContactRole();
                        newCr.ContactId = opp.Primary_Contact__c;
                        newCr.OpportunityId = opp.Id;
                        newCr.Role = 'Decision Maker';
                        newCr.IsPrimary = TRUE;
                        newContactRoleList.add(newCr);
                    }
                    if(!TechnicalContactLocated && opp.Primary_Contact__c != opp.Technical_Contact__c) {
                        OpportunityContactRole newCr = new OpportunityContactRole();
                        newCr.ContactId = opp.Technical_Contact__c;
                        newCr.OpportunityId = opp.Id;
                        newCr.Role = 'Technical Buyer';
                        newContactRoleList.add(newCr);
                    }
                }
            }
            
            insert newContactRoleList;
            
        } catch(DmlException e) {
            System.Debug('The following DmlException has occurred: ' + e.getMessage());
        } catch(Exception e) {
            System.Debug('The following General Exception has occurred: ' + e.getMessage());
        }  
        
    }
    
}

========Test Class=======
@isTest
private class OppCheckPrimaryContactChange_Test{
    @testSetup
    static void setupTestData(){
        test.startTest();
        Contact contact_Obj = new Contact(LastName = 'LastName227');
        Insert contact_Obj; 
        Opportunity opportunity_Obj = new Opportunity(IsPrivate = false, Name = 'Name450', StageName = 'Prospecting', CloseDate = Date.today(), Primary_Contact__c = contact_Obj.id);
        Insert opportunity_Obj; 
        
        Opportunity opportunity_Obj1 = new Opportunity(IsPrivate = false, Name = 'Name450', StageName = 'Prospecting', CloseDate = Date.today(), Technical_Contact__c = contact_Obj.id);
        Insert opportunity_Obj1;
            
        OpportunityContactRole opportunitycontactrole_Obj = new OpportunityContactRole(OpportunityId = opportunity_Obj.id, ContactId = contact_Obj.id, IsPrimary = false);
        Insert opportunitycontactrole_Obj; 
        test.stopTest();
    }
    static testMethod void test_updateOTP_UseCase1(){
        List<Contact> contact_Obj  =  [SELECT Id,LastName from Contact];
        System.assertEquals(true,contact_Obj.size()>0);
        List<Opportunity> opportunity_Obj  =  [SELECT Id,Name,StageName,Primary_Contact__c,Technical_Contact__c from Opportunity];
        System.assertEquals(true,opportunity_Obj.size()>0);
        List<OpportunityContactRole> opportunitycontactrole_Obj  =  [SELECT Id,OpportunityId,ContactId,IsPrimary,IsDeleted from OpportunityContactRole];
        System.assertEquals(true,opportunitycontactrole_Obj.size()>0);
        OppCheckPrimaryContactChange obj01 = new OppCheckPrimaryContactChange();
       OppCheckPrimaryContactChange.updateOTP(opportunity_Obj);
    }
}
===========


Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
edward scott 10edward scott 10
This helps alot. For some reason I am getting an EOF error on line 9. At the public static void. thanks again for your help.
v varaprasadv varaprasad
Hi Edward,

If the above information is informative and helps you please mark it as the best answer.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com