+ Start a Discussion
Mayank.msMayank.ms 

Need test class for the code coverage for the task trigger

trigger OnboardingPackage on Task (after insert) {
    
    Set<String> whatIDs = new Set<String>();
    
    for (Task t : Trigger.new) {
        whatIDs.add(t.whatID);
    }
    
    List<Opportunity> opps = [SELECT Id, Product__c, Accounting_Platform__c,Plan__c,Billing_Cycle__c FROM Opportunity WHERE Id =: whatIDs];
    
    List<Contact> conList = new List<Contact>([select Email from Contact where Id In (select ContactId from OpportunityContactRole where OpportunityId = :whatIDs)]); 
    
    EmailTemplate et1 = [SELECT id FROM EmailTemplate WHERE Name = 'Onboarding Package Desktop included']; // Stephanie
    EmailTemplate et2 = [SELECT id FROM EmailTemplate WHERE Name = 'Onboarding Package Desktop not included']; //Andrew
    EmailTemplate et3 = [SELECT id FROM EmailTemplate WHERE Name = 'Onboarding Package Cloud']; // Andrew
    EmailTemplate et4 = [SELECT id FROM EmailTemplate WHERE Name = 'Onboarding Package Shiplark'];
    EmailTemplate et5 = [SELECT id FROM EmailTemplate WHERE Name = 'Accounting Consult']; 
    EmailTemplate et6 = [SELECT id FROM EmailTemplate WHERE Name = 'Onboarding Package Desktop included for POS']; // Aaron
    EmailTemplate et7 = [SELECT id FROM EmailTemplate WHERE Name = 'Onboarding Package Desktop included for PPI']; //Andrew
    
 
    for(Task t:Trigger.New){
        
        for(Opportunity o :opps){
            
            for(Contact c: conList){
            
               if((t.subject=='Implementation service : Install') && (o.Plan__c=='EnterpriseHosting' || o.Plan__c=='pro/premierHosting' ||  o.Accounting_Platform__c== 'QuickBooks Enterprise (US)' || o.Accounting_Platform__c=='NetSuite')){
                      sendNotification(et1.id, c.id, o.id);
                        
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='QuickBooks POS (US)')){
                    sendNotification(et6.id, c.id, o.id); 
                       
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='QuickBooks Pro/Premier (US)' ||  o.Accounting_Platform__c== 'QuickBooks CA/UK/ZA/AU') && o.Billing_Cycle__c=='Yearly'){
                    sendNotification(et7.id, c.id, o.id); 
                       
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='QuickBooks Pro/Premier (US)' ||  o.Accounting_Platform__c== 'QuickBooks CA/UK/ZA/AU') && o.Billing_Cycle__c=='Monthly' ){
                    sendNotification(et2.id, c.id, o.id); 
                       
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='QuickBooks Online (US)' || o.Accounting_Platform__c== 'Xero (US)')){
                    sendNotification(et3.id, c.id, o.id);  
                  
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='Shiplark')){
                    sendNotification(et4.id, c.id, o.id);  
                      
                }else if(t.subject=='Implementation service : Pro Advisor'){
                    sendNotification(et5.id, c.id, o.id);  
                  
                }
                
            }
        }            
    }
    
  public void sendNotification(string eTempID,string tgtObjID,string  whatID){

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        // CONFIGURE MAIL
          mail.setTemplateId(eTempID); 
          mail.setTargetObjectId(tgtObjID);
          mail.setWhatId(whatID);
          mail.setReplyTo('customersuccess@webgility.com');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

}
Best Answer chosen by Mayank.ms
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
@isTest
public class testOnboardingPackage 
{


