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
Jessica Clark 12Jessica Clark 12 

code coverage - need fresh eyes

Hi everyone,

As I'm sure you have all experienced before, I somehow am not getting the 75% code coverage necessary for deployment. I want to first point out that this controller is already deployed with adequate coverage but for some reason when I wanted to make one small edit in our test environment, it came back with a 6X% code coverage. So I could not deploy it. I have gotten it up to 72% upon deployment, but I have covered all the code (well, 95% of it) in the developer console seemlessly. Someone please give me direction. I have spent way too much time on this.

95% code coverage in dev console:
code coverage in dev console 95%

Errors in first function
errors

Errors in second function
errors

Controller (omitted functions that do not have errors):
public class bAgentListController{ 
  ...
  @AuraEnabled
    //UPDATE OPPORTUNITY
    public static string updateOpportunity(string oppId, string refSource, string agentSource, string refContact, string bCHLBool, string priceRange, string areaInterest, string agentNotes, string description, string mortNotes, string propAddr, string mortStatus, string mortType, string preQualBool, string preQual, string sendCU, string reasonNoCU, string buyOrSell, string updateBoth, string otherOppId, string agentId ){
        try{
            Id RTID_Referred = RecordTypeManager.RecordTypeFor('Opportunity', 'Referred');
            
            boolean sendCuBool = false;
            if (sendCU == 'true'){
                sendCuBool = true;
            }
            
            boolean updateBothBool = false;
            if (updateBoth == 'true'){
                updateBothBool = true;
            }
            
            boolean bCHLPass = false;
            if (bCHLBool == 'true'){
                bCHLPass = true;
            }
            
            double preQualAmount = 0;
            if (preQual != ''){
                preQualAmount = double.valueOf(preQual);
            }
            
            //UPDATE OPPORTUNITY
            Opportunity newOpp = [SELECT Id,Referral_Source__c, Agent_Source__c,Referring_Contact__c,Price_Range__c,Areas_of_Interest_city_state__c,Notes_for_Agent__c,Description,Mortgage_Notes__c,Closing_Address__c,Property_Street__c,Property_City__c,Mortgage_Status__c,Type_of_Mortgage__c,Pre_Qualified__c,Pre_Qualified_Amount__c,Send_To_CU__c,Reason_not_sent_to_CU__c FROM opportunity WHERE Id =:oppId LIMIT 1];
                        
            newOpp.Referral_Source__c = refSource;
            newOpp.Agent_Source__c = agentSource;
            newOpp.Referring_Contact__c = refContact;
            newOpp.BryteCoreHotLead__c = bCHLPass;
            newOpp.RecordTypeId = RTID_Referred;
            newOpp.Phase__c = 'Scrubbed';
            
            //AVOID EMPTY PICKLISTS/STRINGS 
            if(priceRange != ''){newOpp.Price_Range__c = priceRange;}
            if(areaInterest != ''){newOpp.Areas_of_Interest_city_state__c = areaInterest;}
            if(agentNotes != ''){newOpp.Notes_for_Agent__c = agentNotes;}
            if(description != ''){newOpp.Description = description;}
            if(mortNotes != ''){newOpp.Mortgage_Notes__c = mortNotes;}
            if(propAddr != ''){newOpp.Closing_Address__c = propAddr;}
            if(mortStatus != ''){newOpp.Mortgage_Status__c = mortStatus;}
            if(mortType != ''){newOpp.Type_of_Mortgage__c = mortType;}
            if(preQual != ''){newOpp.Pre_Qualified_Amount__c = preQualAmount;}
            newOpp.Send_To_CU__c = sendCuBool;
            if(reasonNoCU != ''){newOpp.Reason_not_sent_to_CU__c = reasonNoCU;}
            
            if(buyOrSell == 'Buy'){newOpp.Pre_Qualified__c = preQualBool;}
            else{newOpp.Pre_Qualified__c = 'No';}
            
            update newOpp;
            
            if (updateBothBool){
                Opportunity otherOpp = [SELECT Id,Referral_Source__c, Agent_Source__c,Referring_Contact__c,Price_Range__c,Areas_of_Interest_city_state__c,Notes_for_Agent__c,Description,Mortgage_Notes__c,Closing_Address__c,Property_Street__c,Property_City__c,Mortgage_Status__c,Type_of_Mortgage__c,Pre_Qualified__c,Pre_Qualified_Amount__c,Send_To_CU__c,Reason_not_sent_to_CU__c FROM opportunity WHERE Id =:otherOppId LIMIT 1];
            	otherOpp.Referral_Source__c = refSource;
                otherOpp.Agent_Source__c = agentSource;
                otherOpp.Referring_Contact__c = refContact;
                otherOpp.BryteCoreHotLead__c = bCHLPass;
                otherOpp.RecordTypeId = RTID_Referred;
                otherOpp.Phase__c = 'Scrubbed';
                
                //AVOID EMPTY PICKLISTS/STRINGS  
                if(priceRange != ''){otherOpp.Price_Range__c = priceRange;}
                if(areaInterest != ''){otherOpp.Areas_of_Interest_city_state__c = areaInterest;}
                if(agentNotes != ''){otherOpp.Notes_for_Agent__c = agentNotes;}
                if(description != ''){otherOpp.Description = description;}
                if(mortNotes != ''){otherOpp.Mortgage_Notes__c = mortNotes;}
                if(propAddr != ''){otherOpp.Closing_Address__c = propAddr;}
                if(mortStatus != ''){otherOpp.Mortgage_Status__c = mortStatus;}
                if(mortType != ''){otherOpp.Type_of_Mortgage__c = mortType;}
                if(preQual != ''){otherOpp.Pre_Qualified_Amount__c = preQualAmount;}
                if(reasonNoCU != ''){otherOpp.Reason_not_sent_to_CU__c = reasonNoCU;}
                //else{newOpp.Reason_not_sent_to_CU__c = 'CU Realty was unable to connect with member';}
                
                if(buyOrSell == 'Sell'){otherOpp.Pre_Qualified__c = preQualBool;}
                else{otherOpp.Pre_Qualified__c = 'No';}
                otherOpp.Send_To_CU__c = sendCuBool;
                
                update otherOpp;
            }
            
            return 'Success';
        }
        catch(Exception e){
            return e.getMessage();
        }
        
    }
    
