• kousik maji 7
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hello,

There is an old trigger in our org that needs to be deactivated. However, the trigger's test class only has 65% code coverage. I did not write this code and do not have a background in development. Could anyone offer clear edits to make so that I can successfully deploy the deactivated trigger to production?

Trigger:
trigger AccountTrigger on Account (before delete, before insert, before update,after insert) {
    AccountTriggerHandler.execute(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap, 
                          Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete, Trigger.isBefore, Trigger.isAfter);
    AccountTriggerHandler.executeLeadLink(Trigger.new,Trigger.old,Trigger.isUpdate,Trigger.isBefore,Trigger.isInsert,Trigger.isAfter);
}
The Class referenced in the trigger:
public with sharing class AccountTriggerHandler {
	
	static RecordType applicantRecordType;
	static Profile adminProfile;
	static {
		applicantRecordType = [Select Id From RecordType Where Name = 'Applicants' and sobjecttype = 'Account']; 
		adminProfile = [Select Id, Name From Profile Where Name = 'System Administrator'];
	}
	
	public static void execute(List<Account> newList, Map<Id, Account> newMap,
                               List<Account> oldList, Map<Id, Account> oldMap,
                               Boolean isInsert, Boolean isUpdate, Boolean isDelete, Boolean isBefore, Boolean isAfter) {    
    	List<Account> accList = new List<Account>();
    	if (isDelete) {
    		for (Account acc : oldList)
    			accList.add(acc);
    	}
    	else {
    		for (Account acc : newList)
    			accList.add(acc);
    	}    	
    	for (Account acc : accList)	{
    		if (isBefore) {
	    		if (acc.RecordTypeId == applicantRecordType.Id && 
	    			System.Userinfo.getProfileId() != adminProfile.Id) {	    		
	    			acc.addError('Only System Administrators may create, edit, or delete Applicants');		
	    		}
    		}
    	}		                           	
    }
    
	public static Map<String,Lead> GetLeadsWithAccountEmailAddress(List<Account> accounts){
        Map<String,Lead> mappedLeads=new Map<String,Lead>();
        Set<String> accountEmails=new Set<String>();
        for(Account a:accounts){
			accountEmails.add(a.PersonEmail.toLowerCase());
        }
        List<Lead> leads = [Select l.IsConverted, l.Id, l.Email,l.Account__c,l.Application_Status__c
                            From Lead l
                            where l.IsConverted = false and l.Email in:accountEmails];   		
        for(Lead lead:leads){
           mappedLeads.put(lead.Email.toLowerCase(),lead);
        }
        return mappedLeads;
    }

	public static Map<String,Lead> GetLeadsWithAccountExternalReference(List<Account> accounts){
        Map<String,Lead> mappedLeads=new Map<String,Lead>();
        Set<String> accountExternalRef=new Set<String>();
        for(Account a:accounts){
            if (!String.isBlank(a.ExternalReference__c)){
                accountExternalRef.add(a.ExternalReference__c);
            }			
        }
        List<Lead> leads = [Select l.IsConverted, l.Id, l.Email,l.Account__c,l.Application_Status__c
                            From Lead l
                            where l.IsConverted = false and l.Id in:accountExternalRef];   	
        for(Lead lead:leads){
           mappedLeads.put(String.valueOf(lead.Id),lead);           
        }
        return mappedLeads;
    }    
    
    public static Map<String,Lead> GetLeadsWithAccountRelation(List<Account> accounts){
		Set<Id> leadIds=new Set<Id>();
        Map<String,Lead> mappedLeads=new Map<String,Lead>();   
        for(Account acc:accounts){
            leadIds.add(acc.Related_Lead__c);
        }        
		List<Lead> leads = [Select l.IsConverted, l.Id, l.Email,l.Account__c,l.Application_Status__c
                            From Lead l
                            where l.IsConverted = false and l.Id in:leadIds];	
        for(Lead lead:leads){
           mappedLeads.put(String.valueOf(lead.Id),lead);           
        }   
        return mappedLeads;
    }
    
	public static void CreateLeadLinkAndUpdateLeadData(List<Account> newList,Boolean updateAccount){
        Map<String,Lead> mappedLeadsByExternalReference=GetLeadsWithAccountExternalReference(newList);
        Map<String,Lead> mappedLeads=GetLeadsWithAccountEmailAddress(newList);
        
        if (mappedLeads.size()==0 &&  mappedLeadsByExternalReference.size()==0){
            return;
        }        
        List<Lead> updatedLeads=new List<Lead>();
        for(Account acc:newList){   
            //Check the external ref first
            Lead lead=mappedLeadsByExternalReference.get(acc.ExternalReference__c);
            if (lead==null){
                lead=mappedLeads.get(acc.PersonEmail.toLowerCase());
            } 
            if (lead!=null){ 
                if (updateAccount && lead.Account__c==null){
                    acc.Related_Lead__c=lead.id;
                }                
                FieldMapperHelper.AccountToLeadMapping(lead,acc); 
                updatedLeads.add(lead);
            }
        }
        if (updatedLeads.size()>0){
            System.debug('Account insert/update updating lead');                
            update updatedLeads;
        }
    } 
	
    public static void UpdateLeadData(List<Account> accList){
        Map<String,Lead> mappedLeads=GetLeadsWithAccountRelation(accList);
        
		List<Lead> updatedLeads=new List<Lead>();
        for(Account acc:accList){   
            Lead lead=mappedLeads.get(acc.Related_Lead__c);
            if (lead!=null){           
                FieldMapperHelper.AccountToLeadMapping(lead,acc); 
                updatedLeads.add(lead);
            }
        }  
		if (updatedLeads.size()>0){
            System.debug('Account insert/update updating lead');                
            update updatedLeads;
        }        
    }
    
	public static void executeLeadLink(List<Account> newList, List<Account> oldList,Boolean isUpdate,Boolean isBefore,Boolean isInsert,Boolean isAfter){      
        if(isInsert && isBefore){     
            Map<String,Lead> mappedLeadsByExternalReference=GetLeadsWithAccountExternalReference(newList);
            Map<String,Lead> mappedLeads=GetLeadsWithAccountEmailAddress(newList);      
            for(Account acc:newList){                
                Lead lead=mappedLeadsByExternalReference.get(acc.ExternalReference__c);
                if (lead==null){
                    lead=mappedLeads.get(acc.PersonEmail.toLowerCase());
                }  
                //Check if lead is not null and lead does not have an account already associated with it
                if (lead!=null && lead.Account__c==null){
                    acc.Related_Lead__c=lead.Id;
                }
                acc.RecordTypeId = FieldMapperHelper.MarkPlusApplicants(acc);
            }
        } 
        if (isInsert && isAfter){           
            CreateLeadLinkAndUpdateLeadData(newList,false);
        }        
        if (isBefore && isUpdate){
			List<Account> accWithNoLeads=new List<Account>();
			List<Account> accWithLeads=new List<Account>();			
		    for(Account acc:newList)
		 	{
		       	acc.RecordTypeId = FieldMapperHelper.MarkPlusApplicants(acc);		       
				if (acc.Related_Lead__c==null){
					accWithNoLeads.add(acc);						
		       	}
		       	else{
		       		accWithLeads.add(acc);
		       	}
		 	}
         	
         	//Send the accounts that do not have leads - we need to search if leads exists and then link the lead
			CreateLeadLinkAndUpdateLeadData(accWithNoLeads,true);	
            
            //If the account already has a lead, just update the lead no need to relink the accounts
            UpdateLeadData(accWithLeads);
        }
    }    
}

