Apex Triggers - 55 (Trigger Interview Scenario)

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

КОМЕНТАРІ • 6

  • @vindhyasravan8256
    @vindhyasravan8256 19 днів тому

    Thanks

  • @sedentaryhooman
    @sedentaryhooman 14 днів тому

    which company asked this?

  • @_DheerajIPPILI
    @_DheerajIPPILI 10 днів тому

    Trigger -
    trigger Accdesc on Contact (after insert, after update, after delete, after undelete) {
    if (trigger.isAfter && trigger.isInsert) {
    UpdateAccDec.DMLConInsertion(trigger.new);
    }
    if (trigger.isAfter && trigger.isUpdate) {
    UpdateAccDec.DMLConUpdate(trigger.new, trigger.oldMap);
    System.debug('Checkpoint -1');
    }
    if (trigger.isAfter && trigger.isDelete) {
    UpdateAccDec.DMLDelete(trigger.old);
    }
    if (trigger.isAfter && trigger.isUndelete) {
    UpdateAccDec.DMLUndelete(trigger.new);
    }
    }
    Handler Class -
    public class UpdateAccDec {
    public static void DMLConInsertion(List conlist) {
    Set accIds = new Set();
    List accList = new List();
    System.debug('Checkpoint-2');
    for (Contact c : conlist) {
    if (c.AccountId != null) {
    accIds.add(c.AccountId);
    }
    }
    System.debug('Accountids' + accIds);
    accList = [SELECT Id, Name, Description, (SELECT Id, Name, CreatedDate FROM Contacts) FROM Account WHERE Id IN :accIds];
    for (Account a : accList) {
    String accDes = '';
    for (Contact c : a.Contacts) {
    accDes += c.Name + ' ' + c.CreatedDate.format() + '
    ';
    }
    if (accDes.endsWith(', ')) {
    accDes = accDes.removeEnd(', ');
    }
    a.Description = accDes;
    }
    if (!accList.isEmpty()) {
    update accList;
    }
    System.debug('Accounts' + accList);
    }
    public static void DMLConUpdate(List conlist, Map oldMap) {
    Set oldAccIds = new Set();
    Set newAccIds = new Set();
    List oldConList = new List();
    List newConList = new List();
    System.debug('Checkpoint -2');
    for (Contact c : conlist) {
    System.debug('Contact ids' + c.Id);
    Contact oldCon = oldMap.get(c.Id);
    System.debug('Oldcontactid' + oldCon.Id);
    if (c.AccountId != oldCon.AccountId) {
    oldAccIds.add(oldCon.AccountId);
    newAccIds.add(c.AccountId);
    } else if (c.AccountId != null) {
    newAccIds.add(c.AccountId);
    System.debug('New Accountid' + c.AccountId);
    }
    }
    System.debug('Old Accountids' + oldAccIds);
    System.debug('New Accountids' + newAccIds);
    oldConList = [SELECT Id, Name, CreatedDate, AccountId FROM Contact WHERE AccountId IN :oldAccIds];
    newConList = [SELECT Id, Name, CreatedDate, AccountId FROM Contact WHERE AccountId IN :newAccIds];
    System.debug('Old contact list' + oldConList);
    System.debug('New contact list' + newConList);
    if (!oldConList.isEmpty()) {
    DMLConInsertion(oldConList);
    }
    if (!newConList.isEmpty()) {
    DMLConInsertion(newConList);
    }
    }
    public static void DMLDelete(List conlist) {
    if (!conlist.isEmpty()) {
    DMLConInsertion(conlist); // reuse the code
    }
    }
    public static void DMLUndelete(List conlist) {
    if (!conlist.isEmpty()) {
    DMLConInsertion(conlist); // reuse the code
    }
    }
    }

  • @kumareshghosh5593
    @kumareshghosh5593 17 днів тому

    At the end, you could have easily loop through the map's keySet directly rather than using the accIds I can see. Otherwise nice approach.

    • @IAmSanthoshReddy
      @IAmSanthoshReddy 16 днів тому

      Looping Map would miss to update account description with no contacts

    • @kumareshghosh5593
      @kumareshghosh5593 16 днів тому

      @@IAmSanthoshReddy That's right though