Apex Trigger and its context variable

Salesforce Development Using Apex Get Started with Apex Triggers
52 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€48.03
List Price:  €67.24
You save:  €19.21
£39.77
List Price:  £55.68
You save:  £15.91
CA$71.90
List Price:  CA$100.66
You save:  CA$28.76
A$79.87
List Price:  A$111.82
You save:  A$31.95
S$67.76
List Price:  S$94.88
You save:  S$27.11
HK$388.39
List Price:  HK$543.79
You save:  HK$155.39
CHF 44.76
List Price:  CHF 62.67
You save:  CHF 17.91
NOK kr565.88
List Price:  NOK kr792.28
You save:  NOK kr226.40
DKK kr358.23
List Price:  DKK kr501.55
You save:  DKK kr143.32
NZ$88.36
List Price:  NZ$123.71
You save:  NZ$35.35
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.45
৳5,948.35
List Price:  ৳8,328.16
You save:  ৳2,379.81
₹4,255.48
List Price:  ₹5,958.01
You save:  ₹1,702.53
RM224.45
List Price:  RM314.25
You save:  RM89.80
₦77,307.03
List Price:  ₦108,236.03
You save:  ₦30,929
₨13,855.41
List Price:  ₨19,398.69
You save:  ₨5,543.27
฿1,710.05
List Price:  ฿2,394.21
You save:  ฿684.16
₺1,760.62
List Price:  ₺2,465.01
You save:  ₺704.39
B$304.36
List Price:  B$426.13
You save:  B$121.77
R921.37
List Price:  R1,290
You save:  R368.62
Лв93.78
List Price:  Лв131.30
You save:  Лв37.52
₩72,554.44
List Price:  ₩101,582.03
You save:  ₩29,027.58
₪181.80
List Price:  ₪254.54
You save:  ₪72.73
₱2,927.63
List Price:  ₱4,098.92
You save:  ₱1,171.28
¥7,833.39
List Price:  ¥10,967.38
You save:  ¥3,133.98
MX$1,004.59
List Price:  MX$1,406.50
You save:  MX$401.91
QR181.46
List Price:  QR254.06
You save:  QR72.59
P688
List Price:  P963.25
You save:  P275.25
KSh6,466.20
List Price:  KSh9,053.20
You save:  KSh2,587
E£2,548.98
List Price:  E£3,568.78
You save:  E£1,019.79
ብር6,205.09
List Price:  ብር8,687.63
You save:  ብር2,482.53
Kz45,890.82
List Price:  Kz64,250.82
You save:  Kz18,360
CLP$49,495.04
List Price:  CLP$69,297.02
You save:  CLP$19,801.98
CN¥364.88
List Price:  CN¥510.87
You save:  CN¥145.98
RD$3,031.17
List Price:  RD$4,243.89
You save:  RD$1,212.71
DA6,739.39
List Price:  DA9,435.69
You save:  DA2,696.29
FJ$115.74
List Price:  FJ$162.05
You save:  FJ$46.30
Q383.54
List Price:  Q536.99
You save:  Q153.44
GY$10,414.16
List Price:  GY$14,580.66
You save:  GY$4,166.49
ISK kr6,969.10
List Price:  ISK kr9,757.30
You save:  ISK kr2,788.20
DH500.98
List Price:  DH701.41
You save:  DH200.43
L922.32
List Price:  L1,291.33
You save:  L369
ден2,953.66
List Price:  ден4,135.36
You save:  ден1,181.70
MOP$398.49
List Price:  MOP$557.92
You save:  MOP$159.43
N$916.42
List Price:  N$1,283.07
You save:  N$366.64
C$1,831.73
List Price:  C$2,564.57
You save:  C$732.84
रु6,771
List Price:  रु9,479.94
You save:  रु2,708.94
S/185.35
List Price:  S/259.51
You save:  S/74.15
K201.85
List Price:  K282.61
You save:  K80.75
SAR187.83
List Price:  SAR262.99
You save:  SAR75.15
ZK1,377.58
List Price:  ZK1,928.73
You save:  ZK551.14
L239
List Price:  L334.62
You save:  L95.62
Kč1,207.24
List Price:  Kč1,690.23
You save:  Kč482.99
Ft19,915.06
List Price:  Ft27,882.67
You save:  Ft7,967.61
SEK kr551.56
List Price:  SEK kr772.23
You save:  SEK kr220.66
ARS$51,102.27
List Price:  ARS$71,547.27
You save:  ARS$20,445
Bs343.97
List Price:  Bs481.59
You save:  Bs137.61
COP$218,480.73
List Price:  COP$305,890.51
You save:  COP$87,409.77
₡25,114.53
List Price:  ₡35,162.35
You save:  ₡10,047.82
L1,263.58
List Price:  L1,769.11
You save:  L505.53
₲388,143.81
List Price:  ₲543,432.39
You save:  ₲155,288.58
$U2,236.96
List Price:  $U3,131.93
You save:  $U894.96
zł204.70
List Price:  zł286.59
You save:  zł81.89
Already have an account? Log In

