Trigger Example and usage of Aggregate query

Salesforce Development Using Apex Get Started with Apex Triggers
55 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.10
List Price:  €67.35
You save:  €19.24
£39.88
List Price:  £55.83
You save:  £15.95
CA$71.97
List Price:  CA$100.76
You save:  CA$28.79
A$80.16
List Price:  A$112.23
You save:  A$32.07
S$67.96
List Price:  S$95.14
You save:  S$27.18
HK$388.27
List Price:  HK$543.62
You save:  HK$155.34
CHF 45.02
List Price:  CHF 63.04
You save:  CHF 18.01
NOK kr569.59
List Price:  NOK kr797.48
You save:  NOK kr227.88
DKK kr358.90
List Price:  DKK kr502.49
You save:  DKK kr143.58
NZ$88.63
List Price:  NZ$124.10
You save:  NZ$35.46
د.إ183.61
List Price:  د.إ257.07
You save:  د.إ73.45
৳5,972.19
List Price:  ৳8,361.55
You save:  ৳2,389.35
₹4,258.89
List Price:  ₹5,962.79
You save:  ₹1,703.89
RM224.30
List Price:  RM314.04
You save:  RM89.74
₦77,378.02
List Price:  ₦108,335.42
You save:  ₦30,957.40
₨13,924.71
List Price:  ₨19,495.71
You save:  ₨5,570.99
฿1,707.62
List Price:  ฿2,390.81
You save:  ฿683.18
₺1,762.36
List Price:  ₺2,467.44
You save:  ₺705.08
B$310.08
List Price:  B$434.14
You save:  B$124.06
R926.14
List Price:  R1,296.68
You save:  R370.53
Лв94.05
List Price:  Лв131.68
You save:  Лв37.62
₩72,995.06
List Price:  ₩102,198.93
You save:  ₩29,203.86
₪183.24
List Price:  ₪256.56
You save:  ₪73.31
₱2,921.84
List Price:  ₱4,090.81
You save:  ₱1,168.96
¥7,853.11
List Price:  ¥10,994.99
You save:  ¥3,141.87
MX$1,006.35
List Price:  MX$1,408.98
You save:  MX$402.62
QR182.19
List Price:  QR255.09
You save:  QR72.89
P691.26
List Price:  P967.82
You save:  P276.56
KSh6,461.20
List Price:  KSh9,046.20
You save:  KSh2,585
E£2,544.49
List Price:  E£3,562.49
You save:  E£1,018
ብር6,361.91
List Price:  ብር8,907.19
You save:  ብር2,545.27
Kz45,590.88
List Price:  Kz63,830.88
You save:  Kz18,240
CLP$49,514.09
List Price:  CLP$69,323.69
You save:  CLP$19,809.60
CN¥364.84
List Price:  CN¥510.80
You save:  CN¥145.96
RD$3,032.23
List Price:  RD$4,245.36
You save:  RD$1,213.13
DA6,760.37
List Price:  DA9,465.07
You save:  DA2,704.69
FJ$115.98
List Price:  FJ$162.39
You save:  FJ$46.40
Q385.05
List Price:  Q539.11
You save:  Q154.05
GY$10,455.66
List Price:  GY$14,638.77
You save:  GY$4,183.10
ISK kr6,980.10
List Price:  ISK kr9,772.70
You save:  ISK kr2,792.60
DH502.81
List Price:  DH703.98
You save:  DH201.16
L922.32
List Price:  L1,291.33
You save:  L369
ден2,959.81
List Price:  ден4,143.97
You save:  ден1,184.16
MOP$399.96
List Price:  MOP$559.97
You save:  MOP$160.01
N$920.98
List Price:  N$1,289.45
You save:  N$368.46
C$1,839.04
List Price:  C$2,574.80
You save:  C$735.76
रु6,810.12
List Price:  रु9,534.71
You save:  रु2,724.59
S/186.41
List Price:  S/260.99
You save:  S/74.57
K202.74
List Price:  K283.85
You save:  K81.11
SAR187.78
List Price:  SAR262.91
You save:  SAR75.12
ZK1,383.10
List Price:  ZK1,936.45
You save:  ZK553.35
L239.36
List Price:  L335.12
You save:  L95.76
Kč1,209.95
List Price:  Kč1,694.03
You save:  Kč484.08
Ft19,835.61
List Price:  Ft27,771.44
You save:  Ft7,935.83
SEK kr553.09
List Price:  SEK kr774.38
You save:  SEK kr221.28
ARS$51,277.48
List Price:  ARS$71,792.58
You save:  ARS$20,515.09
Bs346.08
List Price:  Bs484.55
You save:  Bs138.46
COP$219,391.32
List Price:  COP$307,165.41
You save:  COP$87,774.08
₡25,377.51
List Price:  ₡35,530.54
You save:  ₡10,153.03
L1,269.45
List Price:  L1,777.33
You save:  L507.88
₲390,156.27
List Price:  ₲546,250
You save:  ₲156,093.73
$U2,225.52
List Price:  $U3,115.91
You save:  $U890.38
zł205.57
List Price:  zł287.82
You save:  zł82.24
Already have an account? Log In

