Apex Triggers - 5 (Roll up Summary Trigger)

Поділитися
Вставка
  • Опубліковано 24 гру 2022
  • 💻 Join the Titan Community : Discover Exclusive Insights on LinkedIn / mycompany
    💻 Explore the Power of Titan : Visit Our Official Website Now 🔗 titandxp.com/
  • Наука та технологія

КОМЕНТАРІ • 44

  • @lrm7244
    @lrm7244 Рік тому +3

    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  Рік тому +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 Рік тому

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

  • @sagarbora7768
    @sagarbora7768 5 місяців тому +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).

  • @nitinnainwal
    @nitinnainwal Рік тому +2

    Thanks for this apex trigger series.

    • @sfdcninjas
      @sfdcninjas  Рік тому +1

      Glad you like this series. More scenarios coming🙂

  • @swapnilbawaskar5541
    @swapnilbawaskar5541 6 місяців тому

    Very nicely explained bro...

  • @shivmandirlisari2887
    @shivmandirlisari2887 5 місяців тому

    good

  • @oindrisen1932
    @oindrisen1932 9 місяців тому +1

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

    • @diobrando1253
      @diobrando1253 6 місяців тому +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.

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

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

  • @sagarikabhelkar3477
    @sagarikabhelkar3477 Рік тому +1

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

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

  • @sheetalsharma1675
    @sheetalsharma1675 3 місяці тому

    clear show nhi ho rhi vedio

  • @BalajiBasanolla-jj6em
    @BalajiBasanolla-jj6em 8 місяців тому

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

    • @diobrando1253
      @diobrando1253 6 місяців тому

      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

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

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

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

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

  • @tejalmeshram6534
    @tejalmeshram6534 Рік тому

    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);
      }

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