2

Thank you for all the help on my other question. I was so happy to see a community that doesn't have trolls.

So the good news is that the code compiles with no errors.

The bad news is that I have no code coverage and the trigger will not fire. I have moved the code over to my sandbox so I don't spill anything in production.

As stated in my previous question, this is my first Apex Project and the first time I have done any serious coding in (I'm embarassed to say) nearly 20 years.

Here is the updated code:

@isTest(SeeAllData=true)
public with sharing class FirmwareClass{

public void createCases(List<Customer_Asset__c> assets){
List<Case> casesToCreate = new List<Case>();

for(Customer_Asset__c acc:assets){
 if (acc.Firmware_Update_Available__c == TRUE){
     Case caseToAdd = new Case();
     caseToAdd.AccountId = acc.Account__c;
     caseToAdd.Subject = 'Software Upgrade Available';
     casesToCreate.add(caseToAdd);
        }       
  } 
  if (casesToCreate.size() > 0)
  insert casesToCreate;
   }
}

And the Trigger:

trigger FirmwareTrigger on Customer_Asset__c (after update) {
  System.debug('@@@ trgInsertNote');
  FirmwareClass helper = new FirmwareClass();

 helper.createCases(Trigger.new);
}

The logic seems to work, I just can't get the trigger to fire. There are no validation rules in place to stop it. The only thing that seems to be stopping it from working is code coverage.

2
  • Is the trigger active? Setup | Apex Triggers and verify the status is Active.
    – Mike Chale
    Jun 21, 2013 at 14:21
  • Hi Mike, Yes the trigger is active. Jun 21, 2013 at 14:50

2 Answers 2

2

Take off @isTest from your FirmwareClass - you only should put @isTest on test classes. Create a new class for your tests. Here's a skeleton for you to fill in:

@isTest
private class FirmwareClassTest {
    private static testMethod void testCreateCases() {
        //create a new instance of a Customer_Asset__c to use in the test
        List<Customer_Asset__c> testAssets = new List<Customer_Asset__c> {};
        Customer_Asset__c ta = new Customer_Asset__C();
        ta.Firmware_Update_Available__c = True;
        //add additional required fields to the ta (you may need to create an account and other supporting records)
        testAssets.add(ta);

        FirmwareClass helper = new FirmwareClass();
        Test.startTest();
        helper.createCases(testAssets);
        Test.stopTest();

        // query for cases to see if some were created and do an assertion
    }

}

I recently wrote a blog post on how to write tests for a trigger.

Also, your trigger only fires on After Update, so it won't work for Inserts. Maybe that is part of your problem?

1
  • Good point on having a separate test class, which is now required in Summer '13.
    – Mike Chale
    Jun 21, 2013 at 17:15
2

Regarding code coverage, there are a few problems:

  1. If createCases() is your test method, you have not marked it as a test method. Test methods should be marked as static testMethod in their signature. Example: private static testMethod createCases()

  2. Your trigger is set to only run AFTER UPDATE on Customer_Asset__c and createCases() only inserts new Cases. Unless you update a Customer_Asset__c elsewhere, there is nothing in your test method that would fire the trigger.

1
  • 2
    Also, be weary of using (SeeAllData=true). Best practice is to create all test data you need for that particular test. If your test relies on your org's current data, and that data is deleted, your test code will fail causing headaches (like not being able to deploy changesets). And if it's complicated to create test data (like it often is when validation rules are in play), you can create code that creates testObjects for you (so you only write the code once) and then you can use it again and again in your test code Jun 21, 2013 at 16:53

You must log in to answer this question.

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