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
Season1224Season1224 

Help: Trigger not Working

Hi,

This is my first post on the board and am hoping someone can help!

I created a trigger to populate a custom Task field (Opportunity_Close_Date__c) with the Opportunity Close Date. 

Below please find my trigger and the test class I wrote.

 

The problem is that I'm getting 100% code coverage but the test is failing (Error Message: System.AssertException: Assertion Failed).

 

Any ideas or help would be greatly appreciated!!

Thanks!

 

Here's the trigger:

 

trigger OppCloseDate on Opportunity (after insert, after update) {

Set<Id> oppIds = new Set<Id>();
Set<Id> taskIds = new Set<Id>();{
for(Task t:[select Id, WhatID from Task]){
        String wId = t.WhatId;
        if(wId!=null && wId.startsWith('006')){
            taskids.add(t.WhatId);
for(Opportunity to:[select Id from Opportunity where Id in :taskIds]){
        oppIds.add(to.ID);
    }
    Set<Id> oppstoupdate = new Set<Id>();
    for(Integer i=0;i<trigger.new.size();i++){
        if(oppIds.contains(trigger.new[i].Id)){
            oppstoupdate.add(Trigger.new[i].id);

if(oppstoupdate.size() > 0){

   List<Opportunity> oList = [select Id, CloseDate from Opportunity where Id in :oppstoupdate];
    
   List<Task> taskstoupdate = new List<Task>();{
   for(Task tsk : [select Id, Opportunity_Close_Date__c from Task where WhatId in :oppstoupdate]){
   tsk.Opportunity_Close_Date__c = oList[0].CloseDate;
   taskstoupdate.add(tsk);
            }
        }
             if(!taskstoupdate.isEmpty()){
            update taskstoupdate;
        }
        }
        }
        }
        }
        }
        }
        }

 

And here's the test class:

 

@isTest

private class Test_OppCloseDate {
  
    public static testMethod void myUnitTest(){

 List<Opportunity> opp1 = new List<Opportunity>{ new Opportunity(
        Name = 'Test Opp1',
        StageName = 'Closed Won',
        Type = 'New Business',
        CloseDate = Date.today()+7,
        Demo_ID__c = NULL,
        LicenseType__c = 'Enterprise')};                                
       
    insert opp1;
 
List<Opportunity> opp2 = new List<Opportunity>{ new Opportunity(
        Name = 'Test Opp2',
        StageName = 'Closed Won',
        Type = 'New Business',
        CloseDate = Date.today()+2,
        LicenseType__c = 'Enterprise')};                                
       
    insert opp2;

List<Task> tsk1 = new List<Task>{ new Task(
    WhatId = opp1[0].id,
    Subject = 'Task 2',
    Status = 'Not Started',
    Priority = 'Normal',
    Type = 'Demonstration - In Person')};                                
        
    insert tsk1;
    
  
List<Task> tsk2 = new List<Task>{ new Task(
    WhatId = NULL,
    Subject = 'Task 2',
    Status = 'Not Started',
    Priority = 'Normal',
    Type = 'Demonstration - Web/Phone')};                                
        
    insert tsk2;
    
List<Opportunity> oppstoupdate1 = New List<Opportunity>{ [select id from Opportunity where id in :opp1]};
    for(Opportunity oOP:oppstoupdate1)
    oOP.CloseDate = Date.today();
        Update oppstoupdate1;

List<Task> taskstoupdate2 = New List<Task>{ [select id from task where id in :tsk2]};
    for(task tOK:taskstoupdate2)
    tOK.WhatId = opp2[0].id;
        Update taskstoupdate2;


Task [] tasks1 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk1];
    System.assert(tasks1[0].Opportunity_Close_Date__c == (opp1[0].CloseDate));

Task [] tasks2 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk2];
    System.assert(tasks2[0].Opportunity_Close_Date__c == (opp2[0].CloseDate));  
 }
 }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Team WorksTeam Works
See below what u were missing ----:
Dont forget to click Kudos and mark it as solution if this solves the issue 

@isTest

private class Test_OppCloseDate {
  
