Trigger Example

Salesforce Development Using Apex Get Started with Apex Triggers
51 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
€47.51
List Price:  €66.52
You save:  €19
£39.57
List Price:  £55.40
You save:  £15.83
CA$69.83
List Price:  CA$97.77
You save:  CA$27.93
A$76.76
List Price:  A$107.48
You save:  A$30.71
S$67.19
List Price:  S$94.07
You save:  S$26.88
HK$389.07
List Price:  HK$544.73
You save:  HK$155.66
CHF 44.17
List Price:  CHF 61.85
You save:  CHF 17.67
NOK kr552.55
List Price:  NOK kr773.61
You save:  NOK kr221.06
DKK kr354.40
List Price:  DKK kr496.19
You save:  DKK kr141.78
NZ$85.21
List Price:  NZ$119.31
You save:  NZ$34.09
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.46
৳5,978.91
List Price:  ৳8,370.95
You save:  ৳2,392.04
₹4,225.77
List Price:  ₹5,916.42
You save:  ₹1,690.64
RM223.20
List Price:  RM312.50
You save:  RM89.30
₦84,012.69
List Price:  ₦117,624.49
You save:  ₦33,611.80
₨13,908.54
List Price:  ₨19,473.08
You save:  ₨5,564.53
฿1,733.10
List Price:  ฿2,426.48
You save:  ฿693.38
₺1,727.24
List Price:  ₺2,418.28
You save:  ₺691.03
B$290.46
List Price:  B$406.67
You save:  B$116.21
R906.09
List Price:  R1,268.60
You save:  R362.51
Лв92.91
List Price:  Лв130.09
You save:  Лв37.17
₩70,046.43
List Price:  ₩98,070.61
You save:  ₩28,024.17
₪186.68
List Price:  ₪261.37
You save:  ₪74.69
₱2,952.60
List Price:  ₱4,133.88
You save:  ₱1,181.28
¥7,718.58
List Price:  ¥10,806.63
You save:  ¥3,088.05
MX$1,017.02
List Price:  MX$1,423.91
You save:  MX$406.89
QR182.44
List Price:  QR255.44
You save:  QR72.99
P683.49
List Price:  P956.94
You save:  P273.45
KSh6,461.20
List Price:  KSh9,046.20
You save:  KSh2,585
E£2,482.03
List Price:  E£3,475.04
You save:  E£993.01
ብር6,237.86
List Price:  ብር8,733.50
You save:  ብር2,495.64
Kz45,607.24
List Price:  Kz63,853.79
You save:  Kz18,246.54
CLP$48,681.26
List Price:  CLP$68,157.66
You save:  CLP$19,476.40
CN¥362.01
List Price:  CN¥506.84
You save:  CN¥144.83
RD$3,017.61
List Price:  RD$4,224.90
You save:  RD$1,207.28
DA6,678.15
List Price:  DA9,349.94
You save:  DA2,671.79
FJ$113.48
List Price:  FJ$158.89
You save:  FJ$45.40
Q386.23
List Price:  Q540.76
You save:  Q154.52
GY$10,467.47
List Price:  GY$14,655.30
You save:  GY$4,187.82
ISK kr6,913.61
List Price:  ISK kr9,679.61
You save:  ISK kr2,766
DH499.81
List Price:  DH699.78
You save:  DH199.96
L910.32
List Price:  L1,274.52
You save:  L364.20
ден2,923.74
List Price:  ден4,093.48
You save:  ден1,169.73
MOP$401.12
List Price:  MOP$561.60
You save:  MOP$160.48
N$907.46
List Price:  N$1,270.52
You save:  N$363.05
C$1,841.10
List Price:  C$2,577.69
You save:  C$736.58
रु6,753.23
List Price:  रु9,455.06
You save:  रु2,701.83
S/189.86
List Price:  S/265.82
You save:  S/75.96
K201.41
List Price:  K281.99
You save:  K80.58
SAR187.67
List Price:  SAR262.75
You save:  SAR75.08
ZK1,384.62
List Price:  ZK1,938.58
You save:  ZK553.95
L236.44
List Price:  L331.04
You save:  L94.59
Kč1,203.80
List Price:  Kč1,685.42
You save:  Kč481.62
Ft19,537.53
List Price:  Ft27,354.11
You save:  Ft7,816.57
SEK kr551.39
List Price:  SEK kr771.99
You save:  SEK kr220.60
ARS$50,126.56
List Price:  ARS$70,181.20
You save:  ARS$20,054.63
Bs346.46
List Price:  Bs485.08
You save:  Bs138.61
COP$220,634.58
List Price:  COP$308,906.07
You save:  COP$88,271.48
₡25,407.49
List Price:  ₡35,572.52
You save:  ₡10,165.02
L1,264.30
List Price:  L1,770.13
You save:  L505.82
₲391,827.47
List Price:  ₲548,589.81
You save:  ₲156,762.34
$U2,138.81
List Price:  $U2,994.50
You save:  $U855.69
zł206.57
List Price:  zł289.22
You save:  zł82.64
Already have an account? Log In

