1

As a followup to my previous posts (Again, many many thanks) I am still having trouble with the trigger.

The code seems to function and I created a test class and the code itself is 100% covered.

The Trigger will not work, I am getting 0% coverage and I am not sure what I am doing wrong.

Trigger:

trigger FirmwareTrigger on Customer_Asset__c ( after insert) {        

 List<Customer_Asset__c> listfirm = new list<Customer_Asset__c>();
      for (Customer_Asset__c ta: trigger.new)
 {

List <Customer_Asset__c> firmup = [SELECT Id FROM Customer_Asset__c WHERE Firmware_Update_Available__c = TRUE];

   FirmwareClass helper = new FirmwareClass();
   for (Customer_Asset__c az: Trigger.new);
    helper.createCases(Trigger.new);
  }
}
} 

Test Class

@isTest (seeAlldata=true)
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);
    insert testAssets;
    update testAssets;
    FirmwareClass helper = new FirmwareClass();
    Test.startTest();
    helper.createCases(testAssets);
    Test.stopTest();

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

//do some updating that you want to test

}

Class

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;
}

}

I tried to get fancy and added (what I hoped / thought) would be some sort of validation within the trigger.

Updated Trigger

trigger FirmwareTrigger on Customer_Asset__c (before update) {

 List<FirmwareUpdate> listfirm = new list<FirmwareUpdate>();
      for (Customer_Asset__c ta: trigger.new)
 {
  FirmwareUpdate firmup = [SELECT Firmware_Update_Available__c FROM Customer_Asset__c];
if (firmup == 1)
{
       FirmwareClass helper = new FirmwareClass();
       for (Customer_Asset__c az: Trigger.new);
        helper.createCases(Trigger.new);
      }
}
}

I receive a compile error with the New Trigger:

Error: Compile Error: Invalid type: FirmwareUpdate at line 3 column 47

The old trigger compiles but is showing a 0 code coverage. At this point I am stuck. I believe my class works, but I can't get past this point.

4

your trigger fires on before update

trigger FirmwareTrigger on Customer_Asset__c (before update)

yet your test class just INSERTS records, call update on the testAssets list in FirmwareClassTest to fire the trigger

btw. your trigger creates a case every time a asset gets updated no matter if Firmware_Update_Available__c is changed to true or not

here's updated code

TRIGGER

trigger FirmwareTrigger on Customer_Asset__c (before update) {

  // hold assets for which cases need to be created
  List<Customer_Asset__c> fwUpdates = new List<Customer_Asset__c>();

  for (Customer_Asset__c record:trigger.new)
  {
     // make sure we only include assets for which Firmware_Update_Available__c has been changed to true
     if (record.Firmware_Update_Available__c &&
        !trigger.oldMap.get(record.Id).Firmware_Update_Available__c){

        fwUpdates.add(record);
     }        
  }

  // call FWClass only if there are any assets for which cases need to be created
  if (fwUpdates.size() > 0){
     FirmwareClass helper = new FirmwareClass();
     helper.createCases(fwUpdates);
  }
}

trigger FirmwareTrigger on Customer_Asset__c (before update) {

List listfirm = new list(); for (Customer_Asset__c ta: trigger.new) { FirmwareUpdate firmup = [SELECT Firmware_Update_Available__c FROM Customer_Asset__c]; if (firmup == 1) { FirmwareClass helper = new FirmwareClass(); for (Customer_Asset__c az: Trigger.new); helper.createCases(Trigger.new); } } }

TEST CLASS

@isTest
private class FirmwareClassTest {

   static List<Customer_Asset__c> getTestAssets(){
    List<Customer_Asset__c> testAssets = new List<Customer_Asset__c> {};
    Customer_Asset__c ta = new Customer_Asset__C();
    ta.Firmware_Update_Available__c = True;
    testAssets.add(ta);
    return testAssets;
   }

   // test FirmwareClass.createCases
   private static testMethod void testCreateCases() {

    List<Customer_Asset__c> testAssets = getTestAssets();

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

   // test FirmwareTrigger.trigger
   private static testMethod void testFirmwareTrigger() {

    // insert the asserts with update available false
    List<Customer_Asset__c> testAssets = getTestAssets();
    testAssets[0].Firmware_Update_Available__c = false;
    insert testAssets;

    Test.startTest();
    // now set update available to true
    testAssets[0].Firmware_Update_Available__c = true;
    // that should cause the trigger to create cases
    update testAssets;
    Test.stopTest();
   }
}
  • Thanks Seb__Wagner, I see what you are talking about but I am not sure how to fix it. Any changes I try to make seem to give me compile errors. – Bill Pomeroy Jun 25 '13 at 14:24
  • @BillPomeroy included revised code in my answer, that should do the trick. btw. you get the compile error because there's no class/object called FirmwareUpdate, and you try to assign a list of type Customer_Asset__c from a SOQL query to list of type FirmwareUpdate – Seb Wagner Jun 25 '13 at 18:21
  • You did it!!! Works like a champ!!!!! Your the best! – Bill Pomeroy Jun 25 '13 at 18:28
1
  • Your trigger is an update trigger (before update, after update) it fires on an update to the Customer_Asset__c object.

  • Your test Class does not create or Update and Customer_Asset__c records.

  • You need to add an insert and then an update of Customer_Asset__c records within the test class.

    insert testAssets;
    //do some updating that you want to test
    update testAssets;

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.