    public static testMethod void myUnitTest(){

 List<Opportunity> opp1 = new List<Opportunity>{ new Opportunity(
        Name = 'Test Opp1',
        StageName = 'Closed Won',
        Type = 'New Business',
        CloseDate = Date.today()+7
        )};                                
     ////Demo_ID__c = NULL,
        //LicenseType__c = 'Enterprise'  
    insert opp1;
    system.debug('The opp1 created is as >>>>'+ opp1);
 
List<Opportunity> opp2 = new List<Opportunity>{ new Opportunity(
        Name = 'Test Opp2',
        StageName = 'Closed Won',
        Type = 'New Business',
        CloseDate = Date.today()+2
        )};                                
      
    insert opp2;
    system.debug('The opp2 created is as >>>>'+ opp2);
    
List<Task> tsk1 = new List<Task>{ new Task(
    WhatId = opp1[0].id,
    Subject = 'Task 2',
    Status = 'Not Started',
    Priority = 'Normal',
    Type = 'Demonstration - In Person')};                                
        
    insert tsk1;
       system.debug('The tsk1 created is as >>>> '+ tsk1); 
  //why task.WhatId=Null
List<Task> tsk2 = new List<Task>{ new Task(
    WhatId = opp2[0].id,
    Subject = 'Task 2',
    Status = 'Not Started',
    Priority = 'Normal',
    Type = 'Demonstration - Web/Phone')};                                
        
    insert tsk2;
    system.debug('The tsk2 created is as >>>> '+ tsk2); 

List<Opportunity> oppstoupdate11 = New List<Opportunity>();
List<Task> taskstoupdate11 = New List<Task>();
List<Opportunity> oppstoupdate22 = New List<Opportunity>();
List<Task> taskstoupdate22 = New List<Task>();

List<Opportunity> oppstoupdate1 = New List<Opportunity>{ [select id,CloseDate from Opportunity where id in :opp1]};

    system.debug('The List before update oppstoupdate1 created is as  >>>> '+ oppstoupdate1); 
    for(Opportunity oOP:oppstoupdate1){
       oOP.CloseDate = Date.today();
       oppstoupdate11.add(oOP);}
        Update oppstoupdate11;
        system.debug('The List after update oppstoupdate1 created is as  >>>> '+ oppstoupdate11); 


List<Task> taskstoupdate1 = New List<Task>{ [select id,Opportunity_Close_Date__c from task where id in :tsk1]};
    for(task tOK:taskstoupdate1){
      tOK.WhatId = opp1[0].id;
      taskstoupdate11.add(tOK);}
      Update taskstoupdate11;
system.debug('The List after update task1 created is as  >>>> '+ taskstoupdate11);

List<Opportunity> oppstoupdate2 = New List<Opportunity>{ [select id,CloseDate from Opportunity where id in :opp2]};
    system.debug('The List before update oppstoupdate2 created is as  >>>> '+ oppstoupdate2); 
    for(Opportunity oOP:oppstoupdate2){
       oOP.CloseDate = Date.today();
       oppstoupdate22.add(oOP);}
        Update oppstoupdate22;
        system.debug('The List after update oppstoupdate2 created is as  >>>> '+ oppstoupdate22);         

List<Task> taskstoupdate2 = New List<Task>{ [select id,Opportunity_Close_Date__c from task where id in :tsk2]};
    system.debug('The Task t2 has a null what Id when created '+taskstoupdate2);
    for(task tOK:taskstoupdate2){
       tOK.WhatId = opp2[0].id;
        system.debug('The Value of opp2[0].id >>>>>>>>>> '+ opp2[0].id);       
        taskstoupdate22.add(tOK);}
        Update taskstoupdate22;
system.debug('The List after update task2 created is as  >>>> '+ taskstoupdate22);

Task [] tasks1 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk1];
Opportunity[] o1 =[select Id,CloseDate from Opportunity where Id IN :opp1];
system.debug(o1[0].CloseDate+' <<<<<This is the tsk1 test class>>>>  '+ tasks1[0].Opportunity_Close_Date__c);
    System.assert(tasks1[0].Opportunity_Close_Date__c == (o1[0].CloseDate));

Task [] tasks2 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk2];
Opportunity[] o2 =[select Id,CloseDate from Opportunity where Id IN :opp2];
system.debug(o2[0].CloseDate+' <<<<<This is the test class tsk2>>>>  '+ tasks2[0].Opportunity_Close_Date__c);
    System.assert(tasks2[0].Opportunity_Close_Date__c == (o2[0].CloseDate));  
 }
 }

 

All Answers

levaleva

Looks like the Opportunity_Close_Date__c  and ClosedDate do not match up. add system.debug before the tasks are saved, take a look at the acutal values

Team WorksTeam Works
See below what u were missing ----:
Dont forget to click Kudos and mark it as solution if this solves the issue 

@isTest

private class Test_OppCloseDate {
  