Transcript

This conference will now be recorded. No. So till now we have discussed about, like my collections variables how to create how to write the code in Developer Console. Correct. So it's a great assertion and DML operations. Yeah, highest is going to be punished out any toffee.

Let me know if you have any question for me. No. Perfect. Okay, now as we have discussed about two different types of code, DML operations, like insert the update of third or mas. So PL everything. And this you can follow.

So now what I'm going to do here, so we understood, okay, when I want to write a piece of code, we go into this Developer Console. And we write the code, CW console. And here we write some piece of code. Most code, which I didn't say when you here, but we integrated. We see I just went like the developer, the book opening to diagnose window, we write some that collection examples or some kind of follow, we aggregate it, and we see the output. And I'm not saving that code in here.

So this is like this is one place where I can write the code financing writing of the course. What is that? So now if we see, okay, the way we can invoke graphics, okay. So I think I think what piece of code but what we have written in Developer Console. Now, there is one pathway we have learned from Developer Console as an anonymous window. Okay, we have written some piece of code and integrated, so that anonymous code will be executed at that moment and we do seven years.

So whenever this field intervention happens, that code is not going to do Anyway, because that data is not going to save on my affects metadata server at the moment passes, and delta will learn how many Fs code is being saved in the server. Okay, and when the angels are trying to assess that, that's not going to be invoked. So in that case, let me show you that also, if you remember, like, even with this diagram, when we write some code, that code being saved in mind servers, or the plasma teller server, after comparison, and when some injured associate, we assess from there, but our inverse costs are not being saved. We are not being saved anywhere. We don't have any name to that. So we said that anonymous code, so what are the ways we can invoke the objects?

They pass? We should know that. So one we saw anonymous window, okay, same way, we have something called let's see, I want to execute some piece of code when I have Inserting code or going I'm auditing a account, okay, I want to do something. So that meant nothing but there is some concept we have call a trigger. So how to execute that? Or how to invoke the app of epics during the trigger?

Or we may write a page or we can design a page from the page the logical can write in epics. So how am I epics can be calling a page, or we can write what email service. Okay? Nothing but we'll write one piece of code. When an email comes to one specific address. We'll discard the disciplines.

So how we can write up this class so that my email service can use it. Same way. I have some asynchronous mode topics, okay? How to execute that also, we'll discuss, okay, all this all this we're going to discuss in the system, okay. And whenever we're integrating two different system, so that time also I mean, it happens So we understood what is unused window and how I am integrating it all the way to go to developer console from Developer Console, you just go to debug window, write a piece of code interpreted. I'm not going to save anyway just a good that disappeared, but I want to write a code which will save it, but what I can create a class with a specific name, I can save in my system and I can use it.

So that class I can invoke in my app is triggered. So as you should know, what is this trigger and what is the proposal trigger and how I can write trigger on my system, okay. And this trigger is a very, very important topic for us, okay. So, let me explain you one basic scenario. Let me close this. Okay.

So when I say trigger, what is people to understand that I should understand the structure of transition point, I'm doing the educational flow from my record to my database and attacking completely the initial flow the way my record being flowed from UI to database. Okay, so see, I have a database here. You submitted the cinnamon database. Okay, here I may have many objects, okay? Let's say employee account opportunity or maybe position candidate anything. So say may I have one page?

Okay, here I have let's consider my account name, account type, or maybe candidate name candidate email candidate a mobile number candidate SSN, candidate experience like this many fields we have. And with that, I have one button call set. Okay, so let's see. This is one button which we call us. Let me change the colors. Okay, so here I have left Consider this is a field, which we call custom.