Transcript

This conference will now be recorded. So till the time of this test, what is trigger okay in that place? What are the trigger contest variable we have if you remember we have discussed all these terms. So, trigger if syntax, okay. And in the same time we discussed about the contrast variable like president neutral rate old or new map old map is insert each update or is before now we know what to do nothing but it's a piece of code which is executing on fessing the operations. And let me do let's see one scenario we're going to do, okay, now, consider the scenario going I'm going to insert a consider the scenario what I'm going to give now, it can be handled by configuration, we can do it but to understand the trigger, how to write how to invoke the trigger how to manually logic so I'm saying the simple simulation Which can be handled by workflow or approval or some configuration part, but still will write the code to understand the user consent.

Okay? So don't be confused, okay, it can be handled by configuration of why am I writing trigger? So that for that I'm just just saying it, it can be handled, but we'll write in code to understand it, okay. So, I wanted to go whenever somebody create an account, okay with a specific name, okay. First I will check if this account name already it already exists in my system or not. Okay, if they exist, then I will throw the errors.

Okay. So there are we have duplicate rules or anything we can use to manipulate maybe in some configuration by the look of also we can do too, we can do to achieve this functionality, but still, we'll write a few support to tip it. Okay, so now my requirement whenever somebody creating an account, or when updating the account, if the name is matching with the existing account, then will not allow to create it. That means I want to check if a duplicate account already exists in my system. If there then we won't allow to create with center that is my past trigger requirement. So now if you see these, so let me understand using one aside are you, okay?

Any tool like you're going to developer console or any tools like that I just released today you let me open this web up for my whiteboard. Let me see what we can do. So now financing I'm just going to create an account that go to this account page. Okay. We have so many accounts we have so now the woman to go to this account page. See I have one account So right now in system, I can go and I can create another account with the same name also, I can go here, or I can keep it.

So it's allowing me to create duplicate accounts with same name. So now what I do here, I will write a piece of code when somebody go and create a account, okay, go on create account with the same name, and then click the Save, it will not allow me to create it, it will show me the error message because there is already another account with same name exists on my system. It will not allow me so I want to do this kind of activity. So how we'll do we'll see, okay, no, just a minute. Okay, so now what I'm going to do here. So now, this account I have same way whenever I just go and clear this account.

So now, the moment I click the Search, so what am i It will check my code. So you can go here, nothing, but I am just making my user interface page. And this is my database. So now I just fill this form maybe account name or something I feel and then I click the Save button, the moment I click the Save button, what it what happened now are the equivalent, this is going to have some logic, okay to perform my DML on my account record, just to create a new account. So, now, you see the moment I do this. So, what will happen now, now, the moment I do this run these flows starting from here and going to here.

So this is a flow and I said in between these flow, there is one part where we have met it's the setpoint if you remember the precursor and we explained like this. So now before ship, or after sale, to part. Now I want one logic the moment in account you just click the Save button. So the data What am I going to serve it? So I want to check, do I have the same name already exist on my system or not? That means, before saving this account, I should check if I have same name in so the error message or the resave it so that means the logic part I'm going to write, I want to display on the, I want to execute under before event.

So, I want to write some logic which will be triggered before saving of my account. Right. Got my pointer, any confusion? Perfect. So now in the same way of what I'm going to do here, okay, so now See, this is about creation of account. CMA, if I go to this account, okay, I just want to modify existing okay before my account name was new account, but when I modify I just give the Name, which is existing, matching an existing name, for example, so if I allow you to click save, right now salon so now see in my system, I have exactly three icons with same name, but I want whenever you modify also account with from one name to another name, that time also I'll shake if that name matching with something and existing if there then we'll stop it.

Okay? Anything there is multiple ways to in configuration to control it, but we'll write a code to understand it. So now, whenever you go and write a trigger, what do you do even a standard object, go to setup? Okay, then type the object name on which record DML you want to execute this logic? Like I said, whenever you're inserting a counter or building account, then we'll check Okay, what I do here, insert or update or whatever do you do here crud. So now, during insert or update both operations, we want to have logic on which object I want to pass a logical account.