    static testMethod void testOnboardingPackageTest() 
	{
		Account testAcct = new Account (Name = 'Kamran test Account');
		insert testAcct;
		system.debug('testaccount'+testAcct.ID);
        Contact con = new Contact(LastName ='Kamran',Email='ahmeds+22julytest13@webgility.com',
                             AccountId = testAcct.ID);
        insert con;
        
		Opportunity oppt = new Opportunity(Name ='New mAWS Deal2',
                            AccountID = testAcct.ID,
                            StageName = 'Trial',
                            Amount = 2000,
							Plan__c = 'EnterpriseHosting',
                            Accounting_Platform__c='NetSuite',
                            CloseDate = System.today(),
							Billing_Cycle__c='Yearly'
                            );
		insert oppt;
		
		OpportunityContactRole role1 = new OpportunityContactRole ();
		role1.ContactId = con.id;
		role1.OpportunityId = oppt.id;
		role1.IsPrimary=TRUE;
		role1.Role='Decision Maker'; // add valid name according to ur org
		insert role1;

		Opportunity oppt1 = new Opportunity(Name ='New mAWS Deal2',
                            AccountID = testAcct.ID,
                            StageName = 'Trial',
                            Amount = 2000,
							Plan__c = 'pro/premierHosting',
                            Accounting_Platform__c='QuickBooks Pro/Premier (US)',
                            CloseDate = System.today(),
							Billing_Cycle__c='Monthly'
                            );
		insert oppt1;
		
		OpportunityContactRole role1 = new OpportunityContactRole ();
		role1.ContactId = con.id;
		role1.OpportunityId = oppt1.id;
		role1.IsPrimary=TRUE;
		role1.Role='Decision Maker'; // add valid name according to ur org
		insert role1;
		
		
		Task tk1=new Task(whatid=oppt.id, Subject='Implementation service : Install',Status='Open',Priority='High');
		insert tk1;
      
		Task tk2=new Task(whatid=oppt1.id, Subject='Implementation service : Pro Advisor',Status='Open',Priority='High');
		insert tk2;
		

		Task tk1=new Task(whatid=oppt.id, Subject='Implementation service : Install',Status='Open',Priority='High');
		insert tk1;
		
		
                
    }
    
    
}


 

All Answers

Ramon PereiraRamon Pereira
Post the code where you encountered the problem. 

Use Map or List to get your data. So you realize only one SOQL.
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class.  I hope that will help ypu
@isTest 
public class OnboardingPackageTest 
{
	static testMethod void testMethod1() 
	{
		Account testAcct = new Account (Name = 'My Test Account');
		insert testAcct;

		Contact cont = new Contact();
		cont.email='test@test.com';
		cont.firstName ='Test FiestName';
		cont.LastName ='LastName';
		insert cont;
		
		Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
					AccountID = testAcct.ID,
					StageName = 'Customer Won',
					Amount = 3000,
					CloseDate = System.today() );
		insert oppt;
		OpportunityContactRole role1 = new OpportunityContactRole ();
		role1.ContactId = cont.id;
		role1.OpportunityId = oppt.id;
		role1.IsPrimary=TRUE;
		role1.Role='Decision Maker'; // add valid name according to ur org
		insert role1;
		
		
		Test.StartTest();
			
		Task t = new Task(
			WhatID = oppt.id,
			Subject='Donni',
			Status='Completed',
			Priority='Normal'
			);
        insert t;  

			
		Test.StopTest();
	}
}

Please check below post for more information on test classes
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Please let us know if this will help you

Thanks
Amit Chaudhary
Mayank.msMayank.ms
Hi Amit Chaudhary,

Thanks for the reply. Now with you test class getting 61% code coverage.
User-added image
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
@isTest
public class testOnboardingPackage 
{