Last name, like this many fields here. Okay? So now the moment I save this effort, what happens the moment you fail on this form, and we're trying to click on the button, say, normally, we're trying to click on the button save what exactly going to happen, the data from this page will take to my database. Let's see, I am targeting to this object to create a new record. If I'm creating newly or an object, then go and find out modify the existing record. So now when this flow is happening, I want to execute some some piece of code.

Once I click this button, I want to execute some piece of code. So how I can do it. So nothing but if you go here, let's, the moment you try to create a candidate. I'll create a call. Let's I'll go to the normal example for accounts that I go to this other application. I assume Stop Account tab.

From here, I'm opening this account. Okay, let's see. I'm just going to update this account. I click Edit. Then I opened the page in the edit mode. I just modified Leslie's account site a modified.

Okay? Something name I give. Okay? All right, I just clicked save the moment, I'm clicking Save button. But the moment I click this button, there is a DML operation going to happen on my database. What is the DML operation?

Right now I'm going to update some information of how these account record in my object. Okay, so now when this develops happening in between these operations when click on the Save button and saving database, committing the data to a database, I want to record some piece of epic score. Let's see, I want to do I wrote some lines of code here. I want to do this so how I can into Discord. How I want to do this is with logic. Now what if I say, the moment you create a new account?

Let's go to here account. Okay? The moment you create a new account, let's see I just gave name as new account, okay? And I just gave some oil say, Okay. I just give public account or something and gave and I gave some address and some information here. And I think they said the woman I'm going to do these, let's consider, I want to create one contact by default for this account, I want to create one admin contact for this.

But right now if I save it, it is just an account nothing happening. It's just an account. It's nothing happening no contact, nothing. Why but I want to create an account automatically, one contact recruited. So to create a contact. What do you do?

We know we can create content called Niccolo new content something like this right if you remember as in previous essence to insert any record we do like this or contact we do that instantiation of this content object then we set the field name for that okay then in the end we do insert. So, this piece of code I want to hire whenever there is one account being saved in a database. Great. So now, I write this piece of code, okay. So this code How are you good, okay to execute that piece of code, we have some concept we call it triggers. So we are going to discuss about trigger okay.

The use of it and what are the best practices for it and what are the real time implementation of it, all things will do. So passing the member optics we can invoke from tree So, what is triggered like I explained how this anonymous window getting triggered and how I can it occurs class to execute topics into Jupiter inside the trigger okay. So, epics can invoke from a trigger, so it is nothing but it is allowing us to execute our own logic okay whenever there is a DML operation in object okay. So what we can execute any logic during DML operations, okay of that object. So see if I want to execute some specific logic on account said or account object and I'll write a piece of logic which like you could only for the account whenever or when I want for a contact or maybe opportunity creation, I want it to some logic I will write a piece of logic which will be invoked during a port syndication how we do how we do that So, if you see so simply I can say this is nothing more I'm reading a piece of code.

So So anthing trigger is nothing but some piece of epics code okay which will execute whenever there is a DML operations happening okay demon a person nothing board I can say insert been like update delete anything okay. So, if a tell technical definition a triggering of expose that a gigahertz before or after DML operations so what type operations we have demons, we know we have insert, we know we have update, we know we have delete, okay and these are the basic operations if you remember Have you heard something called offset okay during offset also mind reader will exhibit also same way we have something called March okay we have something on it Eve have deleted something and when I roll back that that time also mentioned go physiological. So we know this INSERT, UPDATE delete, but when it's offered nothing but this is consists of two operations which is insert and update.

So, both operations let me tie this normal language in front of it. So, when you fire these offset in any database operations, so it will check, insert and update both. So, both the logical pair both the logic or both the events insert and update will pass in the system. So that might result in during the both cases say most nothing but if you remember I said delete and update. So, in both these are essentially fire I Lakeland Technically what it is, then you can get that good idea. Already Nothing worse.

I'm just on deleting the record from my Recycle Bin, nothing but I'm rolling back roll, I'm doing roll back for that record. Simply a trigger that invert a piece of code will execute on different DML operations that matter. Okay, so financing DML operations happening. Remember one point here, so, for better understanding, I'm just breaking this line into part. Let me make it bigger one. Let me change some color.

See, somewhere in between, I just made a line here. Okay. So now, from this line, this is what I'm saying. And this is what I'm just making this to make you understand okay. So, I am saying this is nothing but after this point, this point, and this side everything after of this point, and answering this one, I'll be put up this point. Okay.