If you see here, if you go to these under the account, go down to the triggers go something called trigger is I have some material. So now I am going to create a trigger you can create like this year. Okay already 32nd trigger, I'm just going to here just I open the same account to the here on the Asada, Yo, I just open here is just another ID where you can write the code which is directly integrated to Salesforce, see, I just type account trigger which already exists so if not, if you want to create a new one from here, you can just click on the center recommend, okay, then you can go here you can see new trigger, you can do the shows you can give the name so as I have already created trigger, I'm just opening that okay. Now you see, if you see these classes to go whatever thing I can get it, but see this is some different orful appointed that me Open this or this or water Connect now.

Okay, so my affidavit pointed in some other org now so I login login so that it will connect to this. Okay, let me show you how I'm studying. So the moment you do after that you it can record then I make it into a sandbox or production buy they want I can just keep that I just did my username, I allow it to now see it's open. So now I have it real PL account trigger. I'll just open that trigger here then I'll read this as phycological that just wanted to make it open. See, to solve any problem, technical problem or any problem has to understand the problem statement, then break it into granular part of small small parts, then solve the small part in the end, it will solve the concrete problem.

So not same way if I do this one. I have my contract. So, passing with to say if you see in our country God asking for any we insert one part so passing I got to know the moment I do insert nothing but we know now that means my event going to do something called insert correct if my insert event then what I do then second thing go and check if the account name exists okay then I have to check account name exists that means, in my existing system Do I have any other account that means, I have to get the accounts from my system for name match Okay, if anything returned from this then that means I have something that I will say okay, if you have a book called design pattern, then do this thing that went fast will go to your library you will see Do I have any Whoops, didn't button in such there are so many books, but we will try to search the name and then better you get it the moment you get it, then you can do something else you don't get it.

Okay, look, I don't have this same thing. I'll go and search my account object. Okay, with this name. Do I have any account? If yes, then it will return how many accounts? Depending on that if I have more than zero account in existing, then I can say, I'm going to enter some account which is already there.

Correct. So if this happens, then what we'll do what we'll do now we'll go here. So I have mentioned all events right now I'm just removing all the events, okay, dummy events, and then we'll see what we can do. Okay, so I mentioned it before the insert. Now on this before insert, what we do here, so now I specify whenever I'm going to have this account, insert, what we'll do, let me remove this later. It's going big issue.

Let me Close these properties Okay. Now see this is my account trigger. Now I go here so now simple logic I just mentioned one event okay okay, they're fine I learned my logic. So passing what I want passing I will see for current account parties my name and one point always remember whenever you write a trigger always remember you have to write balki fi trigger. So, right now I am showing the logically explain you okay one account I'm inserting or manipulating. But Justin whenever you do a bulk upload whenever you are going to get thousands of records from national system and you're going to post that record to your system.

So you are going to put a collection of records to your database, like maybe a list of collection some a list of List of Count anything. So now when you do that kind of thing, so that may records so you're triggered handle how it can be done. So to make that always remember, always be sure you're writing bulky by trigger. So if you process one record, okay, how much research you're using? If you process one third and effort also, same number of resource you will use. That should be out a good question.

Okay, now, okay, I'm going to insert account passport I need I want to see the account which are inserting, okay, their names. So now in this place, I can insert one account, or I can insert a new account, or I may insert hundred records. So all the hundred names I need to have. Okay, now how will do so now, for each account. I'll take whatever names we have, we'll just go and pause it. So now what we'll do here, see.

So now If you see the contest variable, I have one contest variable who is called trigger dot new. If you remember this trigger this trigger dot new nothing but it is just a collection of new records as a significant system, okay, so what it contains, it contains a list of accounts right now. Okay, let's say give the name of new account list. Okay, so now for this account let's it may have let me explain with the example. So now I'm going to enter one account with name as ss, one account with name devices, one icon with name and in my database I have existing account with name as three with numerous devices with NEMA Smith okay with NEMA Dennis different names I have no, these are my existing economists. So I'm just a trained logic how to build.

Okay, so this is my new account what I'm going to insert. Okay. Now we'll call this our SIS record. Do you have any additional name? No. So philosopher, there's no error, but if you go to this devices name, there is a duplicate record, so it's not allowed.