    static testMethod void testOnboardingPackageTest() 
	{
		Account testAcct = new Account (Name = 'Kamran test Account');
		insert testAcct;
		system.debug('testaccount'+testAcct.ID);
        Contact con = new Contact(LastName ='Kamran',Email='ahmeds+22julytest13@webgility.com',
                             AccountId = testAcct.ID);
        insert con;
        
		Opportunity oppt = new Opportunity(Name ='New mAWS Deal2',
                            AccountID = testAcct.ID,
                            StageName = 'Trial',
                            Amount = 2000,
							Plan__c = 'EnterpriseHosting',
                            Accounting_Platform__c='NetSuite',
                            CloseDate = System.today(),
							Billing_Cycle__c='Yearly'
                            );
		insert oppt;
		
		OpportunityContactRole role1 = new OpportunityContactRole ();
		role1.ContactId = con.id;
		role1.OpportunityId = oppt.id;
		role1.IsPrimary=TRUE;
		role1.Role='Decision Maker'; // add valid name according to ur org
		insert role1;

		Opportunity oppt1 = new Opportunity(Name ='New mAWS Deal2',
                            AccountID = testAcct.ID,
                            StageName = 'Trial',
                            Amount = 2000,
							Plan__c = 'pro/premierHosting',
                            Accounting_Platform__c='QuickBooks Pro/Premier (US)',
                            CloseDate = System.today(),
							Billing_Cycle__c='Monthly'
                            );
		insert oppt1;
		
		OpportunityContactRole role1 = new OpportunityContactRole ();
		role1.ContactId = con.id;
		role1.OpportunityId = oppt1.id;
		role1.IsPrimary=TRUE;
		role1.Role='Decision Maker'; // add valid name according to ur org
		insert role1;
		
		
		Task tk1=new Task(whatid=oppt.id, Subject='Implementation service : Install',Status='Open',Priority='High');
		insert tk1;
      
		Task tk2=new Task(whatid=oppt1.id, Subject='Implementation service : Pro Advisor',Status='Open',Priority='High');
		insert tk2;
		

		Task tk1=new Task(whatid=oppt.id, Subject='Implementation service : Install',Status='Open',Priority='High');
		insert tk1;
		
		
                
    }
    
    
}


 
This was selected as the best answer
Mayank.msMayank.ms
Thanks a lot Amit. Got 86% code coverage.
Mayank.msMayank.ms
Hi Amit,

Can you please help me on the trigger OnboardingPackage again. I am trying to upload a new trigger on live but I am getting error Too many SOQL queries: 101 on the OnboardingPackage trigger. I have also used Static variable for this trigger now but i am unalbe to make a code coverage for it. Can you please help on it.