Transcript

This conference will now be recorded okay. So till now we have discussed the part of the program like during the for audit like from continuing from the previous session. So, we discussed okay I want to create a new list of content or I want to update the contact lists I think what I want to delete my number of employees belly changing account, if you remember, if I go to the eponymous speakerphone, let me open the conference just to give the background knowledge of that program or deed in the previous session CPF one this account in the account I have one field which I call it number of employees. number of employees so now, right now number of employees I have five so in the system I could have five contract. So in a modified to seven, it will be seven minutes trying to so I'm adding extra to the page modifier two, three, that means three minus five, it is nothing but minus two.

So I have to delete two contracts. So like any two contract I'll pick on a little to understand the program only. So now to take the count of number of employees are added or removed need to be done, okay? The difference of Hello from the previous hello to current value so in my old record, so if this is the record and I want to modify the data, okay, let me open this in my another tab. Okay, in database I have this file right now I just go and click on it, I might even have the employees equal to seven. So now you can see my new record having the value seven and old record having the value Hi.

Okay. So now, how to get the old record Hello, I just said using contest variable, trigger dot old map. So triggered an old map nothing but it is going to contain a map of the values Key ID is the ID and value of a subject record. So, I pass the ID in the get method. So, I got that account record completely then DOT number of employees. So, this part returns us exactly in the old record, how many number of employees we had.

So nothing but this value we got. So, out of this value, what I do now, currently the SEC record which is nothing but it is new in that court I have entered. So this value the new value minus old value, if you see, we did that. This is the number of employees minus world Hello number of employees. What do we do? That is nothing but employee count.

So based on that criteria, there's no I in then what I did, my as I'm writing this code bulkhead is not one of them. I may do many accounts manipulation at a time. I may do a Data Loader or any activity where I I can insert 10 accounts or maybe 100 accounts, maybe thousands of accounts or any number. So that time also my code should be able to handle it. Okay? So when it we see I have this account for each account, I take the number of value what value I have.

Okay, so right now I'm going to add the records. I see. So this is nothing what I'm seeing on this part, this account ID is two and I got the number if this number is positive, then I have to add, so seven minus five equal to that means I have got two accounts. So then I'll run the for loop and I'll add that contest in condition. So before to that the conditions would be, if I'm just writing that part. If the number of employee change is greater than zero, that means I have to add a new contact.

It may be one or two depending on the difference. Well, this is one word. So if it's not greater than zero that means if it is less than zero so II see, if it is less than zero, that means less I'm putting in the else part is CP zero then I am not going to do anything if it is the value is not changed and you will be exactly the difference is zero. So that means there is no changes in the value before it was five now also five, so five minutes I withdrew, before it was five, now it is four, hi, four minus five nothing but minus one. So, if it is minus, then what we'll do, so we'll delete the contents. So now elsif the Hello in the map or that account the change of the number of employees if less than zero, then what I do we have to do something to delete, okay.

Delete the content. Okay, so how many continuing to delete we need to delete the number of content what we are See, this is completely insert the follow. So always will avoid to do it in a circle stuckman insert follow to always be elaborate to write any, any kind of database operations, insert a follow, which may be, so kill, it may be insert, or maybe update. So if you're writing for loop, so what will happen, so each time you go and hit database for each that isn't inserted what to do. So now I'll just take a count for each account. Okay, party this negative value.

