Salesforce
Apex-Auslöser
Suche…
Syntax
- Auslöser < Name > für < Objekt-API-Name > (< Ereignisse >) {// Ihre Auslöserlogik}
Parameter
Parameter | Beschreibung |
---|---|
Name | Name des Auslösers |
Objekt-API-Name | Objekt, an dem der Abzug ausgelöst wird. Kann ein beliebiges Standard- oder benutzerdefiniertes Objekt sein. |
Veranstaltungen | Ereignisse, die den Abzug auslösen. Sind eine Kombination aus entweder before / after einem der insert / update / delete . Es gibt auch after undelete ohne before Pendant. |
Grundauslöser
trigger AccountTrigger on Account (before insert) {
System.debug('Account(s) are about to be inserted');
}
Kontextvariablen auslösen
trigger ContactTrigger on Contact (before insert, after insert,
before update, after update,
before delete, after delete,
after undelete) {
/** Before or After trigger execution**/
//Returns true if trigger is before
System.debug('Trigger:Time:Before : ' + Trigger.isBefore);
//Returns true if trigger is after
System.debug('Trigger:Time:After : ' + Trigger.isAfter);
/**DML Operation trigger execution **/
//Returns true if trigger is insert
System.debug('Trigger:DML:Insert : ' + Trigger.isInsert);
//Returns true if trigger is update
System.debug('Trigger:DML:Update : ' + Trigger.isUpdate);
//Returns true if trigger is delete
System.debug('Trigger:DML:Delete : ' + Trigger.isDelete);
//Returns true if trigger is undelete
System.debug('Trigger:DML:Undelete: ' + Trigger.isUndelete);
/** Records on Trigger execution **/
//Returns data in state before DML. Records are read only
//Not available for Insert Operation
//Format: List<sObject>
List<Contact> old_contacts = Trigger.old;
System.debug('Trigger:Data:Old : ' + old_contacts);
//Returns data in state before DML. Records are read only
//Not available for Insert Operation
//Format: Map<Id, sObject>
Map<Id, Contact> old_contacts_map = Trigger.oldMap;
System.debug('Trigger:Data:OldMap : ' + old_contacts_map);
//Returns data in state after DML.
//Allowed for modifications in before context only
//Not available for Delete Operation
//Format: List<sObject>
List<Contact> new_contacts = Trigger.new;
System.debug('Trigger:Data:New : ' + new_contacts);
//Returns data in after before DML.
//Allowed for modifications in before context only
//Not available for InsertOperation
//Format: Map<Id, sObject>
Map<Id, Contact> new_contacts_map = Trigger.newMap;
System.debug('Trigger:Data:NewMap : ' + new_contacts_map);
/** Another context variables **/
//Returns amount of record in DML for trigger execution
System.debug('Trigger:Size :' + Trigger.size);
//Returns true if the current context for the Apex code
//is a trigger, not VF, web service or anonymous apex
System.debug('Trigger:isExecuting :' + Trigger.isExecuting);
//Simple example how to use above context variables
//for different scenarios in combination
if (Trigger.isBefore && Trigger.isUpdate) {
// actions for before update
} else if (Trigger.isAfter) {
if (Trigger.isUpdate) {
// actions for after update
} else if (Trigger.isInsert) {
// actions for after insert
}
}
}
Manipulieren von Datensätzen, die den Abzug ausgelöst haben
trigger MyTrigger on SomeObject__c (after insert, after update) {
if (Trigger.isAfter && Trigger.isInsert) {
System.debug('The following records were inserted: ');
for (SomeObject__c o : Trigger.new) {
System.debug(o.Name);
}
} else if (Trigger.isAfter && Trigger.isUpdate) {
for (Id key : Trigger.newMap) {
SomeObject__c theOldOne = Trigger.newMap.get(key);
SomeObject__c theNewOne = Trigger.oldMap.get(key);
if (theNewOne.Name != theOldOne.Name) {
System.debug('The name of ' + key + ' has been changed');
}
}
}
}
Modified text is an extract of the original Stack Overflow Documentation
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow