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
MobileMobile 

Test Class needed for my trigger. HELPPPP

i am a beginner i managed to create the below trigger to update the campaign member status before lead update. .  

 

can any one help me writing a test class for it.

 

thanks in advance for your help.

 

trigger UpdateMemberStatus on Lead ( before insert ,before update) {

Lead[] leads = Trigger.new;

for (Lead l:leads){

List<CampaignMember> status = new List<CampaignMember>();

// select the status for the most recent campaign for that lead

status = [SELECT status FROM CampaignMember  WHERE leadId = :l.id order by id desc limit 1];

// make sure the lead is assigned to the campaign.

if (!status.isEmpty())

{

status[0].status = l.outcome__c;

update status;

}

}

Best Answer chosen by Admin (Salesforce Developers) 
jpwagnerjpwagner

first of all this trigger must be bulkafied!  (make all SOQL queries outside of loops.)

 

secondly, a test class just contains any action that fires the trigger.

in this case create a fake lead definition, insert it (make sure the status is not empty), update it, and check the results with an assertion.

 

even something as simple as the following will work...

 

public class TestClass {
    public static testmethod void t1() {
     Lead abc = new Lead(fields = values);

     insert abc;

     

     status = [select status from...];

     assertequals(status[0].status,abc.outcome__c);

  }

}

All Answers

jpwagnerjpwagner

first of all this trigger must be bulkafied!  (make all SOQL queries outside of loops.)

 

secondly, a test class just contains any action that fires the trigger.

in this case create a fake lead definition, insert it (make sure the status is not empty), update it, and check the results with an assertion.

 

even something as simple as the following will work...

 

public class TestClass {
    public static testmethod void t1() {
     Lead abc = new Lead(fields = values);

     insert abc;

     

     status = [select status from...];

     assertequals(status[0].status,abc.outcome__c);

  }

}

This was selected as the best answer
MobileMobile
Thank you very much. that worked. i really appreciate your help and advice. i actually have more understanding of test methods now.