+ Start a Discussion
sam_Adminsam_Admin 

Test class help for 2 triggers

I have the below triggers and test class for them, Trigger-1 has 85% coverage and Trigger-2 has 66%, can anyone please help me in improving code coverage? Highlighted lines in bold which are not covering 

Trigger-1:   

    trigger AccountName_Opp on Opportunity (before insert, before update ){
    
        Map<String, Opportunity> OppMap = new Map<String, Opportunity>();
    
        for(Opportunity opp:Trigger.new)
        {
            OppMap.put(opp.Account_Name__c, opp);
    
        }
    
        List<Account> accLst = [SELECT ID, Name FROM Account where Name in :OppMap.keySet()];
    
        for(Account acct : accLst){
            OppMap.get(acct.Name).AccountId = acct.ID;
        }
    }

Trigger-2: 

    trigger AccountName_Contact on Contact (before insert, before update ){
    
        Set<String> conaccounts = new Set<String>();
    
        for(Contact con:Trigger.new)
        {
            conaccounts.add(con.Account_Name__c);
        }
    
        List<Account> accLst = [SELECT ID, Name FROM Account where Name in :conaccounts];
        System.debug(accLst);
        for(Account acct : accLst){
            for(Contact co : Trigger.new){
                if(co.Account_Name__c == acct.Name)
                    co.accountid = acct.id;

            }        
        }
    
    }

Testclass:
 

    @istest
    public class FFCNAccountName
    {
      static testmethod void myTest()
      {
        Map<String, Opportunity> OppMap = new Map<String, Opportunity>();
        
        Account acc = new Account(Name = 'test', phone = '1234567890');
        insert acc;
        
        Opportunity opp = new Opportunity(SaleType__c = 'new', Name = 'test', AccountId = acc.Id, Amount = 50, CurrencyIsoCode = 'USD');
        insert opp;
        
        Contact con = new Contact( LastName = 'Test2', Email = 'test.user@gmail.com', AccountId = acc.Id );
        insert con;
      }
    }
Best Answer chosen by sam_Admin
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- you need to set Account_Name__c field
Try to update your test class like below
@istest
public class FFCNAccountName
{
	static testmethod void myTest()
	{

		Account acc = new Account(Name = 'test', phone = '1234567890');
		insert acc;

		Contact con = new Contact( LastName = 'Test2', Email = 'test.user@gmail.com', AccountId = acc.Id ,Account_Name__c ='test');
		insert con;

		Opportunity opp = new Opportunity(SaleType__c = 'new', Name = 'test', AccountId = acc.Id, Amount = 50, CurrencyIsoCode = 'USD' ,Account_Name__c ='test');
		insert opp;

		
	}
}
Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
NOTE:- you need to set Account_Name__c field
Try to update your test class like below
@istest
public class FFCNAccountName
{
	static testmethod void myTest()
	{

		Account acc = new Account(Name = 'test', phone = '1234567890');
		insert acc;

		Contact con = new Contact( LastName = 'Test2', Email = 'test.user@gmail.com', AccountId = acc.Id ,Account_Name__c ='test');
		insert con;

		Opportunity opp = new Opportunity(SaleType__c = 'new', Name = 'test', AccountId = acc.Id, Amount = 50, CurrencyIsoCode = 'USD' ,Account_Name__c ='test');
		insert opp;

		
	}
}
Let us know if this will help you
 
This was selected as the best answer
sam_Adminsam_Admin
Thank you, that worked
 
jigarshahjigarshah
Sam,

I just found this Blog Post (https://automationchampion.com/2015/03/13/getting-started-with-process-builder-part-10-auto-forward-records-to-a-connection/) which uses an out of box Process Builder approach to automatically share records to a Salesforce to Salesforce connection without having the need to write code. You might want to give it a try since that would help you save all the effort and pain of writing test code for the earlier written Apex Trigger during your production deployment.