function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vinni1vinni1 

Help with Test Class for Trigger Please

Hi All,

 

 Need  for Trigger

 

Trigger ::

 

trigger UpdateLead on Lead(before update) {
    List<Lead> allleads = new List<Lead> ();
    Set<Id> leadIds = new Set<Id> ();
    datetime myDateTime = datetime.now();
    if(Trigger.isUpdate){
        for (Lead newlead: Trigger.new) {
            Lead oldLead = Trigger.oldMap.get(newlead.Id);     
             if ((newlead.Mobile__c == 'N' || newlead.Phone__c == 'N')  && (newlead.Last_Successful_c == NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())>7)){
                newlead.DonotCall= true;
               newlead.Status = 'Closed';
                newlead.Closed_Reason__c = 'all';
            }
             else if ((newlead.Mobile__c == 'N' || newlead.Phone__c == 'Y')  && (newlead.Last_Successful_c == NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())>7)){
                newlead.DonotCall= true;
                newlead.Status = 'Closed';
                newlead.Closed_Reason__c = 'all';
            }
                                          
              if ((newlead.Mobile__c  == 'N' || newlead.Phone__c == 'N')  && (newlead.Last_Successful_c <> NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())<=7)){
                newlead.DonotCall= true; 
                newlead.Status = 'Closed';  
                newlead.Closed_Reason__c = 'all';          
            }                          
              else if ((newlead.Mobile__c  == 'N' || newlead.Phone__c == 'Y')  && (newlead.Last_Successful_c <> NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())<=7)){
              newlead.DonotCall= false;             
            }

        }
    }
}

 

Test Class::

 

@isTest
public class TestUpdateLead
{
  static testMethod void myTest()
  {
   
    Lead l = new lead(LastName='Test123',Company = 'Hp',Email='hp@hp.com',Phone='9738335102',C_Mobile_Opt_in1__c = 'N',Phone_Opt_in__c = 'N',Last_Successful_Call__c = NULL,CreatedDate= System.now()-7);
   
    insert l;
   
    list<lead> l1 = [select id,lastname,phone from lead where lastname='Test123' limit 1];
    system.AssertEquals('Test123',l1[0].lastname);
      }
}

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

you have to insert and update lead record in your test class as per the condition you have written in your class.

All Answers

hitesh90hitesh90

Hi vinni,

 

You haven't writter update in your test class.

and Trigger is fired on before update.

 

so update your Test class as below.

 

Test class:

@isTest
public class TestUpdateLead{
	static testMethod void myTest(){
		Lead l = new lead(LastName='Test123',Company = 'Hp',Email='hp@hp.com',Phone='9738335102',C_Mobile_Opt_in1__c = 'N',Phone_Opt_in__c = 'N',Last_Successful_Call__c = NULL,CreatedDate= System.now()-7);
		insert l;
		
		l.Mobile__c = 'N';
		update l;
		
		list<lead> l1 = [select id,lastname,phone from lead where lastname='Test123' limit 1];
		system.AssertEquals('Test123',l1[0].lastname);
	}
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

 

 

vinni1vinni1

Thank's for your replay Hitesh...

 

My code coverage is 42% onely how to improve...

 

 

 trigger UpdateLead on Lead(before update) {
 2       List<Lead> lstLead = new List<Lead> ();
 3       Set<Id> leadIds = new Set<Id> ();
 4       datetime myDateTime = datetime.now();
 5       if(Trigger.isUpdate){
 6           for (Lead newlead: Trigger.new) {
 7               Lead oldLead = Trigger.oldMap.get(newlead.Id);
 8               if ((newlead.C_Mobile_Opt_in1__c == 'N' || newlead.Phone_Opt_in__c == 'N')  && (newlead.Last_Successful__c == NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())>7)){
 9                   newlead.DonotCall= true;
 10                  newlead.Status = 'Closed';
 11                   newlead.Closed_Reason__c = 'All';
 12               }
 13                else if ((newlead.C_Mobile_Opt_in1__c == 'N' || newlead.Phone_Opt_in__c == 'Y')  && (newlead.Last_Successful__c == NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())>7)){
 14                   newlead.DonotCall= true;
 15                   newlead.Status = 'Closed';
 16                   newlead.Closed_Reason__c = 'All';
 17               }
 18               else if ((newlead.Mobile__c == 'Y' || newlead.Phone__c= 'M')& (newlead.Last_Successful__c == NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())>7)){ 
 19                   newlead.DonotCall= true;
 20                   newlead.Status = 'Closed';
 21                   newlead.Closed_Reason__c = 'All';
 22               }
 23             else if ((newlead.Mobile__c == 'Y' || newlead.Phone__c= 'Y')  && (newlead.Last_Successful__c == NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())>7)){
 24                   newlead.DonotCall= true;
 25                  newlead.Status = 'Closed';
 26                   newlead.Closed_Reason__c = 'All';
 27               }
 28             if ((newlead.Mobile__c == 'M || newlead.Phone__c= 'M)  && (newlead.Last_Successful__c <> NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())<=7)){
 29                   newlead.DonotCall= true; 
 30                   newlead.Status = 'Closed';
 31                   newlead.Closed_Reason__c = 'All';          
 32               }
 33                else if ((newlead.Mobile__c == 'M || newlead.Phone__c= 'Y')  && (newlead.Last_Successful__c <> NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())<=7)){
 34                  newlead.Call= false;             
 35               }
 36               else if ((newlead.Mobile__c == 'M || newlead.Phone__c= 'N)  && (newlead.Last_Successful__c <> NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())<=7)){
 37                  newlead.DonotCall= true;
 38                   newlead.Status = 'Closed';
 39               }
 40             else if ((newlead.Mobile__c == 'Y' || newlead.Phone__c= 'Y')  && (newlead.Last_Successful__c <> NULL && newlead.CreatedDate.Date().daysBetween(myDateTime.Date())<=7)){
 41                  newlead.DonotCall= true;
 42               }
 43           }
 44       }
 45   }

hitesh90hitesh90

you have to insert and update lead record in your test class as per the condition you have written in your class.

This was selected as the best answer