So, whenever I am doing a DML operations we divide into part Okay. One part is before one part is after. So whenever the DML operations operations part into part, okay, so in the complete journey from user interface to my database, there is a place where we save the record but we don't commit the record database. Okay. So that means the record is saved and it can be updated database, okay, like like see, whenever I am going to click on the Save, let's go here. That means extremely once you know the one I just edited, and I'm just going to click the Save.

Okay, while I'm saving, I initiate my operations. Okay, but before inserting the record or before creating the recording mentors, I will check the validation rules. Okay, all the records So, as for my business requirement or not, what are the validation rules we have written? Okay. So, before that we will check if you if all these things are okay, then that means my record can be served. And at that point, we say DJ point hammered as said so before and after, okay then I go to database and I can commit that record to my database.

So see the complete journey I break into part one I call a set okay then before set and after ship, everything done, then I'll commit the record. So, now the trigger when you are writing, so it is nothing but whenever any DML operation fire, I will execute a piece of code. So it reads are nothing but a piece of apics code. Okay, that executes for any of the events and that even when I'm saying the person's Okay, and that often assumes nothing but insert, update, delete, merge, offset, and on And these are presents again I'm looking into but it may be before insert or maybe after insert, maybe before update or maybe after the update, we'll see how we can flick it. So, if you're on a triggers nothing but if you say account we trigger the trigger okay if you creating a new account that means you are inserting your account.

So my logic I can execute on before or or after, I will explain in PowerPoint for doing it. You know any confusion now perfect. So now think example. Whenever you are creating an account, or you want that account record, when you're saving it will create a contract, like the example what I said. So in the end, once you create account then go and create a new contact record. This is just considered the contact record content object.

This is an account object Okay, so we're going to create a new account we'll create a contemptible so I don't know how to write this piece of logic okay what I did I just wrote some piece of code okay some piece of code I wrote which code helped me to create an account now, this piece of code should I write in before part or should I write in appropriate So, this piece of code so data it can be prepared or suniya right enough input okay. If you see that let's consider a scenario I just go on create a account. So my entries are addition certain formula I am going here, okay. So now pasinetta will see EP right this piece of code in before but so now I execute this logic Okay. Then I have created a contract here. Now, after some time, we check some healthy some rule.

Okay, then we saw This role based on this role, this account does not have proper data. So I am not allowing you to save this record, it's wrong, this account cannot be saved. So that moment the account creation is rolled back then I create a contract I roll back or maybe my contract only created without a account. So if this kind of scenario comes, it's not good right here. Let's see, I come to this path in life nothing but my apartment. So I create a car account, I when I checked validation rule everything all paths.

So my account is able to serve in system there is no errors. So I saved after you save the record, which is being you cannot modify it, it is checked, and it's saved. So it can be committed. So now I create a content so I you know, after this point, my record is going to be coming to database. It's already saved but not committed. Now I'm going to commit to the dentist.

So in that case, I came here I create a content and and I do something. So, in that part of what we do here this kind of thing when you see nothing word we write in after event okay and you can hear now perfect. So, then what kind of logic you will write in before even let's see, I have some see all kinds of validation rules I can write in my configuration in my validation rules. So, there may be some complex error message that you want to show or maybe complex validation I want to So, okay or I want to check something before creating a record okay. So that day, I can write that piece of logic here in the before even give me some any logic in the record even which I can include into before insert that went before several hours. I want to check, okay, these are called satisfy these constraints and these conditions are met if that kind of change You want to do then we do in the before So in a nutshell, I can say whenever I'm going for before see the flow I made into but before or after, when we put whenever you want to manipulate anything on Sam record, this is a point you remember on we always when you want to manipulate, insert record okay?

Then go for before event okay? Or if anything you want to manipulate in order object or other record then go for after event so in keeping these two parents so if I go under check the account which I'm going to create if the same account I want to validate with some conditions. Okay before saving then I'll go over before even if I want okay whenever creating an account, I want to create another Recording content object, whenever there is a order records or other objects can, I think, let's see, in more specific case, another object or another record, then we can go for the upper limit. So, for example, whenever you create an account, I want to update some other account records. It may mean the same object, but I'm going to manipulate some another record, not the same record, then you write the logic in the opposite event, okay.

