Can any one help on this .I have a trigger written on Case object for the duplication to get it closed .I have written a Test class where it gives the code coverage as 70% only. When i Execute in Developer Console it throws an Error as :

Line: 3, Column: 24
Only top-level class methods can be declared static


trigger Case_DuplicateClose on Case (before insert) {
    Map<String, Case> newCaseMap = new Map<String, Case>();
    for(Case c : trigger.new)
        while(c.Subject.startsWith('RE:') || c.Subject.startsWith('Re:') || c.Subject.startsWith('FW:') || c.Subject.startsWith('Fw:'))
            c.Subject = c.Subject.substring(3).trim();
            c.Status = 'Closed';
            newCaseMap.put(c.Subject, c);
    for(Case c : [Select Subject From Case Where Subject in :newCaseMap.keySet()])
        newCaseMap.get(c.Subject).status = 'Closed';

Test Class

private class TestCase_DuplicateClose{ 
    static testmethod void testcaseDuplicate(){ 
        list <case>cases = new list<case>{};
        for(integer i=0;i<200;i++){
            case c=new case(Subject='RE:'+i);
        insert cases;
        List<case> insertedcases =[Select Subject,status from case where id in:cases];
        for(case c :insertedcases){

Any help very much appreciated.

1 Answer 1


First - you can't run a test class as a normal class in Execute Anonymous - that's why you are getting that error. You can run that single test in the developer consoler or in Sublime text (or Eclipse) if you want just the stats for that class.

If you want to find the specific lines that aren't getting executed, you'd be best to open the project in SublimeText and run the unit test from there:

Test Runner UI Pic

From here you can examine the code coverage line by line as shown below: Code coverage pic

Also, I think you may need to modify your trigger. I think you don't need a while in the code here:

while(c.Subject.startsWith('RE:') || c.Subject.startsWith('Re:') || c.Subject.startsWith('FW:') || c.Subject.startsWith('Fw:'))
    c.Subject = c.Subject.substring(3).trim();

While I'm not totally certain of what you are trying to do, I think this would work better:

if(c.Subject.startsWith('RE:') || c.Subject.startsWith('Re:') || c.Subject.startsWith('FW:') || c.Subject.startsWith('Fw:')){
    c.Subject = c.Subject.substring(3).trim();

Your final line, I'm guessing is to find all the other Cases that weren't part of this trigger and close them if they have a matching subject - correct?

You will need to update all the cases in this second query list that have changed status - they are not covered by the implicit update that you get in a before insert trigger.

Also, this last line won't get executed - in your test code, this find won't find anything. Your else clause won't get executed either - you'll need some Cases that have a different subject line and also some preexisting Cases with the same subject line to make sure these parts of the code get run.

Sorry for the rambling answer - there didn't seem to be a definitive single answer here, just a bunch of small things.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .