Trigger to count number of Contacts with an account
ฝัง
- เผยแพร่เมื่อ 25 ธ.ค. 2021
- #ProgrammingMadeEasy
Hello and welcome to Programming Made Easy. In this video we would be discussing more on Triggers. In the Previous video we talked about sibling trigger, how to create it and the best practices involved. In this video we would be discussing more on triggers and the trigger for today is to count the total contacts associated with an account in salesforce.
Trigger to count number of contacts associated with an account,
Display the total number of contacts on account detail page,
Count number of contacts on accounts using Trigger ,
How do I find the number of contacts associated with an account in Salesforce?
Count the Contacts in the related list of Account ,
Salesforce | How can we count number of contacts ,
How to use trigger to count number of contacts associated ,
Salesforce Apex: How to Track the Number of Contacts,
Trigger to update number of contacts associated with account,
Salesforce: Trigger to count total no of contacts in an account ,
Account Contact Trigger Scenario,
Trigger scenario(when account address is updated related contact address also updates)
Awesome Man. Without using Map you have solved this problem very well.
very helpful video sir . Thanks alot
Great 👍
Thanks Brother ..
Can you take this one in your next video. write a trigger to get the product count of opportunity and update count field on account
Sure will do
Can you also add after delete event
Good Code is Working
Haha
why you put update accounts(); on line no 32 which is outside the if block, you can put this on line no 30 ... ???
Yes we could have put that in line number 30.
very helpful video
Hello there, may i know why we are adding (Select Id from contacts) seperately in line 23, kindly guide I'm new in this domain
Hi Abhishek (Select Id from contacts) is used because we want the contacts of those accounts whose Id is present in our set. Basically this is an inner query and 1 to many relationship is being followed in here. This means that 1 account can have n number of contacts so If we want such type of thing we generally write this query. Since in here we were just concerned with the total contact size so only took Id , But if we have some other operation to do then we could have queried some other fields as last name etc all depending upon the scenario.
Hope you understood.
Thanks
@@ProgrammingMadeEasy99 Thank you very much bro that you gave time to reply and explained.
do you teach online also
Yes bro kindly mail me at programmingmadeeasy117@gmail.com
How to get the count along with existing records? It is working only for newly created ones.
It works when we contact is created
why we didnt created another list for storing the updated no of count vaues
You can do that also but always remember 1 thing that map is more capable than list both in terms of space time so try using that. This is a beginner friendly video but in future videos we would be going more deep into these things
@@ProgrammingMadeEasy99 thanks for the reply and valuable input sir...
My question was as on line number 32...you did update accounts directly.. but usually we create a list and addd the records which we have updated ( like updatelist.add(acc)) and then we update that list (line number 32)
But here you have directly updated .
So what is the concept behind this that we can do directly the way u did n when we need to take another list put all update records n the update as a whole
Very helpful video. can you provide the test class also for this?
Sure we would work upon this as well.
Why are we taking newlist and oldList? kindly explain
newList will contain new values and oldList will contain old values
can we do count Manually without using Inner query or aggregate query???? please reply
No
Assume the big picture let us say for 1 account you have 50000 contacts.then how would you approach that? So that's why automation is required
@@ProgrammingMadeEasy99 but with the help of inner Query we can get only 50000 records what about millions of records then this code will fail right?
Yes sir. That's why always try to assume the big picture as a key to scalable solution
Can we do this with a object with lookup relationship
Can you elaborate
this kinda functionality is done for look up relationships only, because in master detail relationship we have roll up summary to find the summary or count of the child records
The main aim of this video is to make you all understand how to think in Apex and moreover this is beneficial when you are giving your interviews as they generally give such type of scenarios to code in Apex.
we should also write the logic for the after delete and after undelete operations then only the solution will be a complete one
Yes correct. Actually this was just to give an Idea
@@ProgrammingMadeEasy99 ok bro
We would be posting entire solutions as well
@@ProgrammingMadeEasy99 Nice work , Bro though.❤️
Thanks for supporting
Can u provide us handler code?
Everything is there in this video
how to create a field in account boj (total contact)
If you want to create a field you can go to setup then object manager search account and create new field. You could specify any data type for that.
@@ProgrammingMadeEasy99 which data type (number) or (auto number)
Number field.
@@ProgrammingMadeEasy99 ok sir but can you explain wich formula can use
To get total contacts associated with an account you have to use code no formula is there for it.
Great 👍
Thanks alot maam