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 Жыл бұрын
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 Жыл бұрын
@@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
@swapnilbawaskar958312 күн бұрын
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
@sudharrsunnd311311 күн бұрын
Thanks a lot bro! This trigger playlist is really helpful.
@swapnilbawaskar554111 ай бұрын
Very nicely explained bro...
@sfdcninjas10 ай бұрын
Thanks bro
@oindrisen1932 Жыл бұрын
Sir, Can you confirm the reason why we use "Contacts" in line number 56?
@diobrando125311 ай бұрын
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_journey2 ай бұрын
it's get interesting when you try to make the trigger logic-less by creating a trigger handler class and the trigger separately
@sagarikabhelkar3477 Жыл бұрын
Will you please show a few examples using the helper class as well?
@sfdcninjas Жыл бұрын
Sure Sagarika
@sagarbora77689 ай бұрын
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).
@rohitbhatia23944 ай бұрын
to check if the returned records are there in list or it is empty
@nitinnainwal2 жыл бұрын
Thanks for this apex trigger series.
@sfdcninjas2 жыл бұрын
Glad you like this series. More scenarios coming🙂
@sheetalsharma16758 ай бұрын
clear show nhi ho rhi vedio
@SeekingSmiles236 Жыл бұрын
Can we use aggregate result in this trigger to get no of contacts on Account?
@sfdcninjas Жыл бұрын
Yes we can use and sorry for late reply
@SeekingSmiles236 Жыл бұрын
@@sfdcninjas ok. Thanks for reply
@BalajiBasanolla-jj6em Жыл бұрын
I want this trigger with Handler and how to call trigger.new,trigger.oldmap,trigger.old in trigger class
@diobrando125311 ай бұрын
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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@sfdcninjas thank you so much
@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 Жыл бұрын
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 Жыл бұрын
@@sfdcninjas Thank you for your response
@sfdcninjas Жыл бұрын
Anytime buddy
@SeekingSmiles236 Жыл бұрын
Can you make a video on recurve trigger??
@sfdcninjas Жыл бұрын
Sure i will create a video on it don’t worry and sorry for late reply
@SunnySingh-o7z Жыл бұрын
Don't you think we should avoid so many for loops in the trigger?
@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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@sfdcninjas thank you so much for your reply
@sfdcninjas Жыл бұрын
No worry Ankit i am always open for your queries😉
@shivmandirlisari288710 ай бұрын
good
@sfdcninjas10 ай бұрын
Thanks
@asmitamathur2595 Жыл бұрын
The custom field on account is a rollup summary field ? I guess we cant use rollup summary field in trigger
@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 Жыл бұрын
Sir can u please provide handler class for this?
@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 Жыл бұрын
Please use light background to record videos.
@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 Жыл бұрын
Could you please help me why we store the accountId in set from "accIds.add(trigger.oldMap.get(con.Id).AccoutId"
@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.