So see, this insert, again we can break it to part before insert after insert, same way, we can do for before update appropriate and before delete, or delete, correct. So these are all things we're manipulating here. So now, even summarize, everything is nothing which is nothing but is a piece of code, which in book or any kind of developer lessons, and each of lessons we divided into parts before he went on. event. So lots of homework like before insert after insert. So always remember, in context what happened, we can say there are two types of trigger we have.

Okay? One we call I'm going to do the type type or mistake two types of trigger. Okay, one we call before trigger one we call operative. So I'm talking everything theoretically now then I also use syntax for the syntax on how to write it Okay, before trigger an opportunity. So now, when a reporter is getting deeper insert after insert before insert the product everything comes Okay. Now, so whenever I want to write a trigger, how I will write and what is the syntax for that, okay.

So now we should know how can write it triggers like we we know how to write a class, or how to write efficient code Same way, how can we write triggers what are the syntax for this? So whenever you want to write a trigger Always the trigger will start with a keyword called triggers. Like my class starts with keyword called class bird. So I can write a class like this public class. Class Name. So now I can add in anything inside this the logic.

So same way you're going to write a trigger, you write a trigger, then what is my trigger name? Okay. And always see when I explain this example, I say an account insert happening. Okay? When there is a DML operation happening on an object, so whenever I'm writing trigger that trigger only bound to a object only. So trigger trigger name.

Then these triggers writing on which object I'm writing on object. So you can specify the object name. So what is the object for this? Okay, then the logic water right? See, I may have many logics correct. So quite logical.

You want to write in here this I will have the logics, the logic what I wanted to go, this wasn't you want to put on what time during before insert, or after insert, or before the update or after update. So that event should switch over here. So we can say, all the trigger events we can specify a portable unit. So for example, I want to write a trigger, which will be we're on account, okay? That's the account, trigger on account object, okay. And this is I want to trigger exactly only on before insert, simply, I can write to the for insert.

Okay. So if the same trigger needs to be integrated in a producer sourcing, you can right after insert it see if I want some logic to be good during update also I can write before update so I'm in here, so you can aggregate right, any number of events here, I think what before insert after insert before update after update So all David, you can write with comma separator. Okay? So that kind of thing we can do here. So let's see I'm just writing everything here. So you will understand it.

So now let's during delete, I want to do something so see before delete, okay, then after delete okay so now there is some operation phase here on Delete so up to offset and module is handled by insert, update and delete update then we come on so when I'm going to do on Delete it's nothing but in deleted from my database so it's in Recycle Bin I'm taking it so before Andre I cannot do anything because it's not in my database now. Okay, so once you do on Delete then my record coming to the table is nothing but during Andre only I have after on Delete you can execute some kind of logic you can execute the trigger code you if you want to be good the trigger code on after before on date it's not possible because that is not in my database till the time it don't perform any DML any operation in database, this trigger is not going to fire.

Okay. So these are the combinations of events we can use, you can use any one or you can use all of these at a time also as you like. Okay. So this is one question on undelete. So if the record has been removed from a recycle bin as well, because I believe it remains there for a certain period of time, right after that data goes from the recycle bin as well. So, can we also and delete those records which are no longer available in recycle or no, no, you cannot do you cannot undo it but it's not available available, then you can undo it, or anybody if you delete a file and it's going to recycle been in the system on the e commerce in the time it is here, then I can only delete it.

If it's not there, I cannot do that operations. So that means if any record that has been deleted two months ago, it cannot be deleted. Right? Let's see that the data is on the computers you have you can do it you can contact yourself or support team okay? So they will have some amount of money okay to roll back that records which is like sometimes when there are some business records which may be late and you want to roll back that you can communicate to self esteem and they can do from the backend with adding some some money for that. Okay, okay.

Perfect. So let's see, I want to write the trigger in the system. I'll see how to do it. So it's quite at godus I'll show different ways how to read the trigger. I want to set up on which object I want to write I want to write the trigger on account object if you go to account, I just search here. See there is something called trigger account under the person standard objects you can see under that object name here, you can go and try to trigger Okay, so see I just clicked new.

This is called the compensating tax will be calculated Trigger, then I can specify my trigger name, let's consider I just mentioned account because whenever I take good program, always remember giving a proper naming convention. So see if I wrote a trigger I went after today you come and see, you can see and understand, okay? Or if you give you a class, somebody can Yeah, coconut is the logic from the class name only to that, then party when you want here, okay, if you want only be for insert, see, I'm writing only for insert, I can say. So if you want this trigger to include in all the events, so you can write all the stuffs Obviously, I'm just copying from this. And I'm just I'm just keeping this so I'm just writing all the events here. Okay, everything is edited.