Test Class:
@isTest
private class Test_AccountTriggerHandler {

	static RecordType applicantRecordType;
	
	static {
		applicantRecordType = [Select Id From RecordType Where Name = 'Applicants' and sobjecttype = 'Account']; 
	}

    static testMethod void testAccountError() {
        test.startTest();
        
        Account acc = CreateTestData.createAccount('U_testAccount@tntp.org','externalRef');              
        acc.FirstName = 'Test Account3';
        update acc;
        delete acc;
        test.stopTest();
    }
    
    static testMethod void Account_Gets_Linked_To_Lead_Based_On_Account_ExternalReference_But_With_Different_Emails() {
        test.startTest();
        
        //Create a lead
        Lead lead= CreateTestData.createLead('U_test2@tntp.org'); 
        String externalRef=String.valueOf(lead.Id);
		
        //Createa a an account with different email address
        Account acc  = CreateTestData.createAccount('U_test1@tntp.org',externalRef); 
        
        //Get account from database
        List<Account> accounts = [Select a.PersonEmail,a.Id,a.Related_Lead__c From Account a Where a.PersonEmail ='U_test1@tntp.org'];			

        test.stopTest();
    }    
    
	static testMethod void Account_Gets_Linked_To_Lead_Based_On_Account_Email() {
        test.startTest();
        
        //Create a lead
        Lead lead= CreateTestData.createLead('U_test1@tntp.org'); 

        //Createa a an account with same email address
        Account acc  = CreateTestData.createAccount('U_test1@tntp.org','someinvalidId'); 
        
        //Get Lead from database
        List<Lead> leads = [Select l.Id,Account__c From Lead l Where l.Email ='U_test1@tntp.org'];	
        
        //Get account from database
        List<Account> accounts = [Select a.PersonEmail,a.Id,a.Related_Lead__c From Account a Where a.PersonEmail ='U_test1@tntp.org'];			
        
        test.stopTest();
    }    
    
    static testMethod void Account_Doesnot_Get_Linked_To_Lead_Because_Of_Missing_Account_ExternalReference() {
        test.startTest();
        
        //Create a lead
        Lead lead= CreateTestData.createLead('U_test1@tntp.org'); 

        //Createa a an account with different email address
        Account acc  = CreateTestData.createAccount('U_test2@tntp.org','someinvalidId'); 
        
        //Get Lead from database
        List<Lead> leads = [Select l.Id From Lead l Where l.Email ='U_test1@tntp.org'];	
        
        //Get account from database
        List<Account> accounts = [Select a.PersonEmail,a.Id,a.Related_Lead__c From Account a Where a.PersonEmail ='U_test2@tntp.org'];			
		
        //Should find no match
        System.assertEquals(accounts[0].Related_Lead__c, null);
        
        test.stopTest();
    } 
}

Thank you in advance for any assistance you can provide.