So now if you see a counter ID I have a number of employees I have. So for all these things, I have to query on database to get the influencers. So now, this party if you see, I wanted to write inside my follow. Okay, so simply what I'm going to do here, I'm going to just take this key set contents all the values but I need to get only the Id see I just take another set of IDs Let's account ID to delete contact. Okay. new set of ID we just did that.

So now wherever I have greater than less than zero, I am just adding that account by ID on this list also the on the set. Now see there is a petition with the positive or negative, if it's positive, then we need this part. If it is negative, I'm just giving this part here. So what is my account ID for this okay. So these Okay, so nothing but this key certificate in Mockingbird ik CCD, this is the ID I just kept it. Now I know from this part, so the set contains the number of the account list, which need to be added.

So now I will carry on a cold Korean contact To get existing code let me just scroll down to get existing to get existing to query on contract to get existing contract in the system. contract to be deleted. Okay, now if I do that, so before that I'll check if this account ID is set what I've declared if it is have some value greater than zero, then I'll go under File the query. So I just had a list of contacts. Con list equal to so this is normal SQL statement, select ID. I don't need anything solid ID from contract.

Okay, what is my condition or your account ID in this set? Okay. So now I got my contract. But out of this content or account, let me query account ID field also here. For account one, I may delete two content per account, three, I may delete one content, or maybe account and I may delete high content. So far, I'm going to delete all these contracts, but I just call it all the contract for which account Hello number of employees decreased.

So based on which account have to delete for which account how many contract has filled, we have to build this on this logic what I have on this map, which means it is hidden number of employees. When you say go here, see here. Now I'll just move on this map again. Okay. See, we can optimize it more nicely. So this logic you can take to the top and I can again, come back here, but instead of that what I'm doing, I'm just writing Everything our displays right now.

Okay, so now how many contact I need to delete that contacts list I'll just keep here under declaring another list of contact okay. So, let's I get the name of contact delete list equal to new list of contact. Now, when a caller ID is the police account now I have this contact now for this contact, I will check. So, on this account ID who is contact we have an all these parties a number of employee very decrease. So anyhow to use follow. Let me see for this contract con.

I have the call list I look on each contract. For now I will see for this account or which country I'm taking. So for this account, what are the contracts I need to fill how many contracts I need to delete, okay, the total that put in what I need to do now I have to keep this in mouth. So for each account how many contracts I quoted in that way, there's just multiple EP we don't think globalization and what I do is simply island to anything, just to to make you understand. Simply I can put here Okay, limit I can give this trailer for this me map name this map just to make you understand this dot values if I gave this one it'll return me exactly the number of different strangle just for under just for concept just see if I do this one delete understand this then I'll show you how you can go for the most stuff okay?

So I delete here okay I missed here something okay then as I'm not going to this part okay in your heart I knew this one. I just kept the semicolon here. Let me So let us why do we have so digit correlation? So correlation is defeated by the politics of inequality is total open I have somewhere also defined this longest in 30 countries to delete Okay, so I'm not doing anything I'm just writing a very simple code here okay I completed everything this I selected Okay. Go to these underline no 77 disappeared Okay. So now this value also let me take from here so this.if you remember the map methods to get the value from Pottery do we get as a string okay as a mellows method if you see this icon number employee map, you can go to this map corner employment app it contains employee ID and the count the string and integer.

So now if we come here, so on This is a list currency list it becomes a list, but here on this limit, I will get a Jackie the integer value. So see, the expression must be type of integer. So now on this fellas, I may get many, but I'm just going to zero because I'm not doing calculation here right now, okay, I'm just doing the simple simple way. So, ineligible equation, so, that time we can manage the least, but as I am not writing this code in bulk if I do it, but you will see our all work you know, like all the test cases for to do now it will work but for the bulk records it will fail is when that now I created this video delete. So now on this part, I have maintained one list per contact to be inserted. So this is a part of deletion.

