• Patrick J Waples
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
I'd like to automate the creation of the product schedule when an opportunity product is saved.  I think that I should be able to do this relatively easily with an APEX trigger this using the process builder...but I'm not a developer.  So it's also entirely possible that it's not easy.

Essentially I'd be taking a custom field "Net_Total_Amount__c" and dividing it evenly daily in between "Start_Date__c" and "End_Date__c".

 How difficult is something like this to set up?  Is the basic premise sound?
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;
    }
}


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