+ Start a Discussion
Pinky 10Pinky 10 

getting only 30% code coverage on below class

trigger OppValidateMandatoryFields on Opportunity (before update) {

    List<String> accounts = new List<String>{};
    List<String> asotpa = new List<String>{};
    
    for (Opportunity ao:trigger.new){
        accounts.add(ao.AccountId);
        asotpa.add(ao.ASO_or_TPA_Account__c);
    }

    Map<Id, Account> oppaccountsMap = new Map<Id, Account>([SELECT Referral_Agent_Broker_Contact_Person_new__r.Email, Account_Legal_Name__c, Account_Manager__r.Email, Client_Channel_Segment__c, Client_Channel_Sub_Segment__c, Revenue_Effective_Date__c, Phone, Payment_Terms__c, Utilization_Guarantee__c, Utilization_Guarantee_Details__c, Rate_Lock_Period__c, Rate_Lock_Period_Details__c, Util_Cap_for_Consult__c, Util_Cap_Description__c, Account_Type__c from Account
              WHERE Id IN :accounts]);
    Map<Id, Account> asotpasMap = new Map<Id, Account>([SELECT Name from Account
              WHERE Id IN :asotpa]);  
    Map<Id, OpportunityContactRole> oppcontactMap = new Map<Id, OpportunityContactRole>();
    for(OpportunityContactRole ocr :[SELECT OpportunityId, Contact.Email, Contact.Phone FROM OpportunityContactRole WHERE OpportunityId in :Trigger.New AND IsPrimary = true]){
        oppcontactMap.put(ocr.OpportunityId, ocr);
    }
              
    Set<Id> oppOwners = new Set<Id>();
    for(User u : [select Id from User where Username like 'aroga@teladoc%' or username like 'aford@teladoc%' or username like 'trenz@teladoc%' or username like 'ejoy@teladoc%' or username like 'dgoldberg@teladoc%']){
        oppOwners.add(u.Id);
    }
    
    Id providerType = [Select Id from RecordType where DeveloperName = 'Provider_Platform'][0].Id;
              
    for(Opportunity obj : trigger.new){
        
        Opportunity oldOpp = new Opportunity();
        oldOpp = Trigger.oldMap.get(obj.Id);
        
        if (obj.RecordTypeId != providerType && (oldOpp.StageName != 'Contracting' && oldOpp.StageName != 'Closed Won') && (obj.StageName == 'Contracting' ||  obj.StageName == 'Closed Won') && (/* obj.Client_Retail_Fee__c == null || */ obj.Teladoc_Net_Fee__c == null || obj.Fee_Type__c == null || oppaccountsMap.get(obj.AccountId).Account_Legal_Name__c == null || oppaccountsMap.get(obj.AccountId).Client_Channel_Segment__c == null || oppaccountsMap.get(obj.AccountId).Client_Channel_Sub_Segment__c == null || /* oppaccountsMap.get(obj.AccountId).Revenue_Effective_Date__c == null || */ oppaccountsMap.get(obj.AccountId).Phone == null || /* oppaccountsMap.get(obj.AccountId).Payment_Terms__c == null || */ oppaccountsMap.get(obj.AccountId).Utilization_Guarantee__c == null || oppaccountsMap.get(obj.AccountId).Utilization_Guarantee_Details__c == null || oppaccountsMap.get(obj.AccountId).Rate_Lock_Period__c == null || oppaccountsMap.get(obj.AccountId).Rate_Lock_Period_Details__c == null || oppaccountsMap.get(obj.AccountId).Util_Cap_for_Consult__c == null || oppaccountsMap.get(obj.AccountId).Util_Cap_Description__c == null || (oppcontactMap.get(obj.Id) != null && (oppcontactMap.get(obj.Id).Contact.Email == null || oppcontactMap.get(obj.Id).Contact.Phone == null))) && obj.Description != 'Test Data'){
            obj.addError('You are missing mandatory fields necessary to move an opportunity to Contracting or Closed Won. <a href="https://c.'+System.Url.getSalesforceBaseURL().getHost().substringBefore('.')+'.visual.force.com/apex/missingfields_ci?aid='+obj.AccountId+'&oid='+obj.Id+'" >Click here for list of missing fields</a>', FALSE);
        }else if((oldOpp.StageName != 'Contracting' && oldOpp.StageName != 'Closed Won') && (obj.StageName == 'Contracting' ||  obj.StageName == 'Closed Won')){
                        
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String ASO_Name;
            
            ASO_Name = 'empty';
            if (obj.ASO_or_TPA_Account__c != null){
                ASO_Name = asotpasMap.get(obj.ASO_or_TPA_Account__c).Name;
            }
            String[] ToAddresses;
            //ToAddresses = new String[] { UserInfo.getUserEmail() };
            String[] CcAddresses;
            // CcAddresses = new String[] { 'jvela@cloudberrycreative.com' };
    
            Set<String> CcAddresses2 = new Set<String> {UserInfo.getUserEmail(), obj.Account_Owner_Email__c};
            
            if (oppaccountsMap.get(obj.AccountId).Account_Manager__r.Email != null){
                toAddresses = new String[] { oppaccountsMap.get(obj.AccountId).Account_Manager__r.Email };
            } else if(oppOwners.contains(obj.OwnerId)){
                toAddresses = new String[] { 'ksanderson@teladoc.com','ProvImplementation@teladoc.com' };
                CcAddresses = new String[] { 'aaddis@teladoc.com' };
            } else if(ASO_Name.contains('Aetna') || obj.Name.contains('Aetna')){
                toAddresses = new String[] { 'lferguson@teladoc.com','scrain@teladoc.com','ldelarso@teladoc.com','kwarrick@teladoc.com' };
                CcAddresses = new String[] { 'aaddis@teladoc.com' };
            } else if (obj.Initial_of_Lives__c < 500 && oppaccountsMap.get(obj.AccountId).Account_Type__c != 'Health Plan'){
                toAddresses = new String[] { 'newimplementation@teladoc.com' };
                CcAddresses = new String[] { 'aaddis@teladoc.com' };        
            } else if (oppaccountsMap.get(obj.AccountId).Account_Type__c == 'Reseller'){
                toAddresses = new String[] { 'ResellImplemenation@teladoc.com','kbowie@teladoc.com' };
                CcAddresses = new String[] { 'aaddis@teladoc.com' };
            } else if (obj.Initial_of_Lives__c >= 500 && obj.Initial_of_Lives__c <= 20000 && oppaccountsMap.get(obj.AccountId).Account_Type__c != 'Health Plan'){
                toAddresses = new String[] { 'midimplementation@teladoc.com','dvindedzis@teladoc.com' };
                CcAddresses = new String[] { 'aaddis@teladoc.com' };
            } else if (obj.Initial_of_Lives__c > 20000 || oppaccountsMap.get(obj.AccountId).Account_Type__c == 'Health Plan'){
                toAddresses = new String[] { 'jtardie@teladoc.com','LargeImplementation@teladoc.com' };
                CcAddresses = new String[] { 'aaddis@teladoc.com' };
            } else {
                toAddresses = new String[] { 'aaddis@teladoc.com' };
            }
            
            if(CcAddresses != null && CcAddresses.size() > 0){
                CcAddresses.addAll(CcAddresses2);
            } else {
                CcAddresses = new List<String>(CcAddresses2);
            }
            
            mail.setToAddresses(toAddresses);         
            mail.setCcAddresses(ccAddresses);
            mail.setReplyTo('executive@teladoc.com');
            mail.setSenderDisplayName('Teladoc Mail Service');
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setSaveAsActivity(false);
    
            if (oppaccountsMap.get(obj.AccountId).Account_Manager__r.Email != null){
                mail.setSubject('New client intake form');
                mail.setHtmlBody('<html><body>'+obj.Name+' is now in stage '+obj.StageName+'. <a href="https://'+System.Url.getSalesforceBaseURL().getHost().substringBefore('.')+'.salesforce.com/apex/ClientIntakeForm?aid='+obj.AccountID+'&oid='+obj.Id+'">Click here</a> to view the Client Intake Form.</body></html>');
            } else {
                mail.setSubject('New client intake form, please assign an Account Manager');
                mail.setHtmlBody('<html><body>'+obj.Name+' is now in stage '+obj.StageName+'. <a href="https://'+System.Url.getSalesforceBaseURL().getHost().substringBefore('.')+'.salesforce.com/apex/ClientIntakeForm?aid='+obj.AccountID+'&oid='+obj.Id+'">Click here</a> to view the Client Intake Form. Please assign an Account Manager.</body></html>');
            }
            
            try{
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            } catch (Exception e){
                System.debug(e.getMessage());
                System.debug('There was an error while sending email notification to the recipients: ' + toAddresses + ' CC: ' + ccAddresses);
            }
        }
    }
}