You need to sign in to do that
Don't have an account?
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;
}
}
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