So, if you go to a Sunni then there is no duplicate which would allow so now I should see Do I have any record with this next record? So for assays there is no existing recording monitors to process my using county Bureau for diversity, if you see Delta using record count of one person, if you see there is no equal zero. Okay, you can see the deviations. Okay. Now, in that bad, whatever, and so the IRS would throw for this one. For those who don't Whatever, correct now you should we put the point I'm doing the collection of records.

So how do we do? So now? I'm just writing for each record, okay? So for each account record, I go here sec, and I give the name as new account list. Okay, now, I want to add all the names in a collection. Let's see I just defined one collection with name I said okay, set of string, okay.

New ACC name, ultimate account name equals a new set of string Okay, if you see these new faces in them normal go here, then then I just call to our method, then I mean book, this is dotnet. So now, if let me say this No, no sec name is one variable which contains Since naming the collection variable which contains all this new records Okay, so contents SS contains the basis okay any any convenient in law in the program we know I said see how our agenda say please listen now, in our program I have all this personnel. So see to solve one statement one problem statement there are many logics dimension which logically paper okay now we have now I want to query I want to get my database if I have in database any account with this name okay. So, now I have to curry so now gate account with this name from the database Okay.

So now what we can do here there are many ways again Again you can do group by group by this name field and see how many count of records there or you can Take everything, then you do the count manually. Okay? Two ways you can do. Okay? So if you don't know go by functionality and what you do. So you just call everything that then count.

Okay? With these devices name, how many icon do you have? As its name? How many icon do you have that you can do? Or we can do the group by functionality, okay, with these records, how many records do you have? So you don't know group by features?

Let's see. Don't worry, anything you may know, you may know or not, if don't know, Google is the best resource to give you everything grouped by Account Name field. So, this is your second writing, okay, you can just get this, okay? How to go by number feel how to go by all the steps. So now we know do this route by it written what kind of data? Okay, so my SQL it is normally the list of records are a single record, or maybe interjecting so we're going to do group by sending us a pretty good initial query.

Okay, let me serve that. So another set point, I'll teach you this trigger. I'll just teach you all this logic how to each master query, how does relationship query from time to master and parent or child everything? Okay? How we are going to do that? There are there are many things of this new policy.

This is one type of go by the grouping of grouping by this portion name for maximum. So let's see if I go and write one. So creating Developer Console. Okay, so why am using two babies I don't want to do a lot of manual calculation. Okay, I want to write their best query for that. So everything will be handled.

So now see, I just write, select. Okay, let's see them. You'll see the same way. We'll go and implement how this flow by implementing Okay, select name, then let's see count from account, okay? Then grew by something like this just a small piece of code we wrote, okay, there is no filter nothing, just a small piece of code. Now I just go to nothing in your name count from account.

Okay? grew by let's see, for this count that I just received a ID C. Now you see for this account name, I have three records for this account name, I have one record. So if I query this one, then I can get it. So now I want to give you a condition also from our code. We're now In, okay, let's consider naming. Yes, of course, okay, or naming or shooting.

But now if anything mapping there it will come, maybe execute only this record is there. So this query will help me in that point to use this query 40 can do now. Let me go to here. Okay. So I'm just going to write this kind of query. So this group I'm going to use, what is the return type of my my sequel?

Okay. Let me see if you get any examples and you get to consensus. See, do you have any confusion? I won't see how you use this logic. Okay. See, I will have a session Okay, I'm writing globally and over.

You understood this statement, but tomorrow some other logical outcome you want to build, then where are the reasons You have to find out are Egypt correct? See the same thing I did? Nothing What did this call you? Right select ID and this Okay. So now, whenever do it will return me a good result. Okay, I just think I know nothing in program.

I don't know I'm just going to write as point you will start how you write so that if an issue come then you have to handle it not me. Okay, I see the issue right is I know some aggregate result will come. But how do we handle that it's not going to lead you straight it occurred or planning how we'll handle that kind of thing. Simply go here, in your digital interface, you will get complete sample course you can get so you can go to any blog or any finish anything specific key link notice for that, okay. You can do average count min max, some any kind of agreed. You can do it the same way.

I'm just going to see when they use this aggregate query part Return it return one is a record that will go back. Let me go to this piece of code into this one. What do you want? Same way, return me a list of a good result or a digit code, okay? Then they manipulate from here. Okay?

I am going to do the same You can also follow this salesperson ID query here. Okay. Same thing we are going to do. We I just copied this one. I'll just give my name. Okay.