    public static testMethod void myUnitTest(){

 List<Opportunity> opp1 = new List<Opportunity>{ new Opportunity(
        Name = 'Test Opp1',
        StageName = 'Closed Won',
        Type = 'New Business',
        CloseDate = Date.today()+7
        )};                                
     ////Demo_ID__c = NULL,
        //LicenseType__c = 'Enterprise'  
    insert opp1;
    system.debug('The opp1 created is as >>>>'+ opp1);
 
List<Opportunity> opp2 = new List<Opportunity>{ new Opportunity(
        Name = 'Test Opp2',
        StageName = 'Closed Won',
        Type = 'New Business',
        CloseDate = Date.today()+2
        )};                                
      
    insert opp2;
    system.debug('The opp2 created is as >>>>'+ opp2);
    
List<Task> tsk1 = new List<Task>{ new Task(
    WhatId = opp1[0].id,
    Subject = 'Task 2',
    Status = 'Not Started',
    Priority = 'Normal',
    Type = 'Demonstration - In Person')};                                
        
    insert tsk1;
       system.debug('The tsk1 created is as >>>> '+ tsk1); 
  //why task.WhatId=Null
List<Task> tsk2 = new List<Task>{ new Task(
    WhatId = opp2[0].id,
    Subject = 'Task 2',
    Status = 'Not Started',
    Priority = 'Normal',
    Type = 'Demonstration - Web/Phone')};                                
        
    insert tsk2;
    system.debug('The tsk2 created is as >>>> '+ tsk2); 

List<Opportunity> oppstoupdate11 = New List<Opportunity>();
List<Task> taskstoupdate11 = New List<Task>();
List<Opportunity> oppstoupdate22 = New List<Opportunity>();
List<Task> taskstoupdate22 = New List<Task>();

List<Opportunity> oppstoupdate1 = New List<Opportunity>{ [select id,CloseDate from Opportunity where id in :opp1]};

    system.debug('The List before update oppstoupdate1 created is as  >>>> '+ oppstoupdate1); 
    for(Opportunity oOP:oppstoupdate1){
       oOP.CloseDate = Date.today();
       oppstoupdate11.add(oOP);}
        Update oppstoupdate11;
        system.debug('The List after update oppstoupdate1 created is as  >>>> '+ oppstoupdate11); 


List<Task> taskstoupdate1 = New List<Task>{ [select id,Opportunity_Close_Date__c from task where id in :tsk1]};
    for(task tOK:taskstoupdate1){
      tOK.WhatId = opp1[0].id;
      taskstoupdate11.add(tOK);}
      Update taskstoupdate11;
system.debug('The List after update task1 created is as  >>>> '+ taskstoupdate11);

List<Opportunity> oppstoupdate2 = New List<Opportunity>{ [select id,CloseDate from Opportunity where id in :opp2]};
    system.debug('The List before update oppstoupdate2 created is as  >>>> '+ oppstoupdate2); 
    for(Opportunity oOP:oppstoupdate2){
       oOP.CloseDate = Date.today();
       oppstoupdate22.add(oOP);}
        Update oppstoupdate22;
        system.debug('The List after update oppstoupdate2 created is as  >>>> '+ oppstoupdate22);         

List<Task> taskstoupdate2 = New List<Task>{ [select id,Opportunity_Close_Date__c from task where id in :tsk2]};
    system.debug('The Task t2 has a null what Id when created '+taskstoupdate2);
    for(task tOK:taskstoupdate2){
       tOK.WhatId = opp2[0].id;
        system.debug('The Value of opp2[0].id >>>>>>>>>> '+ opp2[0].id);       
        taskstoupdate22.add(tOK);}
        Update taskstoupdate22;
system.debug('The List after update task2 created is as  >>>> '+ taskstoupdate22);

Task [] tasks1 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk1];
Opportunity[] o1 =[select Id,CloseDate from Opportunity where Id IN :opp1];
system.debug(o1[0].CloseDate+' <<<<<This is the tsk1 test class>>>>  '+ tasks1[0].Opportunity_Close_Date__c);
    System.assert(tasks1[0].Opportunity_Close_Date__c == (o1[0].CloseDate));

Task [] tasks2 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk2];
Opportunity[] o2 =[select Id,CloseDate from Opportunity where Id IN :opp2];
system.debug(o2[0].CloseDate+' <<<<<This is the test class tsk2>>>>  '+ tasks2[0].Opportunity_Close_Date__c);
    System.assert(tasks2[0].Opportunity_Close_Date__c == (o2[0].CloseDate));  
 }
 }

 

This was selected as the best answer
pooja@magichorse.compooja@magichorse.com

Try this...

 

Task [] tasks1 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk1];
    System.assertEquals(tasks1[0].Opportunity_Close_Date__c == (opp1[0].CloseDate), true); //true or false based on your desired result

Task [] tasks2 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk2];
    System.assertEquals(tasks2[0].Opportunity_Close_Date__c == (opp2[0].CloseDate), true); //true or false based on your desired result

 

Season1224Season1224

Thanks so much for your help, namo!

I thought there was a problem with my actual trigger and it turns out I didn't have the class set up correctly.

 

Such a huge help!!!!