I can do it here. This is one way. So if you want to write these from developments of how you do see I can just to quickly leave you You can do several something, if you want from the console, nothing theory, you can go to this file, new new trigger. So all the trigger will associate to object to who's already one. Let's see, I want to write from the object called contact. For example, if you go here, I just cleared the contact, you can give the trigger name.

I just give the trigger name as contact trigger. Okay, now I click on Submit. So now see, there is one trigger theater. Okay? See these triggers are also you can see here your contract in your school here, you see district but also there's the other ways we can refer Can I detect this? Also, if you have any other tools?

Let's see. This isn't very good to use. Okay? Is ID or your name, what is Salesforce ID? Okay. So if you Let me log into this or they log into my Developer Edition I just log in there so here also you can editor Let me open this up EPA want to right in New Jersey I came here there is a new trigger simply trigger on each object and what is the name?

I want triggers. Let's see I will tell opportunities are okay on which object on opportunity. Okay, point I want I want to do it on paper insert okay. I can save that way also. So if you save it will save you see if they come to opportunity. See you can come to here if a sink this trigger so now we have opportunity Now this is the standard of your see if I want to write in position objects let's see I into my custom object and this is a plugin we use if you don't have plugin Don't worry just go to Object like this under create object you can go to this place go to that object name under that any related list you can see triggers you want to write you can write here also okay.

So what is the name or gives him or you can write trigger also here. So, for standard objects you can directly get order that object name only. Okay, so for any other objects, custom objects, you go to the object and see the Related list and click trigger. Okay, so when you're creating trigger, what do you have to give? You have to give trigger name on which object and then on what event? You want to execute the logic.

But my point till no or any questions Perfect and the code what will write will write inside this the complete logic what we want to do will read this. Okay? So now if you see this example Okay, we understood okay we'll head to the let's see, I wrote the logic to create another contact. Okay, so now I am creating the content. So this logic I don't want to execute whenever you delete a account or I don't want it to go whenever an object it I want it to go only in during the insert Okay, I am going to create another object record that means I am going to manipulate with the apple event as an excellent Okay, manipulating some another object then go for after a pivot. In this example, I'll go to my after event.

Correct. So now that means this visual logic I want to execute only during my upgrade nothing but I can say If, after even hired, then Abdul integrated, correct, that kind of thing. Otherwise, don't include this visual logic. So how I can find out these type of records? Any idea? So do you know any confusion?

Tell me then I'll come to this part. No conversion great. No. Okay, so now when I'm saying this, so how can I check these kind of things? And when I'm going to create another contract, so I may take some information or some data from my current account, we're inserting nothing but if you see this, I'm going to insert this account. Okay, so the moment I'm going to create this content, I'ma pick some data from this record and that record that I can keep on my contact object.

Okay, that means how can I access the record data from this Subject the record which I'm going to serve, how can I get data from there okay. So, what are the things available functions for us to use for this to understand that we should know what is context variable Okay, we have some trigger contest variable let me moderate most respect trigger context variable okay. So we can use this trigger context variable okay which will help us to manipulate data to understand on which event I am heading. All the all the steps okay. So whenever I'm talking to your country's variable we have many trigger countries variable given by sensors. Okay, so what are the variables we have?

So let's see. We are doing a question you have Tell me. So is the insert before or all those things are case sensitive. Know when you're writing epics If nothing gets case sensitive, you can write insert you can add all caps or you can write the one cap one small like this How will that one you can you can write like this notice if you want to all you can it is not case sensitive at all okay there was nothing like that okay. So see for example, I want to check the trigger when you're firing. So, it is failing due to insert or due to due to update or delete if you want to check that kind of thing in the trigger context variable, we have some variable already predefined by Salesforce to understand that particular one we call it insert.

So if all these variables return me a boolean value it will return me a Boolean hello to understand this return inside this trigger, exhibiting in insert event or not. Okay, so nothing ready to return. True or false. Okay, If and inserting a record that time this insert going to be true. If I'm updating a record, these aliens are going to be false. Okay?

