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
BaguiarBaguiar 

Help on Test class... not passing

Thanks for all the previous help and here goes one that might be even simpler but I can't get it to validate.

 

ublic class testchangeTrigger {
  public static testMethod void test1() {
    List<Lead> leads = new List<Lead>{ new Lead(FirstName='Test 1',LastName='Test 1',Status='Open',Company='Test1',Sales_Cycle__c='Not yet contacted'),
                                       new Lead(FirstName='Test 2',LastName='Test 2',Status='Open',Company='Test2',Sales_Cycle__c='Open') };
    insert leads;
    List<task> tasks = new List<task>{ new task(WhoId=Leads[0].Id,Subject='Get Appointment',ActivityDate=System.today(), stages__c='in progress'),
                                          new task(WhoId=Leads[1].Id,Subject='Something Else',ActivityDate=System.today()) };
    insert tasks;
    Lead[] leadsgo = [select id,Sales_Cycle__c from lead where id in :leads];
    System.assertEquals('Open',Leadsgo[1].Sales_Cycle__c);
    System.assertEquals('Contacted',Leadsgo[0].Sales_cycle__c);
  }
}

 and the trigger

 

trigger contacted on Task (after update) {
  Set<Id> recordIds = new Set<Id>();
  List<Lead> leadsToUpdate = new List<Lead>();

  for(task t:Trigger.new)
    if(t.subject=='Get Appointment' && t.stages__C=='in progress')
      recordIds.add(t.whoid);

  leadsToUpdate = [select id from lead where sales_cycle__C = 'Not yet contacted' and id in :recordIds];

  for(Lead l:leadsToUpdate)
    L.sales_cycle__C = 'Contacted';

  update leadsToUpdate;
}

 

I'm not sure if this will relate,  but the trigger is (after update) and when I had pretty much the same structure to test another trigger but (before insert) it worked fine.

 

Thanks a lot!

B

Best Answer chosen by Admin (Salesforce Developers) 
shra1_devshra1_dev

Hi Baguiar,

 

The Trigger won't fire for the above test class. Because it is an after Update trigger and you are not performing any update action in your test class.

 

Try to update the task in ur test class then I hope it works...

 

 

 

Regards,

shravan

All Answers

shra1_devshra1_dev

Hi Baguiar,

 

The Trigger won't fire for the above test class. Because it is an after Update trigger and you are not performing any update action in your test class.

 

Try to update the task in ur test class then I hope it works...

 

 

 

Regards,

shravan

This was selected as the best answer
BaguiarBaguiar

Awesome! Thanks Shra1_dev.

 

Thats what it was. Added:

List<task> taskstoupdate = new list<task> {[select id from task where subject = 'Get Appointment' and id in :tasks]};
 for(task t:taskstoupdate)
 t.stages__C = 'in progress';

 update taskstoupdate;

 

 

To it and worked fine.