  @AuraEnabled
    //INSERT RELATED CONTACT
    public static string insertRelatedContact(string oppId, string agentId, string updateBoth, string otherOppId ){
        try{
            //INSERT RELATED CONTACT
            Related_Contact__c rc = new Related_Contact__c();
            rc.OpportunityId__c = oppId;
            rc.ContactId__c = agentId;
            rc.Role__c = 'Assigned Agent';
            
            insert rc;
            
            if (updateBoth == 'true'){
                //INSERT RELATED CONTACT
                Related_Contact__c rcOther = new Related_Contact__c();
                rcOther.OpportunityId__c = otherOppId;
                rcOther.ContactId__c = agentId;
                rcOther.Role__c = 'Assigned Agent';
                
                insert rcOther;
            }
            
            return 'Success';
        }
        catch(Exception e){
            return e.getMessage();
        }

    }
  
}

Test controller:
@isTest(seeAlldata = true)
public class bAgentListControllerTest {
    static testMethod void agentTest1(){

        ... (omitting set up)

        string refSource = 'Member Services';
        string agentSource = 'Member Referral';
        string refContact = '0038000002IcVYpAAN';//jessica clark
        string bCHLBool = 'true';
        string priceRange = 'Test';
        string areaInterest = 'Test';
        string agentNotes = 'Test';
        string description = 'Test';
        string mortNotes = 'Need new Home';
        string propAddr = 'Test123 City, ST 00000';
        string mortStatus = 'In Process';
        string mortType = 'Conventional';
        string preQualBool = 'Yes';
        string preQual = '100000';
        string sendCU ='true';
        string reasonNoCU = ''; 
        string buyOrSell= 'Sell';     
        string buyOrSell2= 'Buy'; 
        string updateBoth = 'true';
        string updateBoth2 = 'false';

        ... (omitting function calls without error)

        bAgentListController.updateOpportunity(o.Id, refSource, agentSource, refContact, bCHLBool, priceRange, areaInterest, agentNotes, description, mortNotes, propAddr, mortStatus, mortType, preQualBool, preQual, sendCU, reasonNoCU, buyOrSell, 'true', o3.Id, c2.Id);

    	bAgentListController.insertRelatedContact(o.Id, c2.Id, updateBoth, o3.Id);

Maybe this isn't enough information. If not, let me know and I will provide. Thanks!

Jessica
Xavier Muñoz 28Xavier Muñoz 28
Hi Jessica,

The issue is because in the test class you are harcoding an Id and that record does not exist in prod
string refContact = '0038000002IcVYpAAN';//jessica clark
Just create that contact by yourself in the test and should work. Also think about removing the seeAllData true in the test, it is not a good practice unless it is strictly necessary :)
Jessica Clark 12Jessica Clark 12
I appreciate this answer! I will take a stab at it. Thank you very much.