Writing a Simple Apex Trigger !!<\/p>\n
Now we know where to write a Trigger, let\u2019s begin with a simple example. In this scenario I would like to add a custom text in the last name of a contact.<\/p>\n
Let’s have a look at the basic structure of a trigger<\/p>\n
trigger MyCustomTriggerName on CustomObject__c (trigger_events) {<\/p>\n
Line of codes to execute(Business Logic)<\/p>\n
}<\/p>\n
Using the above-mentioned structure we will write our code, in the above code we can give Trigger any name we want, and since we want the trigger to be on contact standard object, the structure of trigger looks like below<\/p>\n
trigger ContactTestTrigger on contact (before insert) {<\/p>\n
Line of codes to execute(Business Logic)<\/p>\n
}<\/p>\n
Now that we have defined the basic structure of the trigger now we will write the business logic as discussed in the beginning.<\/p>\n
trigger ContactTestTrigger on contact (before insert) {<\/p>\n
for(contact con : Trigger.new){<\/p>\n
con.lastname\u00a0 = \u2018New Last Name using Trigger\u2019;<\/p>\n
}<\/p>\n
}<\/p>\n
<\/p>\n
This is it, we have written a simple Trigger which initiates at before insert event of a contact. Based on the occurrence of the DML event, there are broadly two types of Triggers; Before Trigger and After Trigger. Further, the trigger can be classified into the below groups based on the events.<\/p>\n
In Trigger, we can modify the data without explicitly calling any DML operation, and if the operation is performed then it results in an error. The record is committed to the database once the trigger has completed its execution<\/p>\n
Here you also see the use of a variable Trigger.new in the for loop, it is a trigger\u2019s context variable.\u00a0 Trigger\u2019s context variable holds the values of the record which initiated the execution of the trigger, for e.g. Trigger.New contains the list of contacts which initiated the trigger. There are some Trigger\u2019s context variables which return Boolean values and with the help of context variables, we can determine which event fired the trigger. There are many context variable available in Salesforce and can be studied in detail here<\/a>.<\/p>\n This concludes how to write a Simple Trigger<\/p>\n Next, I would like to present, how to call apex class method in trigger, callout, and using trigger exceptions<\/p>\n find interview Questions on Trigger here<\/p>\n Interview Questions Apex Trigger<\/a><\/p>\n Writing a Simple Apex Trigger !! Now we know where to write a Trigger, let\u2019s begin with a simple example. In this scenario I would like to add a custom text in the last name of a contact.<\/p>\n","protected":false},"author":1,"featured_media":1342,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[352,40,79,61,74,45,75,38,42,55,347,349,348,354,350,355,51,56,357,356,353,351,52],"_links":{"self":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/885"}],"collection":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/comments?post=885"}],"version-history":[{"count":4,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/885\/revisions"}],"predecessor-version":[{"id":2208,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/885\/revisions\/2208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media\/1342"}],"wp:attachment":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media?parent=885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/categories?post=885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/tags?post=885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Also, Have a look at the below learning resources:<\/strong><\/h1>\n
\n
SOQL (Salesforce Object Query Language)<\/a><\/strong><\/em><\/h3>\n<\/li>\n
Apex Trigger Best Practices and the Trigger Framework<\/a><\/strong><\/em><\/h3>\n<\/li>\n
Salesforce Interview Question and Answers Part 2<\/a><\/strong><\/em><\/h3>\n<\/li>\n
Salesforce Interview Questions on Test Class<\/a><\/strong><\/em><\/h3>\n<\/li>\n
Salesforce-lightning-interview-questions-2018<\/cite><\/span><\/a><\/strong><\/em><\/h3>\n<\/li>\n<\/ol>\n
\u00a0 \u00a0 \u00a06.\u00a0Salesforce Interview Questions Batch Class\u00a0<\/a><\/strong><\/em><\/h3>\n","protected":false},"excerpt":{"rendered":"