Hey guys I have been struggling a lot trying to compare two times. My objective is to check if a Case created by a Contact is assigned to a Task with subject 'Call' if the Task was created by the same Contact in a period of time less than 2 minutes before the new Case. Something like this: IF Case.CreatedDate <= Task.CreatedDate.addMinutes(2) AND Task.Subject == 'Call'
AND Task.WhoId == Case.OwnerId == Contact.Id THEN Task.WhatId = Case.Id Does anyone have any clue on how to make a test for this and also some recommendations on how the trigger approach should be?
This is what I have so far and it is giving me null on both taskCreationDateTime and caseCreationDateTime
public static void taskAssignmentToCase() {
Contact newContact = new Contact(LastName='Doe', FirstName='Joe');
insert newContact;
DateTime aDate = Datetime.newInstance(2021, 10, 11, 10, 0, 0);
Task newTask = new Task(Subject='Call', WhoId=newContact.Id, Priority='Normal');
insert newTask;
Case newCase = new Case(Origin='Phone', OwnerId=newContact.Id, Status='New');
insert newCase;
Datetime taskCreationDateTime = newTask.CreatedDate;
Datetime caseCreationDateTime = newCase.CreatedDate;
System.debug(taskCreationDateTime);
System.debug(caseCreationDateTime);
Date caseCreationDate = caseCreationDateTime.date();
Date taskCreationDate = taskCreationDateTime.date();
System.assertEquals(true, caseCreationDate.isSameDay(taskCreationDate), 'Date of creation of Case and Task is not the same day');
System.assertEquals(true, caseCreationDateTime.time() < taskCreationDateTime.time().addMinutes(2) && caseCreationDateTime.time() > taskCreationDateTime.time(), 'Case creation is not between 2 minutes from Task creation');
System.assertEquals('Call', newTask.Subject, 'Task was not a call');
System.assertEquals(true, newTask.WhoId==newCase.Id,'The task was nos realted succesfully to the new case created with a difference of less than 2 minutes');
}
Hi,
Sorry for the inconvenience. In case you don't receive a response here, may I also suggest joining the group below to collaborate with the experts for best practice and advice.
developer.salesforce.com/forums#!/feedtype=RECENT&criteria=ALLQUESTIONS&
Hope this helps.
Regards,