+ Start a Discussion
uHaveOptionsuHaveOptions 

Test Class... Trying to make it work.

Hello All,

I'm trying to create a test call on Apex Trigger.  Kindly help on this test class.

APEX Trigger:
trigger CallDate on Task (after insert, after update, after delete) 
{
    Set<Id> con_set = new Set<Id>();
    List<Contact> con_list = new List<Contact>();
    for( Task T: Trigger.new )
    {
        if(String.valueof(T.whoid).startsWith('003') && T.Status=='Completed' && T.Subject=='Call' )
        {
            con_set.add(T.whoid);
        }
    }
     
     for(AggregateResult aggregateResult:[SELECT max(createdDate)MaxCDate,whoid FROM Task WHERE whoid IN: con_set AND Status ='Completed' AND Subject ='Call' Group By whoid])
     {
        con_list.add(new Contact(Id=(id)aggregateResult.get('whoid'),Last_Call__c=date.valueof(aggregateResult.get('MaxCDate'))));
     }
     
    try
    {
     
         if(con_list !=null && con_list.size()>0)
         {
             update con_list;
         }
     
    }Catch(Exception e){
         system.debug('Exception ***'+e.getMessage());
      
     }

}


TEST CLASS:
@IsTest()
public class CallDateTest
{
  static testMethod void MyUnitTest()
  {
  
       list<id> con_set = new list<Id>();
       List<contact> con_list = new List<contact>();
       for( Task T: Trigger.new )
       insert con_set;
       
       Task t = new Task(Priority = 'Normal', whoId = task.id, Subject = 'Call', Status = 'Not Started', Call_Result__c = 'Reached');
       insert t;       
       
       try
       {
       insert t;

       }catch (Exception ee)
       {
       }
       
       
}
}


I'm getting a *"Compile Error: DML requires SObject or SObject list type: List<Id> at line 10 column 8"*   Where am I doing this incorrectly?

Thanks 
Best Answer chosen by uHaveOptions
RAM AnisettiRAM Anisetti
update your code like this

 
@IsTest()
public class CallDateTest
{
  static testMethod void MyUnitTest()
  {
  
 Contact c=new Contact();
 c.lastname='ram';
 insert c;

 Task t = new Task(Priority = 'Normal', whoId = c.id, Subject = 'Call', Status = 'Completed', Call_Result__c = 'Reached');
       
       try
       {
       
        insert t;

       }catch (Exception ee)
       {
       }
       
       
}
}

 

All Answers

Chandra Sekhar CH N VChandra Sekhar CH N V
You cannot insert ID type in a DML operation. Only sObject and list<sObject> are allowed in DML.
RAM AnisettiRAM Anisetti
update your code like this

 
@IsTest()
public class CallDateTest
{
  static testMethod void MyUnitTest()
  {
  
 Contact c=new Contact();
 c.lastname='ram';
 insert c;

 Task t = new Task(Priority = 'Normal', whoId = c.id, Subject = 'Call', Status = 'Completed', Call_Result__c = 'Reached');
       
       try
       {
       
        insert t;

       }catch (Exception ee)
       {
       }
       
       
}
}

 
This was selected as the best answer