Same way I have something called each update I have something called delete Okay, I have something called is needed so see all operation what I'm controlling all this across all this I can check from here okay. So it return true or false depending on what DML operations padding on the record right now, which record executing the trigger this is one. So now for going to happen here I want to check my entries are executing on before or on after based on that I divided it before event or after event. So my entry right now I'm in before your event or in May after event how to know to know that we have a contest variable to query which you can One we call is before one we call you doctor that contracts are able to cancel. This is also again returning a true or false is depending on which contest we are right now in reverse.

So are we exhibiting before event, or are we putting after event? Okay? So these are quotas variable abuse. Now see, I said whenever I'm going to create a content, I need to assess some record from this. So to get that we have something called New okay new countries variable. I will write all the controls in a lesson on day one what it is, and we have cold we have new map, we have all map.

Okay, these contests are I will continue the code. So what is new, the new record, which I'm trying to insert the content for time I have, you know what, if you go to this account, let me open it. printed on see when I create this new, what information I'm just entering here I'm going to serve okay? So everything part of what I'm writing here okay. So, all these things when you click save all the data will be stored will be saved as a collection elements list. So, see you may have wondered or I can insert many records in a single transactions mean in the list of collection elements or maybe by using some Data Loader for that event what happened here though all the records is going to be in a collection, it may be one record or many records that nothing but my new new Asia context variable which is going to contain all the new data in all the new records which are going to manipulate in database Okay, so this new is nothing but going to return me a list of new records Okay, so, the record we can trying to separate a device that will contain in new, which I think would at least have record, you remember the collection now see how many Collison coming to us here and seeing this new contents my list of workers okay.

So, when these new variables are available for us we may not get always all the context variables we will see when new available when all is available. Okay, whenever creating a new record that may not have a world record or nothing but the old copy of that that metric. So let's see, I'm just going to modify some account. Okay. So I mean, this bullington capital account. So now I have industrial apparel.

Now what I'm doing, I'm just going to update it. I edited I just change industry to let's see environmental Okay, so this is my new record pod I'm going to start with a new Hello then what is my old record for this dcma will record with old Hello. So whenever you update that time, I will have my old record and my new record but when you insert halftime you are you don't have right now what is the world record? So based on the contest based on the type of person or person the DML events, then we can see which contest variable or variable at what moment, okay, so why not say this new? Okay, its contents the list of new records and this is available only during insert and update that I did and on delete, okay, so now whenever I'm deleting the record, so I'm just deleting from database, I am not modifying any value there.

Whatever database I just deleted that thing. We don't have Anything new, okay? So only this new can be available on insert, update and on Delete. Okay, and that means this new record what I'm saying right now. So, you do insert or update it will update and this new record thing, but you can modify in that context you can modify anything on the record during this before even once you're before even ignition completed, then you are not allowed to modify anything you can modify till this point, if you want to change anything on the record, after this, it is read only you cannot modify the assembler code which is slow because we already saved it We are waiting only to commit. So anything you want to modify that occurred you can modify or to dismiss.

So in that context what I said the new is available only during insert, update and on Delete. Okay, then what is old, old nothing but it will return us the old copy of record as explained in this scenario, this image existing copy in database and I'm modifying to this what is modifying? That is my new and which one I have before that it may old. Okay, let's see now I change industry industry to environmental and before I have industry to epidemic, this complete record isn't nothing in nothing but my world record and this complete record is my new record. So what happened here though this old they turned me against, again a list of old version of record, okay. Okay, so Queen this role is available, this role will be available during updating because during insert I don't have, okay.

I don't have data in database. So during insert, it's not available. So when you update that means I have one copy of the recording database I'm more than good. So that means the old available during our added and delete okay we're going to deleting I have record numbers which I'm deleting that is the old copy and once you delete we have nothing okay then what is new map so new map nothing but same thing but with an ID. So if you see you remember map map is a collection of key analogous. So in this new map also a collection of ID and record pair.

Okay, here is the ID and hello is the same record. So this is what I knew my contents whenever I'm updating this, okay, this record, so this record I said in new it is just this record, but new map this record with their Id see this is record it, this record ID will be in the key and this is going to be added Hello. So that time we will have the new map. new map is a collection of ID and my a subject records and appointment will available which will happen available for a financing ID, or not once my record saving database, Id will come this ID till the time we don't say I don't have ID, this is what I'm trying to say I don't have ID, but once I say I have ID, so that means this will create, always in after time. Okay?

