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
Chitral ChaddaChitral Chadda 

test class

trigger LeadUpdate on Lead (before Update) 
{ 
   
  
 //list<lead> addhere = new list<lead>();
 for( lead ld :trigger.new)
  {
   if(ld.Do_Not_Market__c=='Blacklisted' && trigger.oldmap.get(ld.id).Do_Not_Market__c!= 'Blacklisted')
   {
    //Lead renewal = new lead();
    ld.Do_Not_Mail__c=True;
    ld.DoNotCall=True;
    ld.Email_Opt_Out__c=True;
    ld.Status='Dead';
    ld.LastName=ld.Lastname +'Renewal';
    ld.Company='ABC';
    //addhere.add(renewal);
    }
    
   
   }
   //insert addhere;
 }

only when do not market = blacklisted then it performs action
do not mail =true;
email opt out = true;


but with this test class initiall i inserted do not market = DNC this season
and then when i change do not market = blacklisted 
and test code
 i get error ....System.AssertException: Assertion Failed: Expected: true, Actual: false at line 24
@isTest
public class testLeadUpdate
{
static testMethod void updatelead()
{

lead ld = new lead();
//ld.Do_Not_Market__c='DNC This Season';
//ld.Do_Not_Mail__c=false;
//ld.Email_Opt_Out__c=false;
 //ld.LastName='Renewal';
//ld.Company='ABC';


ld.Do_Not_Market__c='DNC This Season';
ld.LastName='Renewal';
ld.Company='ABC';
insert ld;
system.assertEquals(false,ld.Do_Not_Mail__c);//line 24
system.assertEquals(false,ld.Email_Opt_Out__c);

ld.Do_Not_Market__c='Blacklisted';
update ld;
system.assertEquals(true,ld.Do_Not_Mail__c);
system.assertEquals(true,ld.Email_Opt_Out__c);






}
}

if i do like
@isTest
public class testLeadUpdate
{
static testMethod void updatelead()
{

lead ld = new lead();


ld.Do_Not_Market__c='DNC This Season';
ld.LastName='Renewal';
ld.Company='ABC';
insert ld;
system.assertEquals(false,ld.Do_Not_Mail__c);
system.assertEquals(false,ld.Email_Opt_Out__c);

ld.Do_Not_Market__c='Blacklisted';
update ld;
system.assertEquals(false,ld.Do_Not_Mail__c);
system.assertEquals(false,ld.Email_Opt_Out__c);


}
}

i get 100 % coverage...

but these fields should be set to true when do not market = blacklisted .
Best Answer chosen by Chitral Chadda
Anoop yadavAnoop yadav
Hi,
I tried at my end and got to know that we should use Query after update.
try the below code now.
@isTest
public class testLeadUpdate
{
static testMethod void updatelead()
{

lead ld = new lead();


ld.Do_Not_Market__c='DNC This Season';
ld.LastName='Renewal';
ld.Company='ABC';
insert ld;
system.assertEquals(false,ld.Do_Not_Mail__c);
system.assertEquals(false,ld.Email_Opt_Out__c);

ld.Do_Not_Market__c='Blacklisted';
update ld;

Lead newLead = [Select Id, Do_Not_Market__c, Do_Not_Mail__c, Email_Opt_Out__c From Lead Where Id =:ld.Id];
system.assertEquals(true,newLead.Do_Not_Mail__c);
system.assertEquals(true,newLead.Email_Opt_Out__c);


}
}

 

All Answers

Anoop yadavAnoop yadav
Hi,
Try with the below code.
@isTest
public class testLeadUpdate
{
static testMethod void updatelead()
{

lead ld = new lead();


ld.Do_Not_Market__c='DNC This Season';
ld.LastName='Renewal';
ld.Company='ABC';
insert ld;
system.assertEquals(false,ld.Do_Not_Mail__c);
system.assertEquals(false,ld.Email_Opt_Out__c);

Lead newLead = [Select Id, Do_Not_Market__c, Do_Not_Mail__c, Email_Opt_Out__c From Lead Where Id =:ld.Id];
newLead.Do_Not_Market__c='Blacklisted';
update newLead;
system.assertEquals(false,newLead.Do_Not_Mail__c);
system.assertEquals(false,newLead.Email_Opt_Out__c);


}
}

 
Anoop yadavAnoop yadav
Hi,
Use true at Line 20, 21 instead of false.
Anoop yadavAnoop yadav
Hi,
use the below one.
@isTest
public class testLeadUpdate
{
static testMethod void updatelead()
{

lead ld = new lead();
ld.Do_Not_Market__c='DNC This Season';
ld.LastName='Renewal';
ld.Company='ABC';
insert ld;
system.assertEquals(false,ld.Do_Not_Mail__c);
system.assertEquals(false,ld.Email_Opt_Out__c);

list<lead> ldupdate = [ select id ,Do_Not_Market__c,Do_Not_Mail__c,Email_Opt_Out__c from lead where id =: ld.id];
ld.Do_Not_Market__c='Blacklisted';
update ldupdate;
system.assertEquals(true,ldupdate.Do_Not_Mail__c);
system.assertEquals(true,ldupdate.Email_Opt_Out__c);

}
}

 
Chitral ChaddaChitral Chadda
hello
i get error
System.AssertException: Assertion Failed: Expected: true, Actual: false line 20
@isTest
public class testLeadUpdate
{
static testMethod void updatelead()
{

lead ld = new lead();


ld.Do_Not_Market__c='DNC This Season';
ld.LastName='Renewal';
ld.Company='ABC';
insert ld;
system.assertEquals(false,ld.Do_Not_Mail__c);
system.assertEquals(false,ld.Email_Opt_Out__c);

Lead newLead = [Select Id, Do_Not_Market__c, Do_Not_Mail__c, Email_Opt_Out__c From Lead Where Id =:ld.Id];
newLead.Do_Not_Market__c='Blacklisted';
update newLead;
system.assertEquals(true,newLead.Do_Not_Mail__c);
system.assertEquals(true,newLead.Email_Opt_Out__c);


}
}

 
Anoop yadavAnoop yadav
Hi,
Make sure that your trigger is firing by creating a lead record.
Chitral ChaddaChitral Chadda
hi anoop ,
yes trigger works .
if do not market = dnc this season then the fields are not checked
if i select do not market as blacklisted then fields are checked
Anoop yadavAnoop yadav
Hi,
I tried at my end and got to know that we should use Query after update.
try the below code now.
@isTest
public class testLeadUpdate
{
static testMethod void updatelead()
{

lead ld = new lead();


ld.Do_Not_Market__c='DNC This Season';
ld.LastName='Renewal';
ld.Company='ABC';
insert ld;
system.assertEquals(false,ld.Do_Not_Mail__c);
system.assertEquals(false,ld.Email_Opt_Out__c);

ld.Do_Not_Market__c='Blacklisted';
update ld;

Lead newLead = [Select Id, Do_Not_Market__c, Do_Not_Mail__c, Email_Opt_Out__c From Lead Where Id =:ld.Id];
system.assertEquals(true,newLead.Do_Not_Mail__c);
system.assertEquals(true,newLead.Email_Opt_Out__c);


}
}

 
This was selected as the best answer
Chitral ChaddaChitral Chadda
oh shuckkss . trure

we queried first and then  updated the record  naturally it wud b false if we do this way

thnx