You need to sign in to do that
Don't have an account?

need help - test class
Hi all,
I need help on test class, this is my class:
When creating a task of type (Phone Call or Last Face to Face) from contact,need to update the account fields
I have a trigger:
trigger TaskTrigger on Task (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
TaskTriggerHandler handler = new TaskTriggerHandler (Trigger.isExecuting, Trigger.size);
//After Update
if(Trigger.isUpdate && Trigger.isAfter){
handler.OnAfterUpdate(Trigger.new,Trigger.oldMap);
}
}
TriggerHandler class:
public with sharing class TaskTriggerHandler {
private boolean trigger_isExecuting = false;
private integer batchSize = 0;
public TaskTriggerHandler ( boolean isExecuting, integer size){
trigger_isExecuting = isExecuting;
batchSize = size;
}
public void OnAfterUpdate(List<Task> Tklist, Map<id,Task> OldMaplst){
//EXECUTE AFTER UPDATE LOGIC
//first condition is to check the record type then
Set<ID> tid = new Set<ID>();
list<Account> acclst = new list<Account>();
for(Task tk:Tklist){
if(tk.Whoid!=null && tk.WhoId.getSObjectType().getDescribe().getName()=='Contact'){
tid.add(tk.whoid);
}
}
List<Contact> con = new List<Contact>();
con=[select id,accountid from Contact where id =:tid];
Map<id,account>Lstacc=new Map<id,account>();
if(!con.isEmpty()){
account acc;
for(Task tk:[select id,LastModifiedBy.name,type,LastModifiedDate from task where id in :Tklist]){
DateTime dt = tk.LastModifiedDate;
Date myDate = date.newinstance(dt.year(), dt.month(), dt.day());
acc=new account(id=con[0].accountid);
if(tk.type=='Face 2 Face Meeting'){
acc.Last_Face_to_Face_Activity__c= myDate;
acc.Last_Visited_By__c =tk.LastModifiedBy.name;
}
else if(tk.type=='Phone Call'){
acc.Last_Call_Activity__c = myDate;
}
Lstacc.put(acc.id,acc);
}
if(!Lstacc.isEmpty())
Update Lstacc.values();
}
}
}
I need help on test class, this is my class:
When creating a task of type (Phone Call or Last Face to Face) from contact,need to update the account fields
I have a trigger:
trigger TaskTrigger on Task (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
TaskTriggerHandler handler = new TaskTriggerHandler (Trigger.isExecuting, Trigger.size);
//After Update
if(Trigger.isUpdate && Trigger.isAfter){
handler.OnAfterUpdate(Trigger.new,Trigger.oldMap);
}
}
TriggerHandler class:
public with sharing class TaskTriggerHandler {
private boolean trigger_isExecuting = false;
private integer batchSize = 0;
public TaskTriggerHandler ( boolean isExecuting, integer size){
trigger_isExecuting = isExecuting;
batchSize = size;
}
public void OnAfterUpdate(List<Task> Tklist, Map<id,Task> OldMaplst){
//EXECUTE AFTER UPDATE LOGIC
//first condition is to check the record type then
Set<ID> tid = new Set<ID>();
list<Account> acclst = new list<Account>();
for(Task tk:Tklist){
if(tk.Whoid!=null && tk.WhoId.getSObjectType().getDescribe().getName()=='Contact'){
tid.add(tk.whoid);
}
}
List<Contact> con = new List<Contact>();
con=[select id,accountid from Contact where id =:tid];
Map<id,account>Lstacc=new Map<id,account>();
if(!con.isEmpty()){
account acc;
for(Task tk:[select id,LastModifiedBy.name,type,LastModifiedDate from task where id in :Tklist]){
DateTime dt = tk.LastModifiedDate;
Date myDate = date.newinstance(dt.year(), dt.month(), dt.day());
acc=new account(id=con[0].accountid);
if(tk.type=='Face 2 Face Meeting'){
acc.Last_Face_to_Face_Activity__c= myDate;
acc.Last_Visited_By__c =tk.LastModifiedBy.name;
}
else if(tk.type=='Phone Call'){
acc.Last_Call_Activity__c = myDate;
}
Lstacc.put(acc.id,acc);
}
if(!Lstacc.isEmpty())
Update Lstacc.values();
}
}
}
All Answers
You may want to put try....catch in your code
For test class/method, one of option is
- create a input file with multiple records of task and upload to static resource.
- insert record from static resource and it will fire your trigger and execute other apex class
- create another input fiel with account details and upload to static resource
- compare the list from 3rd step and query for records of account and decide fail/success.
Give it a try and intimate if you were able to progress....
caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Class.TaskTriggerHandler.OnAfterUpdate: line 59, column 1
Trigger.TaskTrigger: line 26, column 1: []
I'm getting the above error
please check in code
for(Task tk:[select id,LastModifiedBy.name,type,LastModifiedDate from task where id in :Tklist]){
DateTime dt = tk.LastModifiedDate;
Date myDate = date.newinstance(dt.year(), dt.month(), dt.day());
// acc=new account(id=con[0].accountid);
if(tk.type=='Face 2 Face Meeting'){
acc.Id = con[0].accountid; //updated HERE
acc.Last_Face_to_Face_Activity__c= myDate;
acc.Last_Visited_By__c =tk.LastModifiedBy.name;
}
else if(tk.type=='Phone Call'){
acc.Id = con[0].accountid; //updated HERE
acc.Last_Call_Activity__c = myDate;
}
Lstacc.put(acc.id,acc);
}
if(!Lstacc.isEmpty())
Update Lstacc.values();
}
But I'm getting a different error:
caused by: System.NullPointerException: Attempt to de-reference a null object
whatid is account id and whoid is contact id..is this causing an issue?
List<Task> tasks = new List<Task>{};
for(Integer i = 0; i < 5; i++)
{
Task t = new Task(Subject='Donni'+i,Status='New',Priority='Normal',Type='Face 2 Face Meeting' ,whatid = a.Id, Whoid = c.Id );
tasks.add(t);
}