Trigger:  OnboardingPackage 
trigger OnboardingPackage on Task (after insert) {
    
    Set<String> whatIDs = new Set<String>();
    
    for (Task t : Trigger.new) {
        whatIDs.add(t.whatID);
    }
     if(OnboardingPackageHandler.isFirstTime){
        OnboardingPackageHandler.isFirstTime = false;
    List<Opportunity> opps = [SELECT Id, Product__c, Accounting_Platform__c,Plan__c,Billing_Cycle__c FROM Opportunity WHERE Id =: whatIDs];
   
    List<Contact> conList = new List<Contact>([select Email from Contact where Id In (select ContactId from OpportunityContactRole where OpportunityId = :whatIDs)]); 
    Map<String, Id> EmailTempMap = new Map<String, Id>();
    
    for(EmailTemplate emailTempList : [SELECT id,Name FROM EmailTemplate WHERE Name in ('Onboarding Package Desktop included', 'Onboarding Package Desktop not included', 'Onboarding Package Cloud','Onboarding Package Desktop included for POS','Onboarding Package Desktop included for PPI','Onboarding Package Shiplark','Accounting Consult')])
    {
        EmailTempMap.put(emailTempList.Name, emailTempList.Id);
    }

    for(Task t:Trigger.New){
        
        for(Opportunity o :opps){
            
            for(Contact c: conList){
            
               if((t.subject=='Implementation service : Install' || t.subject=='Implementation : Setup') && (o.Plan__c=='EnterpriseHosting' || o.Plan__c=='pro/premierHosting' ||  o.Accounting_Platform__c== 'QuickBooks Enterprise (US)' || o.Accounting_Platform__c=='NetSuite')){
                   sendNotification(EmailTempMap.get('Onboarding Package Desktop included'), c.id, o.id);
                        
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='QuickBooks POS (US)')){
                    sendNotification(EmailTempMap.get('Onboarding Package Desktop included for POS'), c.id, o.id);
                       
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='QuickBooks Pro/Premier (US)' ||  o.Accounting_Platform__c== 'QuickBooks CA/UK/ZA/AU') && o.Billing_Cycle__c=='Yearly'){
                    sendNotification(EmailTempMap.get('Onboarding Package Desktop included for PPI'), c.id, o.id); 
                       
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='QuickBooks Pro/Premier (US)' ||  o.Accounting_Platform__c== 'QuickBooks CA/UK/ZA/AU') && o.Billing_Cycle__c=='Monthly' ){
                    sendNotification(EmailTempMap.get('Onboarding Package Desktop not included'), c.id, o.id); 
                       
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='QuickBooks Online (US)' || o.Accounting_Platform__c== 'Xero (US)')){
                    sendNotification(EmailTempMap.get('Onboarding Package Cloud'), c.id, o.id); 
                  
                }else if((t.subject=='Implementation service : Install') && (o.Accounting_Platform__c=='Shiplark')){
                     sendNotification(EmailTempMap.get('Onboarding Package Shiplark'), c.id, o.id); 
                      
                }else if(t.subject=='Implementation service : Pro Advisor'){
                    sendNotification(EmailTempMap.get('Accounting Consult'), c.id, o.id);
                  
                }
              } 
            }
        }            
    }
    
  public void sendNotification(string eTempID,string tgtObjID,string  whatID){

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        // CONFIGURE MAIL
          mail.setTemplateId(eTempID); 
          mail.setTargetObjectId(tgtObjID);
          mail.setWhatId(whatID);
          mail.setReplyTo('customersuccess@test.com');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

}

Test Class 
 
@isTest
public class testOnboardingPackage {


    static testMethod void testOnboardingPackageTest() {
    Account testAcct = new Account (Name = 'Kamran test Account');
		insert testAcct;
		system.debug('testaccount'+testAcct.ID);
        Contact con = new Contact(LastName ='Kamran',Email='ahmeds+22julytest13@webgility.com',
                             AccountId = testAcct.ID);
        insert con;
        Test.StartTest();
		Opportunity oppt = new Opportunity(Name ='testOnboardingPackageTest',
                            AccountID = testAcct.ID,
                            StageName = 'Trial',
                            Amount = 2000,
							Plan__c = 'EnterpriseHosting',
                            Accounting_Platform__c='NetSuite',
                            CloseDate = System.today(),
							Billing_Cycle__c='Yearly'
                            );
		insert oppt;
		
		OpportunityContactRole role1 = new OpportunityContactRole ();
		role1.ContactId = con.id;
		role1.OpportunityId = oppt.id;
		role1.IsPrimary=TRUE;
		role1.Role='Decision Maker'; // add valid name according to ur org
		insert role1;

		Opportunity oppt1 = new Opportunity(Name ='testOnboardingPackageTest2',
                            AccountID = testAcct.ID,
                            StageName = 'Closed Won',
                            Amount = 2000,
							Plan__c = 'pro/premierHosting',
                            Accounting_Platform__c='QuickBooks Pro/Premier (US)',
                            CloseDate = System.today(),
							Billing_Cycle__c='Monthly'
                            );
		insert oppt1;
		
		OpportunityContactRole role2 = new OpportunityContactRole ();
		role2.ContactId = con.id;
		role2.OpportunityId = oppt1.id;
		role2.IsPrimary=TRUE;
		role2.Role='Decision Maker'; // add valid name according to ur org
		insert role2;
		
		
		Task tk1=new Task(whatid=oppt.id, Subject='Implementation service : Install',Status='Open',Priority='High');
		insert tk1;
      Test.stopTest();

		
    }
    
    
}

I am getting only 40% code coverage. 

Thanks