So let's before that, let me I'll check if anything can Justin I will you Insert the content. So contract insert and delete cannot be combined in a single column we have something insert an object that I can combine using offset, but delete and insert cannot be combined Okay. So now I see you see, so if this column is what I have, if it is having some value, then apart I will do, I will go and insert it. Okay. So, to make to understand I'll put many debug lines so that you will understand this this this one. So now, I will do the opposite event, not insert the past I'll cheat, I'll check participant event okay.

So and also how to debug the code. Okay. So now I am writing I am inserted account trigger. Okay, so on the account trigger event, partial check participa Isn't he I sometimes I may not go to By the interpret apprenticeship so let's check. What is the event reading? It's already went or not.

Okay. And one more thing in line number three that symbol which is coming Is it because we have a comprise the lines there? Okay, okay. Okay. Okay. Now after, so I'll show you what is the Hello coming for this, then we can we can.

So initially what a lot better I have declared one map. So a particular reason I'll show you, we don't have any value for this. Let me print it. Okay, so I'm just printing all the values so you can see the flow how the system flows? Okay, we know what is the value of this? Is there any error?

So when you debug, you can find out how to resolve the issues. We have to debug the code. So you should be expert in debugging the code. Now how many accounts do you have? So we'll see four With the account we have not seen now I'm going to print each account. Okay?

So account and we'll print exactly the health like I need this relative not everything good. So now let's continue about this journal. So my new village what is I have? parties may already lose the tender condition to compare it so you can see the data is going inside or not. You know, I'm just putting the same thing here so that you would be willing to steal it because you understood here, then we may compensate. If we come in third, then I just want to show you, let's process everything here.

So when you came out, what is the value of this? So this is number of employees that map what we have. Okay, we'll see them our vendors. We make a difference or not. That's fine. We'll see what's up Then if that value is there so how many times am going inside this loop or node and for which account I'm going and what are the controls I need to create just printing here criminalist so many debug lenses not Gordon system like in production but to understand to debug the code we can keep the debugging and we can check but it's not good practice to write so many debug lines Okay.

So now, let me keep one more line here to see what is the value of this account ID Okay, this will work coming for each account. Okay. Okay till we do this one. So mind control active So now, if you see right now in the content, I have 400 Okay, let's just modify these. Let me just comment this line of insert a little insert one record. Now, let me save it.

Again, I'll come back and rollback this one because right before this logic was not there. So in the contracts, I have four contracts with value each five, let's think it is save as it is. Okay? So I'm just going back, just on committed. So now, in the system, I have exactly four contacts. Okay.

And if you see, the number of location also calls for contacts, and the number of employees is your support. So let me show you how you can put a debug line, I just go to setup. And from setup setup, I just go into debug logs. Okay, I'm just trying to put debug log on the major. I'm just going to integrate it. So I just edited Okay, I just said they will look for one over the scene, what are whatever I'm doing, everything will be logged in, everything will be logged.

Okay? So I have a log level which I call a subjective console. I just kept this name it see in this log level Courtney to filter department to attract everyone depends. Okay, so I just clicked this I can save. Okay, so we'll see what logs are the whole logs I'm going to do I am reading all the old logs. I don't have anything No, for example, and get it done.

There is no old log. Now I'll go. I'll just edit this contract. Instead of four. I'm going to make six anything to make Bankole may not work. If not works so good.

We'll see how to debug how to fix the issue. And if it works, stealers, I'll show the debug log. What is happening? I click this. I don't know what happened. CODEL triggered something happened.

So Now we'll see what is happening in my new content manually insert also or maybe insert Okay. Now I came here. Now if you see the log, first thing you see, so account result each update true. The chapter two so is going in update trigger in after event. So now see number of employee map is empty now. Okay, so what new wouldn't value I'm giving I'm giving new value a six right?

And what is my old value old value is four. So now for this account so new healthcare six card so now according to our employee, get his six or see the difference here. So now we get here to visit old papers for new map is six. So but my map and then six but we expect it to condense six minus four to my card. line number 61. Let's go there.

Okay, in line number six To recap this one third see if there is a difference the number of employees not normal then we check if all value is not null then we come here number of employees minus old number of employees that is going to be my employee count okay that that I want but see maybe by mistake I said again here employee county colu sec number of employees read this letter kept somewhere sometime that means this code will set exactly to now for me, so, six minus four it should be said to but again when I came what happened implicant vegenaise had sex you saw here this purse So, this is the extra quad quad we had caught my point. Yeah. So, we remove this we just come in this one so we don't need this one logically. Okay, then. Okay. So past we got the issue would be talking here, here.