I just doing the same activity here. Let's see this Okay, so now when I do this one, group by name and this name in Isaiah, so instead of this naming, I have my name in my set. Nothing but newer condom. I just between your condom so there's no reason why So let's see I'll say account name count result. Okay, so now, let's see, I'll just take a screenshot of this. This one is from the debug, which will help us to debug the issues.

Okay. Let's see, I'll just, I just follow on with on which class are triggered this type of statements. And what I want to print, if it's inside a method, I can give them method name also. So they can see okay, from this trigger, this is the manual data I want to see. I just save it. And now I carry some using some aggregate concern, where I quoted the name and the number of records in that way, I could, I think, but when you see the query will return me something like this.

Let me remove this one, so that I can see many records here. Here, if you go here, I can see all this things. So my map says my name and my account. Okay? So same query I use for interviews this query, I'll get this letter. Okay.

Whatever onyx token disrupted novella, I'm coming to this to say we select these from opportunity and we do everything. So then we came here aggregate results these select name contract account from account where name equal to this group by name, and I got this one. Now I know I got some individual It is nothing but at least up or it's a collection of array, political elements in whatever other symbol you can see. Or if you want to use another list, let's I just went to array and how we can manipulate this array. Whenever this kind of our accounts, they manipulate like this. For example, I don't know anything.

I'll just put a debug statements and I will see what is coming then based on that I'll decide what to write and what to do. Okay. Just trying to write the code as a witness. Okay, so my aggregate results in event of the data What else? Okay, now what you'll see from these for them getting the lead me to a diverse report. Okay?

So I just want you to be comfortable to like deprogram, not me the answer. I'm just referring some somewhere here and getting this data. I'll go and I'll just print it. Okay, so my past filament what I have I have nothing but nail Okay, let's see here don't get named doing here. Right Yes, please. So we'll see all the all the data that we have on our spending NYC we do something called get extras.

And you know, I'll explain about this. Let me check what we can do. Okay. The moment I go To create an account, okay, this code is going to invert. So if you see this account object to it pass trigger so I'm taking a lot of time to explain you already we can go a little faster I can trigger it is also active shutter. Now, when somebody says you have to debug like see I have kept some debug lens to see the outcome outcome of that.

Okay, what is my name coming? What is my account name? Rachel. Let me see. What are the account names, you have the new names for inserting everything I want to go straight. Okay, so now you see I just kept debug logs.

So I left when this debugging deep into the system. But for now, something I will system the debug debug statement, where I can print the message. So now, to set the debug log in the setup menu, just go on source debug log go to Under my intro there is something called debug log go to there then you can create respect okay trust flag nothing work let's see I just get it responds with a specific name let's see I will say SF DC debug flag okay. And this is not there is not existing So, I cannot get anything. So, I can go on period trust magnets you know nothing is there. So, what I can do I can create a plug.

So, there is what I have I can take it from here debug level that is equal debug level. Now, I came here see if I have something you can take or not okay I have it this one I just took this when they said they did consult and how which user or for police class or the police entity you want to trace the log. Okay. So now I want to trace the log for a specific user as a user or maybe automated process, maybe class a trigger. Right now I am going to do per user who is user, the current user logged in nothing but the message We're going to manipulate this activity and I want to log this from this time to this time, this is which way the time. Now I go on Save it.

So now between these timeframes what I said, if any activity I do that log will generate it because I said on the user level, whatever you do, on log will generate. Now see, I just go with this name, and I try to save the record. I click the new and I click save now, see right now to go as if nothing is hidden over. Now I go and click Save. And now I go to refresh this. There is some log file came here, I am just going to see this log file.

I click on View. Now we'll see what I can see. My name said contents this one, line number 15. I have a debug statement where you can see this. Okay, so, line number 10. I have a debug statement where I have this one.

Okay, if you see the line number In here, there is a system to debug newer content what it can do I just go here I in the log file, I just Ctrl F i got it okay my upon images. Now, I'll go to higher encoded then I got it as an I go to directory on line number two L, then you go in line number two M digest so kill statement will fire select name count from a content name equal to this group and name it return me a get one record, okay? So nothing but if you see in line number 12, which interpreted is a new one record and that record I printed here, if we go to line number 13, see we printed the results the name is nothing but these and express inequalities. And whenever I print the name, I got this one.

Then in a lineup when I print this the same way. I'm just going to print this one out. We'll see what is there. So same way, I just go and print this. Okay, here, don't get So if you see nothing but slowly think Well, no, the same time same thing I do again. Now, this statement going written me what going to tell me the number of count for this man?

