Wednesday, 29 August 2012

Creating a Trigger - Apex

Prerequisites:
  • Tutorial #1: Creating Warehouse Custom Objects
  • Tutorial #2: Using the Developer Console
  • Tutorial #8: sObjects and the Database
The trigger you’ll create in this lesson fires before the deletion of invoice statements. It prevents the deletion of invoice statements if they contain line items.
  1. In the Developer Console, click the Repository tab.
  2. In the Setup Entity Type section, click Triggers, and then click New.
  3. Expand the drop-down list next to New, select Invoice_Statement__c, then click New.
  4. For the trigger name, enter RestrictInvoiceDeletion and click OK.
  5. Delete the auto-generated code and add the following.

    trigger RestrictInvoiceDeletion on Invoice_Statement__c (before delete) {
        // With each of the invoice statements targeted by the trigger  
        
        //   and that have line items, add an error to prevent them  
        
        //   from being deleted. 
        
        for (Invoice_Statement__c invoice : 
                        [SELECT Id
                        FROM Invoice_Statement__c
                        WHERE Id IN (SELECT Invoice_Statement__c FROM Line_Item__c) AND
                        Id IN :Trigger.old]){
            Trigger.oldMap.get(invoice.Id).addError(
                                            'Cannot delete invoice statement with line items');
        }
    }
  6. Click Save. Once you save the trigger, it is active by default.

Tell Me More...

  • The trigger is called RestrictInvoiceDeletion and is associated with the Invoice_Statement__c sObject.
  • It fires before one or more Invoice_Statement__c sObjects are deleted. This is specified by the before delete parameter.
  • The trigger contains a SOQL for loop that iterates over the invoice statements that are targeted by the trigger and that have line items.
  • Look at the nested query in the first condition in the WHERE clause: (SELECT Invoice_Statement__c FROM Line_Item__c). Each line item has anInvoice_Statement__c field that references the parent invoice statement. The nested query retrieves the parent invoice statements of all line items.
  • The query checks whether the Id values of the invoice statements are part of the set of parent invoice statements returned by the nested query: WHERE Id IN (SELECT Invoice_Statement__c FROM Line_Item__c)
  • The second condition restricts the set of invoice statements queried to the ones that this trigger targets. This is done by checking if each Id value is contained inTrigger.oldTrigger.old contains the set of old records that are to be deleted but haven’t been deleted yet.
  • For each invoice statement that meets the conditions, the trigger adds a custom error message using the addError method, which prevents the deletion of the record. This custom error messages appears in the user interface when you attempt to delete an invoice statement with line items, as you’ll see in the next lesson.

No comments:

Post a Comment