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
Patrick J WaplesPatrick J Waples 

Issues with migrating crucial APEX class between orgs!

To start...I'm not developer...so this is probably a silly question but...we've recently gone through a migration to a new SFDC org and I have a bit of code originally written 8 years ago and the test class that was written for it simply won't work.  But I don't know enough about writing test code to have the foggiest idea why! :(

It's a crucial part of our current billing process...so any help would be greatly appreciated!
/*
Description:
 Automatically email customer their invoice once it’s been approved.
 
Functional Logic (API names in brackets[]):
 Whenever a case is updated and the Send Invoice [Send_Invoice__c] flag is true
 send an email to the case contact [ContactId] including the case attachment as 
 an email attachment if and only if there is one and only one attachment of a size less than 5 MB attached to the case.
 
Updates:
12/14/09 - Send a user friendly error email to the case owner if invoice fails to get sent
02/14/08 - Reset send_invoice__c flag if email fails

*/
@isTest
private class InvoiceMailerTest {

  // ensure no errors occur when sending a invoice correctly and that
  // send invoice flag remains checked
    static testMethod void testInvoiceMailer() {
    
    // create test case
    Case testCase = getTestCase();
    insert testCase;
    
    // add one attachment to simulate adding an invoice
    Attachment testAttachment = getTestAttachment(testCase);
    insert testAttachment;
    
    // mimic invoice approval process by checking send invoice
    System.Test.startTest();
    testCase.Send_Invoice__c = true;
    update testCase;
    System.Test.stopTest();
    
    // assert that send invoice is still checked as any errors will uncheck it
    testCase = [select send_invoice__c from Case where id = :testCase.id];
    system.assert(testCase.send_invoice__c == true, 
      'The invoice mailer appears to be failing when a case has a valid contact and attachment.');
    
    }
    
    // ensure no exceptions are thrown if case has no contact id
    // and that the send invoice flag is reset
    static testMethod void testInvoiceMailerErrorNoContactId() {
      // create test case and zero out contact id field
      Case testCase = getTestCase();
      testCase.contactId = null;
      insert testCase;
      
      // add one attachment to simulate adding an invoice
    Attachment testAttachment = getTestAttachment(testCase);
    insert testAttachment;
    
    // mimic invoice approval process by checking send invoice
    System.Test.startTest();
    testCase.Send_Invoice__c = true;
    update testCase;
    System.Test.stopTest();
    
    // assert that send invoice is not checked as no contact id should generate an
    // error email and uncheck the send invoice flag
    testCase = [select send_invoice__c from Case where id = :testCase.id];
    system.assert(testCase.send_invoice__c == false, 
      'The invoice mailer appears to have succeeded even though the test case had no associated contact.');  
    }
    
    // ensure no exceptions are thrown if case has no attachments
    // and that the send invoice flag is reset
    static testMethod void testInvoiceMailerErrorNoAttachment() {
      // create test case
    Case testCase = getTestCase();
    insert testCase;
    
    // don't add any attachments
    
    // mimic invoice approval process by checking send invoice
    System.Test.startTest();
    testCase.Send_Invoice__c = true;
    update testCase;
    System.Test.stopTest();
    
    // assert that send invoice is not checked as a case with no attachments
    // should generate an error email and uncheck the send invoice flag
    testCase = [select send_invoice__c from Case where id = :testCase.id];
    system.assert(testCase.send_invoice__c == false, 
      'The invoice mailer appears to have succeeded even though the test case had no associated attachment');
    }
    
    // ensure no exceptions are thrown if case has more than one attachment
    // and that the send invoice flag is reset
    static testMethod void testInvoiceMailerErrorTooManyAttachments() {
      // create test case
    Case testCase = getTestCase();
    insert testCase;
    
    // add multiple attachments
    Attachment testAttachment1 = getTestAttachment(testCase);
    insert testAttachment1;
    Attachment testAttachment2 = getTestAttachment(testCase);
    insert testAttachment2;
    
    // mimic invoice approval process by checking send invoice
    System.Test.startTest();
    testCase.Send_Invoice__c = true;
    update testCase;
    System.Test.stopTest();
    
    // assert that send invoice is not checked as a case with more than one attachment
    // should generate an error email and uncheck the send invoice flag
    testCase = [select send_invoice__c from Case where id = :testCase.id];
    system.assert(testCase.send_invoice__c == false, 
      'The invoice mailer appears to have succeeded even though the test case has more than one associated attachment');
    }
    
    // create a generic case related to a contact with a non-null email
    private static Case getTestCase() {
      Contact testContact = new Contact(lastName = 'Test', email = 'test@mailinator.com');
      insert testContact;
      
      Case testCase = new Case(contactId = testContact.id);
      return testCase;
    }
    
    // create a generic attachment related to a given case
    private static Attachment getTestAttachment(Case parentCase) {
      Blob attachmentContent = Blob.valueOf('Hello World!');
      Attachment testAttachment = 
        new Attachment(parentId = parentCase.id,
                 name = 'Invoice.pdf',
                 body = attachmentContent);
      return testAttachment;
    }
}


 
Best Answer chosen by Patrick J Waples
mjohnson-TICmjohnson-TIC
Quickest way to make it work is deleting the assert on line 86. T

system.assert(testCase.send_invoice__c == false,
'The invoice mailer appears to have succeeded even though the test case had no associated attachment');
    }

Is there a trigger or workflow on Case that was not moved over to your new org? The assertion is that updating the Send_Invoice__c boolean from false to true should not work due to some behind the scenes logic (possibly written in a trigger or workflow).

All Answers

mjohnson-TICmjohnson-TIC
Does the test class not compile or does it fail when the test is run? What is the error message you get?

Most likely scenario I can come up with is not all the custom fields from your old org exist on your new org. See below.

Does the checkbox field Send_Invoice__c exist on your Case object in your new org?
Patrick J WaplesPatrick J Waples
Thanks for your reply!  The errors I get are as follows:

InvoiceMailerTest testInvoiceMailerErrorNoContactId Failed System.Exception: Assertion Failed: The invoice mailer appears to have succeeded even though the test case had no associated contact.

InvoiceMailerTest testInvoiceMailerErrorNoAttachment Failed System.Exception: Assertion Failed: The invoice mailer appears to have succeeded even though the test case had no associated attachment

InvoiceMailerTest testInvoiceMailerErrorTooManyAttachments Failed System.Exception: Assertion Failed: The invoice mailer appears to have succeeded even though the test case has more than one associated attachment

I checked and do have send_Invoice in the new org, yes. :(
mjohnson-TICmjohnson-TIC
Quickest way to make it work is deleting the assert on line 86. T

system.assert(testCase.send_invoice__c == false,
'The invoice mailer appears to have succeeded even though the test case had no associated attachment');
    }

Is there a trigger or workflow on Case that was not moved over to your new org? The assertion is that updating the Send_Invoice__c boolean from false to true should not work due to some behind the scenes logic (possibly written in a trigger or workflow).
This was selected as the best answer
Patrick J WaplesPatrick J Waples
OH!  You're absolutely right!  There's a trigger that we removed from the migration list as unused that just does that...flips the Send_Invoice__c back to false.  I can't believe I missed that...not the fact that this CLS was looking for that...but that the trigger we didn't move was needed. 

Thank you!  Once moved the test class passes flawlessly!