See, then we're trying to insert six hunters, but why not? Sources contest. What do you see? Under the contract? I have a little four, no six. So I insert the content.

But if you see the code, we didn't drag that contract to the second the content con passionate, we set it and we insert, but content inserted if you see that contents are not tagged under this account, but see, so in the contact ID and set account field, so if you debug then you can see what kind of errors are getting the system how to be. So you can put a glance always see how to conduct Manuel de colo, my SSID simply see, got it. I just saved it. So now we switch if you see our debug level you understood Okay, since k which is further creating, inserting, but if you see in the system, I don't earn anything. Let's go and do it again. Okay, so now already six.

So forget, forget it. So what we'll do here I'm just going to comment this ones, which I don't um, let me show you let me solve. Okay, so I'm not going to insert anything now, before also for now. So before I just I'm just trying to match what was my old record based on that same legacy notional working, Lisa, this motion non negative value in Okay, some kind of arrow came for a decrease the value system could accept some least most of non negative. Okay, I'll fix this letter. Okay, then what we'll do here, let me manually get to contentious just to make the match.

Let's see, I just give Manuel Khan one. I'm just kidding. My knowledge has given him so now you can see under this account. I have no high contacts now I'm going to create one more contract manual Ctrl C i have not this much duplicate old defenders Fine. I'll just to save it not. So now see, which I have no.

So what I'll do now on this part if we go to this account now I have six contracts defined here and my number of employees value is six. Now let's go and see let me go and delete all the developers what I had before. Okay, now we'll see what is happening we'll see in the boys Okay, so now on the account I go and modify the account value from six to eight so I'm expecting two new contact to be inserted under this image they go on field said everything was fine now. So we don't know How many contract came there See? Yeah, to contract instead of just one to test one one as per main code, or even again, you go, anybody paid number of location to something else? employee eight to 10.

Now see, I'll have 10 contracts. Okay, so one part of code if you see it's working now 10 contract. Okay. So now if you see here, the debug debug logs, you can find each line by line, how it works. So eight to 10. One a deed, see my previous relu, my old Hello.

So number 10, and eight. Now it's four these are coded to, we got four these are going to create two content and inserted two contracts indirectly. That is logic. But now, let's do now. Now let me go. We'll see it may not work.

We'll see that or we'll fix it. We'll debug it. We'll see how to debug. So 10 was there. I'm just modifying to six Now I go on pizza. See some big error system, no refunds.

If you see these error messages is showing on this trigger on line number 89 column one very general whatever the query Axelsson limit must be in non negative angle. So if you need to see, I just kept the harmony number of record like not to handle the bulk records simply to control that. I said how many records to pitch, but this value contents and negative value so limit never takes a negative value. So what I can do here, I just declare your integer. Okay? Let the number of contact deleted equal to so this will look what I had with index zero.

I'm taking that here. Okay. So this is nothing but one negative a positive. So now, you you will think okay, how I'll convert this to positive Hello. So don't worry, we have many functions. Simply go and search math in epics mathematical functions for the year, which is a math class.

So positive negative absolute. These are all mathematical functions. So also many functions are defined in certainties. You see I'm not saying anytime remember all these methods. Okay, I think you remember going to walk you in remote. So you see, Do I have anything which work for you see there is some so many absolute nonsense returns the absolute value of decimal quality integer.

If you go here, see if you mentioned 42. If you use absolute return me first even, okay, so that means I need this, this smells absolutely. If I go here, if I use math absolute. So you need to know what we're going to do, what method we have, we can find out in the developer resource. I just did this was not so long ago and save again. Not Sorry, I've already paired here but Hello I should reference you will get triggered save this trigger now it wants it.

So, I modified from Tennessee say something else can see did it fail processors on DVDs whatever deleted this who knows some cases. So, some quiet spelling there are still few cases referred to this content for deleting. So, what I will do I will try to delete the content what I am inserting okay. So that right now this contract is empty contents okay if you go to this contact So, the new contents what we have inserted this guest contents that will integrate these consider officially to so many cases and everything. So as for a system we cannot delete so I won't delete the context to start with just one. So that means I want to get connected with account already matches.