See, I am just stressing it so much so that you can understand deeply. Okay? You don't want to depend on someone to write a query or to understand the logic. Okay, see this value can be very simple, but I want you understand each week. Now if you go here, see my name count input. So if anytime I get a name count greater than one in I will say it's error.

Okay. So, if you write these soy now see I got my period reserved and I got My Name Is this okay? Then what I can do okay you understood from this if my expression is greater than zero that means there is one record which is going to duplicate name one question Yes. So, what does the expression zero actually means when when do we use it only for the count or no eating nothing but, so, let me show you in a diverse level. Now, when you came these relish this value How to say that we call a person do So, from this aggregate record. So, for expression 0.2 passing one thing is name which we pass this one, okay.

So, after that the aggregate query I may have two digital sensors, okay. So, I can Give me another aggregate function also. So let's see average or net something. So password we represent as a percentage. Let me see, you know what essentially that's a nice question that you've caught it that we can use assuming it wasn't zero. Let me see kind of with something we use to express and I think what it denotes the order of magnitude fields so if i highly exerted fields, the past can be extremely as 01234 Okay, if you specify with Judo expression zero that means of past field okay.

And if there is no extremity alias, okay, so here it is the parsing it the same way to is to get this one. So, if there is no duplicate for any name, that means, all the records are no duplicates. If there is some error I got greater than zero that means that the duplicate records correct. Nothing but the score EPC everything 1102 clear that when this occurred, how So right now it's three. No, let's see, I want to enter one record on this one anyway. So if anytime I got this record having a one and I'm trying to insert once again, that means it is going to give me it's going to create a duplicate name.

So my results would be nothing with the name match. What's the name again? It should not have an email. Okay? So that means I want to give each of these a dot gate expression zero. Anytime greater than zero, that means I just don't want to do anything.

I'll just give it okay. So whatever the accounting process or not, we don't need so all the account I want to stop simply on the on the record, I can add on 82 of Central Yeah, I can tell all the curse we set as duplicate a limit or I can Insert specifically which account name have the two options let's see I want to set okay for these accounts only this record have a duplicate. So, you may want to set that. So this here I create a map okay map of string and count nothing What I can say integers let's see, I will say existing existing account count are extremely simple the army in the map and said also list everything record new map of string comma images. So now in this position and count what I do, even in time we are greater than zero. Then I go here with this thing name comm dot pass I'll put what is the name Nothing but my air dot get them if you see in the debug line we've got the name nothing but we got my account name and then the account we are going to put the thing sentence pass parameter I put the string nothing which return from the air dot get name and second parameter report dot gate and Swiss Angela so now I got this much okay now I saved it No I didn't stop I just said which account What?

Okay, let's say the Mr. cynical so asuni you used to be confusion. Let me know if any confusion on this line Are you okay? Okay, so he just one town where kept this much Hello, and the set. Okay? There's nothing interesting about value of objects from the time okay? Even one working point I do this here dot gate expression.

So it is a thing about returning me one object time. Okay? So now this object it. It's not a integer. So that would be converting Jesse this photo data coming from the multi object thing. Okay, these objects the average amount or whatever the thing to do is hit object size.

Okay, so now when I'm trying to convert to this integer, nothing happens. So I don't want to do anything. I just type the object type what type is available? I'll try to do it. See, there are many constraints which we are facing here. We'll try if we can solve a lot.

Okay. No, it is one thing to simply I go is time what record came. I'll just go here. I'll just put it okay. Getting sufficient data. So let's see in the map we can say one word does not exist in this initial output object object okay this is the person object I just gave a string object object.

So this aggregate query already returned me object type okay now I got this one. So now you see I got this addition permissions. Now, we can check what that I have. Now from this I understood Okay, this is my map, this is my map which contains my name and count Okay. Now on these Can I check if this map contains duplicate if this made this map however, hello, which record by being query inserting, if anything is greater than zero, can I say error message? So now we should see how to set error message.

Okay. And how to stop complete DML from this account okay. No any confusion? No. Okay, Ashish? No, nothing only to write this eight lines of code I just don't completely understand I don't 40 minutes.

But in this Meanwhile, I have explained what is the aggregate query? How to use this aggregate query and what is this expression. So, in future whenever I need to write, you will write that kind of logic, okay. If you don't write this in English for you what it can do simply can it follow one you can make plus plus plus count when you can, you can come to this program. But here we understood, okay, there is something happening here. Third, in that way, so, if we don't want all this stuff, simply, you can write 140 and do your own capitalism.

