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 

Test Class code coverage 71% -- not running if statement

I am creating a test class, and everything is running cleanly except one if statement. Although I am passing the correct string as an argument for the method, it is not recognizing it in the comparison statement. Below image is the code coverage, skipping if updateBoth == 'true'.Code Coverage skipping if statement

Test call: (note: I define o3, o, and c2 elsewhere; those are not an issue)
string refSource = 'Member Services';
        string agentSource = 'Member Referral';
            string refContact = '0030R00000SCIl3QAH';//jessica testclark
            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= 'Buy';     
        	string buyOrSell2= 'Sell'; 
            string updateBoth = 'true';
        	string updateBoth2 = 'false';
 
bAgentListController.updateOpportunity(o3.Id, refSource, agentSource, refContact, bCHLBool, priceRange, areaInterest, agentNotes, description, mortNotes, propAddr, mortStatus, mortType, preQualBool, preQual, sendCU, reasonNoCU, buyOrSell, 'true', o.Id, c2.Id);



Controller method:
@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 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 (updateBoth == 'true'){
                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'){newOpp.Pre_Qualified__c = preQualBool;}
                else{newOpp.Pre_Qualified__c = 'No';}
                otherOpp.Send_To_CU__c = sendCuBool;
                
                update otherOpp;
            }
            
            return 'Success';
        }
        catch(Exception e){
            return e.getMessage();
        }
    }

 
Best Answer chosen by Jessica Clark 12
Raj VakatiRaj Vakati
If i am correct , your code is failing with some exception on update .. Can you remove the try and catch and run the test class so you will be able to find the exception ... 

Based on it set the data to test class  

 

All Answers

Jessica Clark 12Jessica Clark 12
Well, when I was trying to write this post, I thought it would be smart to bold and emphasize the if statement and the string passing through, but it was translated to <b></b> and <i></i> in the post. I do not have those in the function; I promise.
Raj VakatiRaj Vakati
If i am correct , your code is failing with some exception on update .. Can you remove the try and catch and run the test class so you will be able to find the exception ... 

Based on it set the data to test class  

 
This was selected as the best answer
Jessica Clark 12Jessica Clark 12
Hi,
Thank you! That worked. I found a couple errors in the logs by doing that. I thought it was the true/false variable causing the error. I appreciate it!

Jessica