Okay and name like Remember name? So what name I'm giving? Yes? If any name starts with this, delete that only. Okay? So see how the query for to learn how to use one by one.

So now if you go here, okay, I'll go on Save do the same search again not modifying anything I just go to save it, I saved it. So, now you can see how I had before 10 I modified to six. Now see how many Could I have now? exactly six seconds. So if deleted, so, as for my requirement it worked right. So but whenever I am going to manipulate multiple record, so can my trigger handle it?

No, my trigger say this part we did Welcome to the server you can do how many records No problem, but this part within the bulkhead, we just taking the index you do nothing but my constructor. So we have to write this as a bulky position also. Okay, so for each account how many contiguity direct that By doing it, so in that one how to do, okay, I will either keep in in the middle like this, okay? I'm just going all the all the contacts associated to that accounts which need to be deleted. Okay? We take that there.

Now we'll see what to do first. Okay. So how many contacts came and how what is the account to? set we have. Okay, so that's it. We'll look now, okay?

Integers. See I'll keep the number of let's see my account ID con nothing to it Dave. Okay, account will convert I kept all the IDs, let's just move with ID, the ID. So each account ID I'm taking, okay, I'll see what other contracts we have associated and we'll delete it. So now if you see here Each account ID two and what Budo is I can do simply what I'll do here, let me take the ID here. So now this is my account ID for each account ID go.

Now, what else do I need to get? Exactly that number of contacts. So I would another for loop here. Okay. So see many many nested for loops are not good in program once again, okay, we can write a good map to handle it. So now, I'm just writing another contact.

So I have a list of contacts here. So from there I have to get in Detlef which contract to delete. So now if we go here, and I process from the east contact, see sorry. Okay to delete. So what do we know? So, this contract whatever how many contracts I need to take, I need to take, depending on how many accounts I have, okay?

For each account What number is defined on this map, if you see account number of employment app it'll depend I need only that many contacts for that account okay? Not more than that. So, if you see here integers nothing about this line, I am just concentrating on this line in this integer number of contact to delete for this account, so what I do so from this map, okay, dot gate account ready for this account ID, how many that I have, okay, I wanted that many, okay. So now I go to for each account, okay, now know what I have to delete that contract only until figure in the map is the logic more logic and mode, okay? So I'm going to add that context here. Okay?

So, see them so now, but I have to restrict once I achieved how many to deleted. So let's see for each time when you come initially this come here I have some yellow, that's the moment this number of content of delete greater than zero, okay, till that day I will do on each time you go, you just do minus Okay, I'll explain this logically now. So now I'm going to do it from account before I had considered Six, I just modified to four. So, what is my difference now, my difference will be now to correct that means to contact to be deleted, as per our logic. So how we handled. So if you see these sec con map whatever I have, okay, this is the number of employee map, it contains What?

So it contains my account ID constantly one and hello is minus two. Okay. And now we will query up the contents for this one. So now in this year, I pass some contract with a counter ID in these a name like this. So, now, these call lists to delete what we get from these from these, we get how many contents we get maybe 10 contracts and last, but I don't want to delete all the contracts, how many contract I ended up getting to So, I went for each account for which I want to delete the content for that account how many contacts you deleted I took the number here okay. So that I kept us right so i got from here to so now, from this contact, I lose on each list okay, I have this contact listed I love on it then right now values two, two is greater than zero yes then are in the list to deleted okay then again I came to my diet No, here we had two then we came, we did minus one now, then again is this loop loop continued.

So now again, hello became one as a modifier here, one greater than zero, then I came out a second element operator, then again I did minus one is zero, not zero greater than June not funds. That means this list contains now exactly two contracts for that account. Now in the end, I came and they going to delete that context okay. So if everything works like like this way and then you come and delete the contents, so II my contact delete list dot size greater than zero then I have something then go on delete it Okay, this is a concrete logic Okay, so let me see what else okay missing statements on here okay line hundred days missing semicolon Samira Mr semicolon Let me see you very Mr. mccullen okay in lines Okay Okay, so now this is normal people so it will work for any number of account you process, okay how many number of contracts to be deleted or to be processed, everything will work.

