Apex Triggers - 5 (Roll up Summary Trigger)

  Рет қаралды 23,901

SFDC Ninja

SFDC Ninja

Күн бұрын

Пікірлер: 48
@lrm7244
@lrm7244 Жыл бұрын
Hi EveryOne Instead of This Long Trigger you can also Try this Trigger but Make sure the Field Count_Child__c on Account Object should have Data Type NUMBER otherwise you will get Error of cannot convert string to integer trigger NinjaScenarioNoFive on Contact (after insert,after update,after delete,after undelete) { //Rollup Summary Trigger set accountIdSet = new Set(); if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate || trigger.isUndelete)){ for(Contact con : trigger.new) { if(String.isNotBlank(con.AccountId)) { accountIdSet.add(con.AccountId); } } } if(trigger.isAfter && (trigger.isUpdate || trigger.isdelete)) { for(Contact con : trigger.old) { if(String.isNotBlank(con.AccountId)) { accountIdSet.add(con.AccountId); } } } Map accMap = new Map(); if(!accountIdSet.isEmpty()){ for(Account acc : [Select Id,Count_Child__c,(Select Id from Contacts) from Account where Id IN:accountIdSet]) { acc.Count_Child__c = acc.Contacts.Size(); accMap.put(acc.id,acc); } } update accMap.values(); }
@sfdcninjas
@sfdcninjas Жыл бұрын
Hi Buddy, thanks for sharing this but i think it is not a good practice to use inner soql correct me if i am wrong
@lrm7244
@lrm7244 Жыл бұрын
​@@sfdcninjas Hi no problem be can take a List of Account outside with same query and we will iterate that same list it will work Same as Earlier
@swapnilbawaskar9583
@swapnilbawaskar9583 12 күн бұрын
can we use aggegrate function in line no 56 i.e. count aggegrate funcction to store the count ? if possible, could you pls paste the querry.. will be really helpful
@sudharrsunnd3113
@sudharrsunnd3113 11 күн бұрын
Thanks a lot bro! This trigger playlist is really helpful.
@swapnilbawaskar5541
@swapnilbawaskar5541 11 ай бұрын
Very nicely explained bro...
@sfdcninjas
@sfdcninjas 10 ай бұрын
Thanks bro
@oindrisen1932
@oindrisen1932 Жыл бұрын
Sir, Can you confirm the reason why we use "Contacts" in line number 56?
@diobrando1253
@diobrando1253 11 ай бұрын
its inner query and whenever we fetch child records based on parent records we have to use the Relationship name. If you check in contact object you can see its relationship name.
@opeyemi_salesforce_journey
@opeyemi_salesforce_journey 2 ай бұрын
it's get interesting when you try to make the trigger logic-less by creating a trigger handler class and the trigger separately
@sagarikabhelkar3477
@sagarikabhelkar3477 Жыл бұрын
Will you please show a few examples using the helper class as well?
@sfdcninjas
@sfdcninjas Жыл бұрын
Sure Sagarika
@sagarbora7768
@sagarbora7768 9 ай бұрын
Hey buddy, your video was comprehensive!! I wanted to ask why have you kept null check on Trigger.old.isEmpty() inside if(Trigger.isAfter && Trigger.isDelete).
@rohitbhatia2394
@rohitbhatia2394 4 ай бұрын
to check if the returned records are there in list or it is empty
@nitinnainwal
@nitinnainwal 2 жыл бұрын
Thanks for this apex trigger series.
@sfdcninjas
@sfdcninjas 2 жыл бұрын
Glad you like this series. More scenarios coming🙂
@sheetalsharma1675
@sheetalsharma1675 8 ай бұрын
clear show nhi ho rhi vedio
@SeekingSmiles236
@SeekingSmiles236 Жыл бұрын
Can we use aggregate result in this trigger to get no of contacts on Account?
@sfdcninjas
@sfdcninjas Жыл бұрын
Yes we can use and sorry for late reply
@SeekingSmiles236
@SeekingSmiles236 Жыл бұрын
@@sfdcninjas ok. Thanks for reply
@BalajiBasanolla-jj6em
@BalajiBasanolla-jj6em Жыл бұрын
I want this trigger with Handler and how to call trigger.new,trigger.oldmap,trigger.old in trigger class
@diobrando1253
@diobrando1253 11 ай бұрын
trigger.new is list of records of a object (here list of contact records) and trigger.oldmap is map of id, record trigger.old is also list as well when u write logic in your class give list as method parameters and call those methods in trigger
@shubhamsri.1895
@shubhamsri.1895 Жыл бұрын
Hello Sir, Can u please explain in After Update events (second loop) why we are adding both old Account Id as well as New Account Id in Set? And while updating how old contact count get minus and it added to new contact count?
@sfdcninjas
@sfdcninjas Жыл бұрын
Hello Shubham Sri, In the After Update events (second loop), both the old Account Id and the new Account Id are added to the Set for the following reason: The purpose of this trigger is to update the "Number_of_Contacts__c" field on the related Account record whenever a Contact record is inserted, updated, deleted, or undeleted. In the case of an update, the trigger needs to consider both the old and new values of the Account Id for each Contact record. The trigger compares the old and new Account Ids using the condition if (conObj.AccountId != trigger.oldMap.get(conObj.Id).AccountId). If the old Account Id is not null and different from the new Account Id, it means the Contact has been moved from one Account to another. In this scenario, both the old Account Id and the new Account Id need to be added to the parentAccIds set. Adding both old and new Account Ids ensures that the trigger will update the "Number_of_Contacts__c" field for both the old and new Accounts affected by the Contact record update. Regarding your question about how the contact count gets updated, the trigger retrieves a list of Accounts related to the Contacts in the parentAccIds set: List acctList = [Select Id, Number_of_Contacts__c, (Select Id from Contacts) from Account where Id IN : parentAccIds]; Then, for each Account in the acctList, the trigger updates the "Number_of_Contacts__c" field with the count of associated Contact records: acc.Number_of_Contacts__c = acc.Contacts.size(); This means the contact count is obtained by counting the number of Contact records associated with each Account. The updated Account records are stored in the accountsToUpdate list and subsequently updated in the database. I hope this explanation clarifies your questions. Let me know if you have any further doubts or concerns!
@shubhamsri.1895
@shubhamsri.1895 Жыл бұрын
@@sfdcninjas thank you so much
@manasapalle480
@manasapalle480 Жыл бұрын
Hi sir once we check both new accountid and old account I’d for a contact it means they have a value right, what is the purpose of 27th line and 31st line
@sfdcninjas
@sfdcninjas Жыл бұрын
Hi buddy, apologies for the delayed response. When storing a field value or iterating over a list in Apex, it is always a best practice to apply null checks. This helps prevent null pointer exceptions in our code.
@manasapalle480
@manasapalle480 Жыл бұрын
@@sfdcninjas Thank you for your response
@sfdcninjas
@sfdcninjas Жыл бұрын
Anytime buddy
@SeekingSmiles236
@SeekingSmiles236 Жыл бұрын
Can you make a video on recurve trigger??
@sfdcninjas
@sfdcninjas Жыл бұрын
Sure i will create a video on it don’t worry and sorry for late reply
@SunnySingh-o7z
@SunnySingh-o7z Жыл бұрын
Don't you think we should avoid so many for loops in the trigger?
@sfdcninjas
@sfdcninjas Жыл бұрын
Hi Sunny ,Yes, we can optimize the code by using maps instead of multiple for loops. However, it's important to note that using maps can add complexity to the code, especially for beginners. This playlist aims to help people understand the approach to writing triggers and keep things simple. In the future, there is a plan to start an advanced trigger scenario series which will cover more complex scenarios and advanced techniques.
@ankitdangre4790
@ankitdangre4790 Жыл бұрын
Sir when im performing delete operation im getting this error "There's a problem saving this record. You might not have permission to edit it, or it might have been deleted or archived. Contact your administrator for help." plz help on above error
@sfdcninjas
@sfdcninjas Жыл бұрын
Hi Ankit, there could be two reasons 1. Check your permissions : Ensure that you have the correct. persmissions to delete the record 2. Check if the record is already deleted : verify that the record you are trying to delete has not already been deleted. If the record has been deleted you will not be able to delete it again
@ankitdangre4790
@ankitdangre4790 Жыл бұрын
​@@sfdcninjas thank you so much for your reply
@sfdcninjas
@sfdcninjas Жыл бұрын
No worry Ankit i am always open for your queries😉
@shivmandirlisari2887
@shivmandirlisari2887 10 ай бұрын
good
@sfdcninjas
@sfdcninjas 10 ай бұрын
Thanks
@asmitamathur2595
@asmitamathur2595 Жыл бұрын
The custom field on account is a rollup summary field ? I guess we cant use rollup summary field in trigger
@sfdcninjas
@sfdcninjas Жыл бұрын
Hi Asmita, number_of_contacts__c on account is not a roll up summary field its a number field in which we are storing totle number of contacts related to an account.
@tejalmeshram6534
@tejalmeshram6534 Жыл бұрын
Sir can u please provide handler class for this?
@sfdcninjas
@sfdcninjas Жыл бұрын
please try this code , public class ContactTriggerHandler { public static void countContacts(List newList, Map newMap, List oldList, Map oldMap) { Set parentAccIds = new Set(); if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUndelete)) { if (!newList.isEmpty()) { for (Contact conObj : newList) { if (conObj.AccountId != null) { parentAccIds.add(conObj.AccountId); } } } } if (Trigger.isAfter && Trigger.isUpdate) { if (!newList.isEmpty()) { for (Contact conObj : newList) { if (conObj.AccountId != oldMap.get(conObj.Id).AccountId) { if (oldMap.get(conObj.Id).AccountId != null) { parentAccIds.add(oldMap.get(conObj.Id).AccountId); } if (conObj.AccountId != null) { parentAccIds.add(conObj.AccountId); } } } } } if (Trigger.isAfter && Trigger.isDelete) { if (!oldList.isEmpty()) { for (Contact conObj : oldList) { if (conObj.AccountId != null) { parentAccIds.add(conObj.AccountId); } } } } if (!parentAccIds.isEmpty()) { List acctList = [SELECT Id, Number_of_Contacts__c, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :parentAccIds]; List accountsToUpdate = new List(); if (!acctList.isEmpty()) { for (Account acc : acctList) { acc.Number_of_Contacts__c = acc.Contacts.size(); accountsToUpdate.add(acc); } } if (!accountsToUpdate.isEmpty()) { try { update accountsToUpdate; } catch (DmlException e) { System.debug('An error has occurred: ' + e.getMessage()); } } } } } trigger countContacts on Contact (after Insert, after Update, after Delete, after Undelete) { ContactTriggerHandler.countContacts(Trigger.new, Trigger.newMap, Trigger.old, Trigger.oldMap); }
@Ankhenatan
@Ankhenatan Жыл бұрын
Please use light background to record videos.
@sfdcninjas
@sfdcninjas Жыл бұрын
Thank you for suggestion, actually I have consistently incorporated a lighter background in all of my recent videos you can check them out . (From 7th scenario till now) to be exact.
@GudduKumar-l9k9h
@GudduKumar-l9k9h Жыл бұрын
Could you please help me why we store the accountId in set from "accIds.add(trigger.oldMap.get(con.Id).AccoutId"
@sfdcninjas
@sfdcninjas Жыл бұрын
Hi Guddu , this is for the situation when a user change the parent account of a contact in that case we have to update count on both New Parent account and old parent account and for that we have to fetch id of both new and old parent account that's why we are storing old account id in our set.
Apex Triggers  - 6 (Prevent Duplication of Records)
8:19
SFDC Ninja
Рет қаралды 17 М.
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 53 МЛН
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 18 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Apex Triggers - 31 (Infosys Interview Trigger Scenario)
16:38
SFDC Ninja
Рет қаралды 9 М.
RollUp Summary Apex Trigger
26:59
Procodingskills
Рет қаралды 10 М.
Apex Trigger (Rollup Summary with Apex Trigger)
16:15
Salesforce Techbook
Рет қаралды 10 М.
Apex Triggers - 57 (Trigger Interview Scenario)
14:30
SFDC Ninja
Рет қаралды 1,2 М.
Real time Scenario: Custom Rollup Summaries using TRIGGERS
1:04:55
Salesforce Exclusive
Рет қаралды 41 М.
What are Roll-up Summary Fields in Salesforce? | How to create them?
52:53