Wednesday 29 August 2012

Invoking the Trigger - Apex

Prerequisites:
  • Tutorial #3: Creating Sample Data
In this lesson, you’ll invoke the trigger you created in the previous lesson. The trigger fires before the deletion of invoice statements, so you can cause it to fire by deleting an invoice statement either through the user interface or programmatically. In this lesson, you’ll perform the deletion through the user interface so you can see the error message returned by the trigger when the invoice statement has line items. You’ll also create an invoice statement with no line items. You’ll be able to delete this new invoice statement since the trigger doesn’t prevent the deletion of invoice statements without line items.
  1. In the Salesforce user interface, click the + tab.
  2. Click Invoice Statements.
  3. With the View drop-down list selected to All, click Go!.
  4. Click the sample invoice statement, with a name like INV-0000.
  5. On the invoice statement's detail page, click Delete.
  6. Click OK when asked for confirmation.A new page displays with the following error message:

    Validation error on deletion of invoice statement
  7. Click the link to go back to the invoice statement's page.
  8. Click Back to List: Invoice Statements.
  9. You’re now going to create another invoice statement that doesn’t contain any line items. Click New Invoice Statement.
  10. Click Save.
  11. Let’s try to delete this invoice statement. To do so, click Delete.
  12. Click OK when asked for confirmation.This time the invoice statement gets deleted. When the trigger is invoked, the trigger query only selects the invoice statements that have line items and prevents those records from deletion by marking them with an error. Since this invoice statement doesn’t have any line items, it is not part of those records that the trigger marks with an error and the deletion is allowed.

Tell Me More...

  • The validation error message that appears when deleting the sample invoice statement is the error message specified in the trigger using the addError method on the invoice statement sObject: 'Cannot delete invoice statement with line items.'
  • The trigger universally enforces the business rule, no matter where operations come from: a user, another program, or a bulk operation. This lesson performed the deletion manually through the user interface.

No comments:

Post a Comment