1

Im stuck, how can I test the Apex trigger frfing on case before update? Any help appreciated.

Im quite new into this universe so I bear in mind :-) Apex trigger:

trigger CaseEscalationTrigger on Case(before update) 
{
   for(Case c : Trigger.new)
   {   
       if (c.Escalate_to__c != NULL)
       {
           String GroupID = c.Escalate_to__c;
           c.OwnerId = [SELECT Id FROM Group where Type = 'Queue' And Name = :GroupID].id;
           c.Escalate_to__c = '';      
       }     
   }
}

Test Class:

@isTest
public class testEscalateToFunction
{
    public static testmethod void testAll()
    {
        // ContentVersion conv=new ContentVersion();      
        Case c = new Case();
        c.Escalate_to__c = '1. SAP';
        //update c;
        System.assertEquals('', c.Escalate_to__c);        
    }
}
1

You are on the right track but there are some details to refine:

// Tests are isolated from most org data include Group so need to create one
Group g = new Group(
        Name = '1. SAP',
        DeveloperName = '1. SAP' + System.currentTimeMillis(),
        Type = 'Queue'
        );
insert g;

Case c = new Case();
insert c;

// Can't update unless already inserted
c.Escalate_to__c = '1. SAP';
update c;

// The trigger operates in the database layer so you have to requery to see the result
Case actual = [select Escalate_to__c from Case where Id = :c.Id];
System.assertEquals('', actual.Escalate_to__c);

Once you have your test working you should refactor your trigger as it contains the anti-pattern of a query inside a loop. The general pattern is to collect the set of Escalate_to__c values in a Trigger.new loop first, then query the whole set of id values from Group (single query however many Case objects the trigger fires for) in a map, then loop over Trigger.new again using values from the map.

PS

The refactored trigger would look something like this:

trigger CaseEscalationTrigger on Case (before update) {
    Set<String> groupIds = new Set<String>();
    for (Case c : Trigger.new) {   
        if (c.Escalate_to__c != null) {
            groupIds.add(c.Escalate_to__c);
        }
    }
    if (groupIds.size() > 0) {
        Map<String, Id> groupNamesToIds = new Map<String, Id>();
        for (Group g : [
                select Id, Name
                from Group
                where Id in :groupIds
                ]) {
            groupNamesToIds.put(g.Name, g.Id);
        }
        for (Case c : Trigger.new) {   
            if (c.Escalate_to__c != null) {
                c.OwnerId = groupNamesToIds.get(c.Escalate_to__c);
                c.Escalate_to__c = '';
            }
        }
    }
}
| improve this answer | |
  • The test still doesn't work. When i choose run there is a red-cross almost instantly. Do I need to fill in required fields of the Case to test it? Is it better to retrieve a case already created, and modify the field Escalate_to__c, and how do I query to get a complete case, like Select *from Case where id=000111 ? – Claus Machholdt Apr 6 '14 at 16:24
  • Tests are like any other code: you have to work though compile errors and runtime errors until you get them working. Tests typically take as long or longer to write than the code you are testing. (Its an investment.) The best way to write tests is for them to work in any org and not rely on pre-existing data. So yes you will need to populate any required fields including relationships. (It is possible to query existing data in an org by removing "testmethod" and adding "@isTest(SeeAllData=true)" instead. But that is a bad habit to get into and leaves the tests very fragile and rather unclear.) – Keith C Apr 6 '14 at 16:47
  • Got it to work now. Thank you for giving me the direction. Though I haven't refactored the trigger. Would you care to show me with code, how you would do it? – Claus Machholdt Apr 6 '14 at 17:53
  • @ClausMachholdt I've added some (untested) code to the answer. – Keith C Apr 6 '14 at 19:28
  • Looks good and it worked well, witrh a minor change regarding variable names. Thankyou for your time and sharing of insights :-) – Claus Machholdt Apr 7 '14 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.