Okay. So now, but I can't what I'm inserting here. I'm inserting the account. Each account I'm taking here, we'll check if I have anything left out. So I'm just going to have this new account list. Okay, so in this account list, we'll see if any records, okay, from this map, if any records from this nap, we're getting.

Okay. So now again, we'll get a problem here, where I want to again, compare this object type to integer. So we need to do a proper type testing for that. Let me write it see the error message will fix it. So if you relative get error messages, how to fix it, same way in Google. So if this map dot let's see, contents key we use here.

So if this map contains my account name, that means I got this sec document. Okay, if there is any record came out okay. So then that means there is some account already exists with this name. If zero, then I don't get it. So anytime you see this query, I didn't get any name of zero. The moment I just call it this one.

Yes person to name I have given here. It didn't give me as soon as I could only give me his first report, whichever one. So who is not exist? I will not get the account. Anytime I have this name. That means there's a duplicate account.

Okay, we have this contents, then I want to solder to solder messes. We do sec dot Okay, let me I don't know. I'll show you. Okay. So how to solder illnesses, okay. So in a subject, it is nothing but my a solid record, a subject record our EP subject, we have a method, which you call others.

And what message you want to write. You can take it, let's go there. see something called hard adira you can go here see how to do it in trigger, you can do other. So for a specific record, I can go and I can do these are errors. So what I want to do there is let's say a string messes duplicate accounts are not allowed with the name. No see this is see I'm trying to save so I'll get error We'll see.

Okay, so what is also we'll try to solve, we didn't get any errors. Okay, now I'll try to create what are called with this new duplicate our calls are not allowed within this we got the message. If I just change something here, now I try to ServiceNow ticket. Let's see it's set. So, see the logic is quite simple when you when you break it when you write it, okay. So, we do nothing here see if a demo this debug statements with the code are kept in the map, then we check if you how I handle this in bulkhead.

See if I insert 20 accounts, it will check Okay, if there is any accounts are there or not to stop all or anything, so it can handle my 20 Records in the list. Okay, let's see all the account name I took here. I just had this query, I got it and everything I kept in map Okay. Then I go here and I just check for which account I have duplicate. Now. This is very simple code, correct.

Yeah. So any confusion? Anybody? any confusion? Tell me, Sophie. Past trigger you should not be confused okay.

So, if I can get into it if you go somewhere for this training if somebody right it is a very simple trigger pass hello world or change the places to doctor or maybe just create a contact something but see this program also we would like in a simpler but we should write in optimizer always okay. So, what are the simple over here see and always again depends you should write all time the code it should check how my system performance interview. So, what is the optimizer joy of this series? What is the simple royalties, so, see, instead of this what I can do under siding again same piece of code I'll just committed. So, I just write a for loop here okay. So, everybody will see when they will try to trigger they will write integer count equal to select all from Call for your name in this new account.

Okay, so many people write this when they can this is wrong logic but we not feel it at all. Okay? II count greater than zero. Okay, and then what I do here, I will go and I will just write one piece of vision statement. This is for my developer the session was there. So simply what I'll do here, so, my hammer you sec, new list new This is a list of bureau.rs I'm just going to do this way.

Let me write a message here. See this will also work right now. Based on Second logic, that is, let's say I wrote, okay, and you'll see, I'm just commenting out all this all this course now. So now we have this support situation to look simple. Okay? We can, again we can, I can close this trigger system, or I can close Okay, it's working.

So now if I go and I'll just do try to create an account with this Okay, go here. I just tried to do that on a distance it's Give me the credit. Now, I will create an account with this name and another name so select return error or not. So I want Okay, it should return only the error for this occurred but not for another liquidation or stock. How are you? Okay, so but in this case, we checked for everyone which they could have duplicate or not.

This is also one simple solution, but it is optimized. Is this going to handle your alternate views? No, it's not going to undo all changes. Here. So see, I can say let's see, whenever you want to. Let me roll back this.

I'm just commenting it out. So now if you see when we go and save this record that is completed. Let me save this you know, my era has changed with the name which Docker Hub this triplicate, okay, with this new FIFA 18 account I'm inserting that I can switch up which accounts are only duplicate, not others. Okay, that name all of them. I can display here. Okay, that kind of thing.

Getting my point. Yeah, I think we can do it in another simple way. just yet. Get all the names and put it in a string and Just use the fact loop and check one by one does the string which has all the names from the icon hat contains this this new name or not? Yes. So he definitely also absolutely correct.

Let me ask you one more question. Okay. Answering when you learn it, you in the in your system you have only 10 Records. Okay, the logic what are the disqualified or or what was it? You're saying? It's absolutely fine it was.

