Apex Triggers - 4 (Update Parent by Child)

Поделиться
HTML-код
  • Опубликовано: 18 ноя 2024

Комментарии • 60

  • @victordeoliveira344
    @victordeoliveira344 Год назад +1

    Amazing example! Thanks!

  • @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 11 месяцев назад

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

  • @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  Год назад +3

      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.

  • @isai.geetha1867
    @isai.geetha1867 Год назад

    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  9 месяцев назад +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);
      }
      }

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

  • @SmithaVlogsA-Z
    @SmithaVlogsA-Z 2 месяца назад

    Thanks , I could crack a problem in my project using your solution, it saved my day thanks

  • @salesforceneard9655
    @salesforceneard9655 Год назад +1

    Thank you so much sir

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

  • @swathikotha3977
    @swathikotha3977 9 месяцев назад

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

    • @sfdcninjas
      @sfdcninjas  9 месяцев назад

      Thanks, it will take the description from latest updated contact

  • @SabareeshKinthali
    @SabareeshKinthali Год назад +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

  • @hrushikeshdhamdarhd5664
    @hrushikeshdhamdarhd5664 6 месяцев назад

    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?

  • @goldylodhi2116
    @goldylodhi2116 Год назад

    Very nice sir ❤

  • @shyanilmishra6169
    @shyanilmishra6169 4 месяца назад

    Thank you very much 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.

  • @Shaik_Abdul_01
    @Shaik_Abdul_01 Год назад

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

    • @sfdcninjas
      @sfdcninjas  9 месяцев назад +1

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

  • @DS12399
    @DS12399 5 месяцев назад

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

  • @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 Год назад

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

  • @chitreshdesale2535
    @chitreshdesale2535 10 месяцев назад

    It is child to parent scenario not parent to child

    • @sfdcninjas
      @sfdcninjas  10 месяцев назад

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

    • @chitreshdesale2535
      @chitreshdesale2535 10 месяцев назад

      Ohh sorry it's my mistake

    • @chitreshdesale2535
      @chitreshdesale2535 10 месяцев назад

      Can you make video on dispatcher framework

  • @Tushar.S.Kitchen
    @Tushar.S.Kitchen 11 месяцев назад

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

    • @sfdcninjas
      @sfdcninjas  9 месяцев назад

      show me your code please

    • @Tushar.S.Kitchen
      @Tushar.S.Kitchen 9 месяцев назад

      @@sfdcninjas problem has solved bro.

    • @sfdcninjas
      @sfdcninjas  9 месяцев назад

      that’s good and Sorry for late reply buddy

  • @MrAnshulk
    @MrAnshulk Месяц назад

    Simpler solution
    trigger conTrigger on contact(after update){
    Map accountMapToUpdate = new map();
    for(Contact con:trigger.new){
    if(con.description__c!=null && con.description__c!=trigger.oldMap.get(con.Id).description__c){
    accountMapToUpdate.put(con.accountId,con.description__c);
    }
    }

    List accountsToUpdate = new List();

    if(!accountMapToUpdate.isEmpty()){
    for(Id accid: accountMapToUpdate.keySet()){
    Account acc = new Account(
    Id = accid;
    acc.Acc_description__c = accountMapToUpdate.get(accid)
    )
    accountsToUpdate.add(acc);
    }

    }
    if (!accountsToUpdate.isEmpty()) {
    update accountsToUpdate;
    }
    }

  • @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 7 месяцев назад

      I think here we get null pointer exception

    • @harinath1821
      @harinath1821 7 месяцев назад

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

  • @subhisingh4004
    @subhisingh4004 9 месяцев назад +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 4 месяца назад

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

  • @shekharnartam3444
    @shekharnartam3444 Год назад +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  Год назад

      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

  • @SathishKumar-ok6tu
    @SathishKumar-ok6tu 9 месяцев назад

    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  9 месяцев назад

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

  • @Kings238
    @Kings238 10 месяцев назад

    Thank you so much sir