You need to sign in to do that
Don't have an account?
Apex Trigger Help
We get a ton of spam cases coming into SF so we created a formula field that evaluates to TRUE when the case is 7 days old and the Owner of the case is set to a specific Queue which happens to be the spam queue.
I want to trigger off of this formula field and delete the case. Thus far I have:
TRIGGER_____________________________________________________
trigger DeleteUnknownCaseTrigger on Case (after update, after insert) {
List<Case> lstCaseToDelete = new List<Case>();
for(Case c : trigger.new) {
if(c.QUEUE_UNKNOWN_DELETE__c == TRUE){
Case cDel = new Case(Id=c.Id);
lstCaseToDelete.add(cDel);
}
}
delete lstCaseToDelete;
_____________________________________________________________________________
Test Class
_____________________________________________________________________________
@isTest
private class UnknownCaseTriggerDeleteTestClass {
static testMethod void validateTrigger() {
Date myDate = date.newInstance(2013, 6, 1);
Case c = new Case(
OwnerId = '00G20000000ltb9',
Status = 'New',
Subject = 'Test Case',
Description = 'Case Description'
);
insert c;
c.Estimated_Completion_Date__c = myDate;
}
}
________________________________________________________________________________
The problem is that the Trigger is only 66% covered and it complains that code inside the if statement isn't covered. I am new to apex so I thought the by setting the date of Estimated Completion Date it would cause the formula to fire and thus test that loop. What am I doing wrong?
Hi,
Try this
Test Class
_____________________________________________________________________________
@isTest
private class UnknownCaseTriggerDeleteTestClass {
static testMethod void validateTrigger() {
Date myDate = date.newInstance(2013, 6, 1);
Case c = new Case(
OwnerId = '00G20000000ltb9',
c.QUEUE_UNKNOWN_DELETE__c = true,
Status = 'New',
Subject = 'Test Case',
Description = 'Case Description'
);
insert c;
c.Estimated_Completion_Date__c = myDate;
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
QUEUE_UNKNOWN_COMPLETE__c is a formula field. That won't work. He'll get an error trying to save the test class.
That is true, you can't do that. So I put in some debug statements and the completion date is getting set but I am not sure why the formula field isn't updating and thus triggering?
That's strange because I duplicated your code and mine ran fine. Could you please post the formula for QUEUE_UNKNOWN_DELETE__c. I suspect your testing something different than what's setting the formula field value. Also I think you have an unintended error in your trigger code.
IF( Owner:Queue.QueueName = 'Ubi Support: Queue UNKNOWN' && ( TODAY() - Estimated_Completion_Date__c > 5 ) , TRUE, FALSE)
So basically if the case is in the correct queue and the Today-Estimated_completion_date is greater then 5 then set the formula field to TRUE which should then be an update to the case and fire th trigger?
Any reason you're doing this in a trigger and not a validation rule?
So how can you delete a case via validation?