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,

 

  Please help me for writing test class

 

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 == 'M' || 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 == 'M' || 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  == 'M' || 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  == 'M' || 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',Mobile__c = 'M',Phone__c = 'N',Last_Successful__c = NULL,CreatedDate= System.now()-7);
   
    insert l;
   
   
  }
}

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Try this

 

@isTest
public class TestUpdateLead
{
  static testMethod void myTest()
  {
   
    Lead l1 = new lead(LastName='Test123',Company = 'Hp',Email='hp@hp.com',Phone='9738335102',Mobile__c = 'M',Phone__c = 'N',CreatedDate= System.now()-8);
   
    insert l1;
   update l1;

 

 Lead l2 = new lead(LastName='Test123',Company = 'Hp',Email='hp@hp.com',Phone='9738335102',Mobile__c = 'M',Phone__c = 'Y',CreatedDate= System.now()-8);
   
    insert l2;
   update l2;

 

Lead l3 = new lead(LastName='Test123',Company = 'Hp',Email='hp@hp.com',Phone='9738335102',Mobile__c = 'M',Phone__c = 'N',Last_Successful_c = 'test',CreatedDate= System.now()-6);
   
    insert l3;
   update l3;

 

 Lead l4 = new lead(LastName='Test123',Company = 'Hp',Email='hp@hp.com',Phone='9738335102',Mobile__c = 'M',Phone__c = 'Y',Last_Successful_c = 'test',CreatedDate= System.now()-6);
   
    insert l4;
   update l4;
   
  }
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks