+ Start a Discussion
Anil DuttAnil Dutt 

Code Coverage Required

Any one help me  writing Test Case for folowing Trigger for 100 % code coverage

 

trigger ConfirmationEmailContentTrigger on Opportunity (after insert) {
  List<Opportunity> listOpp = new List<Opportunity>();
    for (Opportunity op: trigger.new)
    {
        Opportunity oppNew = [SELECT Id, Confirmation_Email_Content__c FROM Opportunity WHERE Id =:op.Id];
        
        oppNew.Confirmation_Email_Content__c = '<p>I am pleased to inform you that your flights have been ticketed. Our ticketing department has also completed your seat assignments and confirmed any special requests that you have brought to my attention.</p>';
       
         if(op.AccountId != null)
    {
         Account acc =  [SELECT Id FROM Account WHERE Id =:op.AccountId limit 1];
          if(acc != null)
          {
            Contact[] con =  [SELECT Id, Email FROM Contact WHERE AccountId =:acc.id order by CreatedDate asc limit 1];
            if (con.size() > 0)
            {
              oppNew.Email__c = con[0].Email;
              oppNew.Notification_Email_Address__c = con[0].Email;
            }
          }
         }  
        listOpp.add(oppNew);
    }
    if (listOpp != null && !listOpp .isEmpty())
    {
       Database.update(listOpp);
    }
}

 

Thanks in advance

Navatar_DbSupNavatar_DbSup

Hi,


Try the below code as reference:


@isTest
private class testTmethod
{
public static testMethod void unitTestinsert_Contact_Activity4Task()
{
account a=new account(name='test');
insert a;
List<Opportunity> listOpp = new List<Opportunity>();
opportunity op=new opportunity(name='testOpp',CloseDate=system.today(),StageName='Closed Won',accountid=a.id);
listOpp.add(op);
insert listOpp;
}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Rahul SharmaRahul Sharma

Hi Anil Dutt,

 

You need to rewrite your code as it would throw exception with hitting governor limits while performing bulk updates.

Try avoiding Queries in for loop.

Refer to below documentation for learning more best practices:

Salesforce Trigger Best Practices

Trigger and Bulk Request Best Practices 

Writing test methods