So now if you want to test this policy You have to do your data upload, okay. So bulk of records will do at a time, then we can see. Let me see now I have six I just joined, I just met him why single records working or not to check I just joined and capsular see 12. So six plus six record will be inserted. We come here, contact Normal Form tool. So, when I have to Well, I just go here, I just can complete to eight.

So, for record I want to delete. So, if you come here, now I see in the contract, I haven't get laid, okay, are these two laptop test content? Now that's coded so that what you're doing here, okay, so this is one small scenario, okay. So if we'll forget the logic. Okay, what we did here completely, I'm just writing a operability event, where I'm using a contrast variables, new map, old map and a certain measurement Adding the elements in a state and map and list and manipulating them to delete or insert, okay. Then based on this option we do depend logics.

One logic we wrote here to get the number of difference number of contracts to be deleted insert that we kept in this map. Okay, so now once we kept it, then we, if it's a positive value, then we go to create new contact with negative value, then we are for whose account to be deleted, then we can query the contact here. Okay. Then for each account, how many two digit we already in the list? And I already did magic. Okay.

See everyone have different logic. This logic can be more of dementia payment more optimization, you'll be more confused. Okay. below. Let me know if you have any more questions. So it will delete random contacts, right?

So out of the existing one, so it will pick any one of those and it's We'll build it and validate. Yeah, it will take right now to pick up. Anyone from this condition match. Again, my name is test, it would delete that, if I'm going to give four but I don't have any any contact with neighbors test then it will not tell it how many contacts I have neighbors Yes. So see what will happen. So which contract to be delivered?

Basically that conditions will be given. Okay. Right now, as I have just long dummy conditions, I kept this one. Okay, so you can also specify in the query that what all contacts Yeah, we want to get deleted. Likewise, the oldest one from the existing list or kind of thing, okay. Correct.

Okay. So in that way, so, whenever we're ready to go, we'll do like this. But see, like, whenever you work in Samia, you will not you will not see too, right. It's a very small requirement. And you see, we wrote the logic of 109 lines of code. Nobody is there.

Okay? But see if you like these in my account trigger, I may have so many logics. And in the end, if we come up to writing all the logic in this trigger only, then imagine how much long this trigger is going to be. Okay? And it's so much longer than how we're going to handle it. So basically what we do, we follow design buttons which are not taught right now.

Okay. But the simplest way you can create one handler class and all the logic whatever I have here, everything will move to handler class. So what I do here is go and create a new class. Okay, let's the neck give the name as our current triggered handlers. Okay, this is main class now. So here, I am writing one method.

Okay. Let's see public mode. passed. I'll move my differential logic Why'd you do here? We just check some duplicate secret industry that I had to negotiate. So now public wired duplicate check one method I wrote and I wrote another method public via let's update number of employees to a method or a vote here.

Okay, so I'll bring the logic here. So now to these four they have no I have nothing at least favorite always bulk vacation. So I wrote a list of account is a list. Let's give the name as newest this list and because I am going to my number that in the differ only. So now here, I wrote this, I made a map map of Add a new sec map and map of ID column. All this is enough this isn't.

So now the concrete logic whatever I have written here, if you see what I would have written here, I'm just taking the company logic code from here. And then keeping here normally we'll write past here, then we'll refer in trigger but to make you understand I brought everything to here. Okay, so triggered a new I don't need now the new account lists for you see here, this variable party party will pass from my trigger. So despite I don't so during the class, I cannot refer my trigger dot new I know nothing. I'll get into that new right now. It's just a class.

Okay. So same way let me save this test. So now, I will take this piece of code again. Understand this copying everything here Ctrl x and I came here Okay, so what changes I need to do here now. So wherever I have trigger neum trigger reference that will change to these variables for Now see, if you let me save, then we'll get the error message whatever you get, okay? In number of employees to get an old map, so simply, I am referring this old icon now.

I'm just sending it okay. So wherever I have old map let me search. See, here's a world map. Just go and motivate or map. So always the logics will write in a utility class. luckless if you don't follow any pattern also Okay, so do we have a new map Let me check again I have one called New with new record new record nothing but my new book on added values.

