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
Ubisense SupportUbisense Support 

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?

 

souvik9086souvik9086

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

ryanjuptonryanjupton

QUEUE_UNKNOWN_COMPLETE__c is a formula field. That won't work. He'll get an error trying to save the test class.

Ubisense SupportUbisense Support

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?

ryanjuptonryanjupton

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.

Ubisense SupportUbisense Support

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?

ryanjuptonryanjupton

Any reason you're doing this in a trigger and not a validation rule?

Ubisense SupportUbisense Support
Well I am new to SF so I guess I can look into validation, don't know anything about it.
Ubisense SupportUbisense Support

So how can you delete a case via validation?