Apex Triggers - 5 (Roll up Summary Trigger)

แชร์
ฝัง

ความคิดเห็น • 46

  • @nitinnainwal
    @nitinnainwal ปีที่แล้ว +2

    Thanks for this apex trigger series.

    • @sfdcninjas
      @sfdcninjas  ปีที่แล้ว +1

      Glad you like this series. More scenarios coming🙂

  • @sagarbora7768
    @sagarbora7768 4 หลายเดือนก่อน +1

    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).

  • @swapnilbawaskar5541
    @swapnilbawaskar5541 6 หลายเดือนก่อน

    Very nicely explained bro...

    • @sfdcninjas
      @sfdcninjas  5 หลายเดือนก่อน

      Thanks bro

  • @lrm7244
    @lrm7244 ปีที่แล้ว +2

    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  11 หลายเดือนก่อน +1

      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 11 หลายเดือนก่อน

      ​@@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

  • @nripeshjoshi6884
    @nripeshjoshi6884 ปีที่แล้ว +1

    Thanks for this video but I have a question why we are not using before trigger here ?

    • @sfdcninjas
      @sfdcninjas  ปีที่แล้ว +2

      Hi Nripesh because on an account contacts can be counted only after it gets saved to database e.g. if you are using before trigger with insert then you can’t have id of that record which means you will not be able to count it because id value is not there in before trigger basically we are checking count of contacts after every operation means when the record’s changes saved to database (AFTER record gets saved) after operations like insert, update , delete or undelete.

  • @oindrisen1932
    @oindrisen1932 8 หลายเดือนก่อน +1

    Sir, Can you confirm the reason why we use "Contacts" in line number 56?

    • @diobrando1253
      @diobrando1253 5 หลายเดือนก่อน +1

      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.

  • @sagarikabhelkar3477
    @sagarikabhelkar3477 ปีที่แล้ว +1

    Will you please show a few examples using the helper class as well?

  • @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  ปีที่แล้ว +5

      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

  • @SeekingSmiles236
    @SeekingSmiles236 10 หลายเดือนก่อน

    Can you make a video on recurve trigger??

    • @sfdcninjas
      @sfdcninjas  10 หลายเดือนก่อน +1

      Sure i will create a video on it don’t worry and sorry for late reply

  • @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

  • @user-we9tl2ys1y
    @user-we9tl2ys1y ปีที่แล้ว

    Don't you think we should avoid so many for loops in the trigger?

    • @sfdcninjas
      @sfdcninjas  ปีที่แล้ว +2

      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.

  • @SeekingSmiles236
    @SeekingSmiles236 10 หลายเดือนก่อน

    Can we use aggregate result in this trigger to get no of contacts on Account?

    • @sfdcninjas
      @sfdcninjas  10 หลายเดือนก่อน +1

      Yes we can use and sorry for late reply

    • @SeekingSmiles236
      @SeekingSmiles236 10 หลายเดือนก่อน +1

      @@sfdcninjas ok. Thanks for reply

  • @shivmandirlisari2887
    @shivmandirlisari2887 5 หลายเดือนก่อน

    good

    • @sfdcninjas
      @sfdcninjas  5 หลายเดือนก่อน

      Thanks

  • @BalajiBasanolla-jj6em
    @BalajiBasanolla-jj6em 7 หลายเดือนก่อน

    I want this trigger with Handler and how to call trigger.new,trigger.oldmap,trigger.old in trigger class

    • @diobrando1253
      @diobrando1253 5 หลายเดือนก่อน

      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

  • @sheetalsharma1675
    @sheetalsharma1675 3 หลายเดือนก่อน

    clear show nhi ho rhi vedio

  • @asmitamathur2595
    @asmitamathur2595 10 หลายเดือนก่อน

    The custom field on account is a rollup summary field ? I guess we cant use rollup summary field in trigger

    • @sfdcninjas
      @sfdcninjas  10 หลายเดือนก่อน

      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.

  • @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  ปีที่แล้ว +1

      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😉

  • @tejalmeshram6534
    @tejalmeshram6534 11 หลายเดือนก่อน

    Sir can u please provide handler class for this?

    • @sfdcninjas
      @sfdcninjas  11 หลายเดือนก่อน +1

      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.

  • @user-qv3hd1tw1u
    @user-qv3hd1tw1u ปีที่แล้ว

    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.