See, now I'm just going to save it what line number 37 so you here also have something called old map Okay, the Bible does not exist. number of employees or less is in map dot get sec.id same way here also dot get id.id Okay, so noisy in the class we don't refer anything Trigger context variable. So what we'll do here, we'll pass everything as one reference to this method what we have. So, now, let me fix this, all the errors first of all this is map dot get visa map. So, I want to get this into account ID. So, the account ID is the key here, I just said get a secondary number of employees.

So, now my class is going to be saved, line 41 is fixed and entering the class, let me see. So now, all the logical we move to the IRS and how I mean book did that class. So simply, I have to create an instance of this class that means nothing. I have to create object for this class that 20 for this one So, now if we go here to pass I create one object I give the name as handler equal to new account holder handle the class then I'll invoke the class logic. So, if during insert what I do handler dot past method nothing but this duplicate check I came here duplicate check, I need to pass what my trigger dot new okay. So, before I left for everything is not new here.

So, I passed the reference please go to new nothing but a list of account. Now, see, I have the parameter list of the same way you pay competence but what I do here, handler dot What is my method name, my method name object number of employees, for example. So I just write here what I want to pass two parameters. First, we got a new map and trigger dot old love. I passed in all the logic. I moved from the Before that this is optional in an opening take some time let me save it was it not saved Do we need going into DSPs Let me see you can it's glad somebody served or not here in the backend yes already Mike my them in class.

The class name is account trigger handler. If you see you see no complete logic Okay, this one not came Okay. Let me copy this hold it right here. So always whenever writing a trigger, create a class write the logic Then that logic invoke on this place tomorrow you want to use them piece of code somewhere else, you can directly invoke on so there is some lockup and we are in a position to finish this trick and let us know this. Okay. Let me do one second.

Sometimes yourself a server is a some resource being used, Okay, it looks for any other options and so it's some parallel operations. So maybe for the design stuff or something, you can show them the sandwiches and loved and the changes requested the code itself for.com temporary lock off admission. So that will say no. So I'm just keeping this court right now on this this place. Okay, this is a simple, simple way to separate my trigger and make class. Okay, nothing, nothing.

There's nothing we did. So I just create a class. And I report that by creating the instance. Okay, let me know if you have any question on this. Whenever we just create a class that needs to be referred in, trigger, is it necessary to give that handler name to it? Or it just Oh, yes.

Okay, so the PVC Yeah, in our in my production as well. I see many classes we are in they have written handler. So I thought it's a kind of, is it a rule or is it a practice to write and It surprises me because he is this class right now. Okay, I can see that we got a controller inserted handler. So let's see, I don't know in my class in May or I have many classes 1000 classes. So certainly you see the class call accountant random.

That means Understand this is a handler class who is being used in account trigger eval the give this name as okay update my account let's consider then can you understand what is the purpose of this class you cannot Yes. So, origin for the best practice right that account render. So, they are now named on Wi Fi What is your need? Okay. So, you go on you see the if you see any trigger in your personal Okay, you will see looks like this. So, there may be some other design button okay.

If not, you will see the quarterly some hundred class and then the in bogies, then in the class, there will be the main logic okay it may be utility class from this class we can invoke another class so that we can generically use the functionality in many places. So, like see if duplicate check is a thing which would need on this account trigger. If I have a page or something here I want to create this account They are also getting multiple physical so you can invoke this to this logic. So there is no image and you can make use of the functionality. Okay? So logically any confusion logical is like see.

So the court any confusion you got here the let me know, then we'll close this. No, I'm okay with the logic because I have to practice while I start working on it definitely. I'll stuck at some places because I'll be writing it for the first time. But I can refer the code which you have written and then I'll be able to clear my doubts, but for now I'm okay. I'll have any questions and I will let you bring that in. Yeah.

So. So, tomorrow Do we have class on Monday? Tomorrow, I think we'll have the class. We'll have the Any change in plan the live message message? Okay, man, thanks. I'm trying to have flexibility so that we can complete them quickly.

Okay, okay. Thank you. Okay. Thank you so much. Hello, baby. Have a nice day.

You too.

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.