So once you insert the record, I may have the new map, okay? So if you want to be updated record, so before update, also, you can access this new record because that's because I have my ID, I have my record. So also, after update, I may have dition. But before insert, I don't have before insert you did we are trying to insert it. So database didn't issue one ID for this record, okay, only after insert before update after update, okay, so all the times when I have one specific ID, okay. And once you own delete from the system, then I can assess it because once you handle it again it will be in my database and I can get matrix Like the record ID and the records, okay?

So if you remember he here I do insert update on the, so the new records, so this update happens and before and after, and these on delete and insert will be after on insert and after on digit, then this new map will be accessible. So when this all numbers seven, same thing, it's a ID and record of the old version of the records, all records, okay, so pointers are going to irrelevant. This is going to be available during update treasure and delete triggers. Okay. So at that moment, we can assess this hold up anybody before update after update before delete, after delete all the time, we'll have the show Okay. Hold on.

Okay, so now understood how to access the records. So now Sometime. Let me explain one logic here. I wrote this trigger, okay? And I'm just writing one speak last year. Okay?

Let's see my account logic is one class okay? And I wrote all the logics inside this class okay? Now, I'm just going to invoke this class from here okay. And also this class being used in some ways maybe in some other class, other other other places, okay. So, now, I want in this logic, I want to check is this logic invoked from a trigger or not? If it is not invoked from a trigger, then I may do some other activity able to invoke from a trigger then I want to do some other activity in how will understand this class is being invoked from a trigger.

So that moment we can see if my triggers exhibiting or not. There is one context variable Who would you call is exhibiting? Okay, would you return a true or false depending on the current code executing from a trigger or not, the mind code can be invoked from a trigger can be invoked from a web service, or a mail service, or maybe Visualforce page, or maybe my anonymous window. So I want to take this piece of code being invoked from a trigger or not, the time we can use is executing. Okay. So now, on this trigger, right now, in this page, I'm just going to manipulate one record.

So I can contain the records in a list collection of records. And I can insert you could remember, we're going to insert in accounts 10 contracts. So when I write it triggered, and I mean second 10 accounts are linked. So I do I want to see how many records are executing in a trigger. So if you want that kind of thing, we have something called site we can use site. Okay, we're gonna do all the all the stuffs So now so to use this, remember EPA want to check degree interesting not I will use insert, but always remember you just write this in test like this trigger dot age insert or not is this way okay?

Trigger dot each update or not that okay, when I do do this practically with a jumbo you will understand what is happening below any can confusion or question for me on new map, you have said that we can use it on after insert before update, but it can't be used for before insert right because it has not been created yet. Okay. Okay. So now see this is the theoretical explanation of the triggers. Okay, then what we'll do, we'll write different kinds of triggers. Okay.

We'll have a lot of continuation for in writing trigger, what is the best practices? We'll do some hands on practice for the triggers, okay. So just when I said the syntax and what are the background of the trigger to you see if I don't know the context variable and all the things I cannot date. So, then in the next session we will write the trigger will identify six triggers, okay, so that you will be acquainted with this practice how to do it. Okay. Then we'll discuss the best practices right this is a very very common thing for developer to use.

We will use always although or we will go use a lot of triggers to be there. A lot of logic here. So this is like myself first classes added from here. Okay. Three other things any confusion? No.

Perfect. Sorry, but the thing is, can we call the After insert from the before insert, after insert before insert, the table after insert is false, okay, but you have a class which you want to use both in before incident after the insert that it can do, let's say this class or you want to use both in after insert and after the differential then what to do? He, I'm just writing a syntax for you here, okay. If I write trigger.is before okay. And desert eater, so you want to execute both in different chapter, then you can write this, okay, I can invoke ecologic invoking and we get it this project can be used in voting, but we have to consider some water parameter we're passing in before an insert and after insert in the new one old map, we are different for it. So if you You want to pass the world map in the book the time, then before insert, I don't have old map it may fail.

So when you do that, you are considered to consider which parameter to pass into passing any parameters. Okay, you can invoke the logic in the both events, but with taking care of the logics, okay? Okay, perfect. So we'll do a lot of hands on for this trigger on this the core part of c++, don't worry for it, and go through this continuation like, part of contents variables, if you can remember it, it's very good to remember. Okay, although you have to play around with it. And they see these triggers in tests everything and in the next session, we'll write the only the core trigger code will lead to small triggers and the best practices will make the complete structure.

Okay. Then we'll meet in the next lesson. Thank you. Thank you so much. Okay

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.