Apex Triggers - 4 (Update Parent by Child)

แชร์
ฝัง

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

  • @infantremi408
    @infantremi408 16 วันที่ผ่านมา

    Hi,
    I guess there is an issue in this snippet, In the second for loop you were iterating the trigger.new again but the accMap was created for only the records where the related account Id is not null and description should be updated.
    So, if a contact record is updated without any change on the description, we will face an issue in line 26 as KeyNotExists I guess.
    However we can overcome this by adding a containsKey() in line 26.
    Thanks!

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

    Thank you so much sir

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

    Amazing example! Thanks!

  • @SARKAR00007
    @SARKAR00007 ปีที่แล้ว +4

    Nice but I think the question itself not very clear. account may have multiple contact then which contact description it should update. trigger.new() may have multiple contact related to one account then it will override the description each time.
    if someone reparent the account or edit the contact and remove the account selection, then it will not be able to handle it.

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

      Hi Soumendu thanks for your comment.I appreciate your feedback. See it is a very simple example for beginners to demonstrate how we can update parent by child. In case of reparenting account we need to fetch and store both old parent and new parent account ids in set which i have used and explained in my further videos.

  • @srinivasmutyala9362
    @srinivasmutyala9362 ปีที่แล้ว +5

    Amazing.. and useful content for live coding in SFDC Interviews....In the above example what if single account has multiple contacts???

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

      Good Question.
      I think the Account Description must be same as Contact whose description will be updated at last,

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

      Thank you for your support.
      Coming to your question This trigger will update the description field of an Account record with the Description of its most recently updated related Contact record.

    • @simmiagrawal6763
      @simmiagrawal6763 7 หลายเดือนก่อน

      @stdcninjas Very good, at every needed point you are adding null check. Good practice

  • @user-bo4tf2ro3x
    @user-bo4tf2ro3x ปีที่แล้ว +2

    Hi,
    Thank you very much for your lectures.
    I have a small doubt could you please explain it.
    what if an account contains multiple contacts associated with it, which contact description is going to be in account description filed.

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

      Hi Sabareesh in that case the latest Contact's description will be populated in Account's Description

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

    Hi I got doubt like instead of map can we use listAc list=[select Id,AccountId,description,account.description from contact where I’d in:ContId];
    Then we can itereate through list like for(Account acc:Ac list)

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

      Hi , if we will use List then we have to use nested for loop which is not a good practice so to avoid problem of nested for loop we are using map.

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

    I am new to coding, if you explain when to use Map and when to use set that would be helpful

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

      I will create a video on this for sure.

  • @isai.geetha1867
    @isai.geetha1867 9 หลายเดือนก่อน

    Interesting Scenario - Can you please create a video with this exact scenario using Trigger Handler and Trigger separately as it is Best Practice.

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

      Sorry for late reply
      handler class -> public class ContactTriggerHandler
      {
      public static void trgMethod(List newConList,Map oldConMap)
      {
      Set accIds = new Set();
      Contact oldCon = new Contact();
      List accList = new List();
      if(!newConList.isEmpty())
      {
      for(Contact newCon : newConList)
      {
      if(oldConMap != null)
      {
      oldCon = oldConMap.get(newCon.Id);
      }
      if(newCon.AccountId != null && (oldCon == null || oldCon.Description != newCon.Description))
      {
      accIds.add(newCon.AccountId);
      }
      }
      }
      if(!accIds.isEmpty())
      {
      Map accMap = new Map([Select Id,Description from Account where Id IN : accIds]);
      for(Contact cont : newConList)
      {
      if(cont.AccountId != null && accMap.containsKey(cont.AccountId))
      {
      Account acc = accMap.get(cont.AccountId);
      acc.Description = cont.Description;
      accList.add(acc);
      }
      }
      }
      if(!accList.isEmpty())
      {
      update accList;
      }
      }
      }
      trigger -> trigger conTrg on Contact (after insert, after update)
      {
      if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate))
      {
      ContactTriggerHandler.trgMethod(trigger.new,trigger.oldMap);
      }
      }

  • @subhisingh4004
    @subhisingh4004 5 หลายเดือนก่อน +2

    Hi Please comment what is wrong if i do like below :
    List updateaccList = new List();
    if(trigger.isupdate){
    for(Contact c: trigger.new){
    if(c.Description!=trigger.Oldmap.get(c.id).description){
    Account a = new Account(id=c.AccountId);
    a.description=c.Description;
    updateaccList.add(a);
    }
    }
    update updateaccList;
    }

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

      what if contact doesn't have an account mapped? it will thrown an exception

  • @Shaik_Abdul_01
    @Shaik_Abdul_01 9 หลายเดือนก่อน

    Hi Ninja Is it mandatory to fetch the description of Parent record to update ?

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

      Sorry for late reply, yes it is required otherwise you will get soql error

  • @hrushikeshdhamdarhd5664
    @hrushikeshdhamdarhd5664 2 หลายเดือนก่อน

    Hi for the First For loop you filter all the records and then
    Again use trigger.new for which is unfiltered.
    Can you explain on this?

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

    What if someone updated 2 related contacts description same time

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

      Hi Tanmay I am extremely sorry for late reply, then the latest modified Contact's description will be populated in account's description.

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

    Great session , but If more than one contact is there, how it will get updated?

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

      Thanks, it will take the description from latest updated contact

  • @goldylodhi2116
    @goldylodhi2116 ปีที่แล้ว

    Very nice sir ❤

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

    Why we didn't create map first, like we did in parent to child

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

      Amit there could be many different ways to write a trigger.

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

    Hi, will this work?
    public void conmethod(list nlcon, map omcon){
    set accId = new set();
    map accdes = new map();
    for(contact c : nlcon){
    if(c.description != omcon.get(c.id).description && c.accountId!=null){
    accId.add(c.accountId);
    accdes.add(c.accountId, c.description);
    }
    }

    list lacc = [select id,description from account where Id in : accId];
    list llacc = new list();
    for(account a : lacc){
    a.description = accdes.get(a.id);
    llacc.add(a);
    }
    update llacc;
    }

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

      Hello, Yes it will work correctly but please use best practices like null checks on list.

    • @rajajha6386
      @rajajha6386 ปีที่แล้ว +3

      There are some issue in it
      1) you are using add in Map
      2)This can be done without using any SOQL query.Reduce SOQL query wherever possible
      Please find :
      trigger contactdescription on contact (after insert, after update){
      map accmap = new map();
      list acclisttoupdate = new list();
      for(contact con: trigger.new){
      if(trigger.isAfter){
      if(con.description != null && con.AccountId !=null && (trigger.isupdate && con.description != trigger.oldmap.get(con.id).description || trigger.isInsert) ){
      accmap.put(con.AccountId,con.Account);
      }
      }
      }
      if(!accmap.isEmpty()){
      for(contact con:trigger.new){
      Account acc = new Account();
      acc.id = con.AccountId;
      acc.description = con.description;
      acclisttoupdate.add(acc);
      }
      if(!acclisttoupdate.isEmpty()){
      update acclisttoupdate;
      }
      }
      }

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

      Thanks for sharing ,sorry i missed that thing.

    • @SameerAli-kx6km
      @SameerAli-kx6km 11 หลายเดือนก่อน

      Nice Raja bhai your code is Very Simple thx for the code Man !!!
      Are you salesforce developer ?? @@rajajha6386

  • @DS12399
    @DS12399 2 หลายเดือนก่อน

    You can avoid the other for loop by below code
    public class UpdateContact {

    public static void UpdateDescription(Map oldContactMap,Map newContactMap){
    List accList = new List();
    for(Contact con : newContactMap.values()){

    if(!con.Description.Equals(oldContactMap.get(con.Id).Description)){
    Account acc = new Account();
    acc.Id = con.AccountId;
    acc.Description =con.Description;
    accList.add(acc);
    }

    }
    if(!accList.isEmpty()){
    Update accList;
    }
    }
    }

  • @Tushar.S.Kitchen
    @Tushar.S.Kitchen 7 หลายเดือนก่อน

    Hi I am following the same code but still my trigger is not working what could be the issue?

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

      show me your code please

    • @Tushar.S.Kitchen
      @Tushar.S.Kitchen 6 หลายเดือนก่อน

      @@sfdcninjas problem has solved bro.

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

      that’s good and Sorry for late reply buddy

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

    It is child to parent scenario not parent to child

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

      Hi Chitresh i think you got confused in video title it is mentioned that update parent by child .

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

      Ohh sorry it's my mistake

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

      Can you make video on dispatcher framework

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

    trigger updateDiscription on Contact (after update) {

    If(trigger.isAfter && trigger.isupdate){

    List acc= new List();
    for(contact c : trigger.new){

    IF(c.Description != trigger.oldmap.get(c.Id).Description){

    account a = new account();
    a.id = c.AccountId;
    a.Description=c.Description;
    acc.add(a);
    }

    }
    update acc;

    }
    }
    This code is also working am i missing any best practice hear?

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

      hi buddy sorry for late reply, you can apply null checks on trigger.new and list of account before updating it also it is a good practice to use handler class

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

    If we write accMap.get(cont.accountId). description=cont.description
    Will that work?

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

      yes that will work

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

      @@sfdcninjas If I do that Like accMap.get(cont.accountId). description=cont.description ,
      Then What should i add in ListTobeUpdated(????)

    • @manjubhingarkar1930
      @manjubhingarkar1930 ปีที่แล้ว

      if u using this logic how u gonna store this in Account object and later how you gonna add it in collection to perform DML.

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

      I think here we get null pointer exception

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

      Because what if that cont.account id doest not exists in accmap

  • @SathishKumar-ok6tu
    @SathishKumar-ok6tu 6 หลายเดือนก่อน

    Do we need to take all the Account IDs separately in the Set?
    My below code works fine.
    trigger contactTrigger on Contact (after update) {
    if(trigger.isAfter && trigger.isUpdate)
    {
    if(!trigger.new.isEmpty())
    {
    List updateAccount = new List();
    for(Contact con : Trigger.new)
    {
    if(con.Description != Trigger.OldMap.get(con.Id).description && con.AccountId != null)
    {
    Account acc = [Select id,Description from Account where id = :con.AccountId];
    acc.Description = con.Description;
    updateAccount.add(acc);
    }
    }
    if(!updateAccount.isEmpty())
    update updateAccount;
    }
    }
    }

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

      sorry for late reply, yes it is a best practice to use collection for these type of requirements.

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

    Thank you very much sir

  • @Kings238
    @Kings238 7 หลายเดือนก่อน

    Thank you so much sir

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

      Most welcome