But see now just thing you have 1 million account in a system can query 1 million records in a system to process it each one by one input loop. So if you process each record in follow 1 million times, then how much processing time you are going to conjunct? No, no, no, I won't take. I won't go with 1 million accounts one by one. I'm saying just if you have 1 million records in your database You can do it by getting all the names just the names into the string will never intend for a new account or the account which exists in the system. So, the new ones will be like like the same like we put it in here as my new nail like you kept it in a list.

So that will be same and names which we have in the database all the coordinates we will get it into a string just a string the logical way to a Delta question. So all the account what I have already seen database I want to get a string. So now you will hire a software to get all the account name is this one mess on my system correct. So now I have 1 million accounts in the system. That means your query is going to return 1 million records to me and from there, you will take only the names during a point. So now what do you need to do?

You will fire select ID or name From account, okay, now let me see you on account here. Okay. Now I'll tell the pharmacy logic is not wrong. Okay, but see the Portman fix. Now you want this, then let's see from here you will see existing account name.rd you do then you have now it's easy dotnet crud you want to be good now this code is going to return how many records I have one minute upon it is going to return me 1 million records. So my follow going to equal how many times my for loop is going to do 1 million times.

But in this case, as you see in my current in this account, I have 10,000 records so you will not feel anything. But the moment you will be working in real time projects. You have 50,000 Records. I have one more scenario plan for this. So instead of querying 1 million records, just like the way you get it like we were we can put a clause like way Naming oh yeah name it and I don't even need the ID and we don't even have to put it in the account. Attribute we just have to put it in the some string because just I just got D Now I got this HP No, this is yet no, we don't need the map, just put it in a string.

We don't even need the account. No no then you have to do this with dotnet or whatever it is many reports and cannot do it. So this this will turn you obviously I'm not doing one record I'm doing many bulk records. This will return obviously, if matching admits in one record memory john 10 records you can record inserting, so you can have a duplicate a unit in Britain, all the accounts that you have to bring it then you can keep it safe. Don't map a list anyway. Okay, yeah.

Does it return the records or just the names? It returned the complete record, not in a complete record, but only named Hello, which means complete. Then besides the map, we can just put the string and just add on that name. Yeah. It's on the other scale in the theta.com dotnet. Now this year, yeah.

That's it. Okay. Now, what we'll do here, can you So, how many duplicate you have on this name? See on this query, what you wrote with these, we are not allowed with this name. Let's I can just write on the content here. There.

There are this many of duplicate records. Okay, I have this count. So from here, it says Go and just put this number in duplicate record, see for every problem that depends on the transcript. So can you do it okay according AI, the code you have right has a special feature like we can print the number of duplicates we have. But problem we, we are trying to, I mean, the solution we are trying to give doesn't require any, anything to display number of duplicates, right. So that no I just, yeah, that that's a pretty fancy for everyone.

There's multiple solutions. So now, it depends. Again, how has this for each going to execute? Okay? How many pornography are going to maintain, okay, how much processing time is going to consume and how this logic is going to be flexible Okay, to some new requirement to the IRS and how quickly you can maintain it that we'll see here also weekly one aggregate treasures, okay? You're also equally 111 so we'll same thing okay.

Now once you get again you will do the same for loop to check, they can have the same look and if you want to saw the error message in all accounts into the sample looks, okay, everything that same kind of thing, okay, how optimistic Are you are if you do the single query, it will be faster than these, okay? Every problem has many legends manage themselves. So, the way we write it, so I don't write a complex simple one okay do and complete, but if you go see the moment you see this piece of code, then you understood this is simple also you can do it, if we right from setting this one, then you will never understand how to read this one. Getting my point. So every problem or multiple solutions, right? Yes.

Australia in any condition. And a fish. No. Perfect. See, I'm not going more today. Okay, so I'll extend this logic for a bit time how it will work.

Okay? And then I'll see more few scenarios to write this code. Okay, we'll see if anybody likes to now bring before insert, and I'll try to do some logic in the outer event and I'll try to make some DML operations and we'll see how we're doing the manual David. Okay. So with this trigger tech, a lot of times has provided content support, okay, but it will give you the base, how to do everything. I hope nobody's confused.

Now, is that Okay, perfect. So I'm just putting this on today from now, and we'll see you next weekend. Okay. So devices please share the link of this particular record. You have recorded this session, right? Yes, he says yes.

So please share the link for that. Okay, thank you. Yep. Thank you, baby. Have a nice day. It Thank you.

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.