Apex Trigger Real time Scenarios || Part 3

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 พ.ค. 2024
  • In this video i am sharing :
    apex trigger interview questions scenario based
    salesforce coding interview questions
    apex trigger scenarios
    Want to Connect with me one to one :
    topmate.io/salesforce_in_5_minutes

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

  • @Arunaish-99
    @Arunaish-99 21 วันที่ผ่านมา +1

    Hey bro i did it in flow very easily

    • @salesforcein5minutes
      @salesforcein5minutes  21 วันที่ผ่านมา +1

      Yeah but jf interviewer asks you to do via trigger you have to use trigger no matter what

  • @GauriKadam-e3o
    @GauriKadam-e3o 8 วันที่ผ่านมา +1

    Have a doubt if we have 2 active contact what will happen

    • @salesforcein5minutes
      @salesforcein5minutes  8 วันที่ผ่านมา

      Write a before trigger which will throw an error if there is more than one contact which is going to be active this will. Just prevent the situations like this

  • @memesv3.093
    @memesv3.093 29 วันที่ผ่านมา +1

    Working code with a different approach. Suggestions/ Feedback are welcomed
    trigger ContactTrigger on Contact (after insert, after update) {
    Map conMap = new Map();

    if(Trigger.isAfter){

    if(!Trigger.new.isEmpty()){

    for(Contact c: Trigger.new){

    if(Trigger.isInsert && (c.Phone!=null)){
    conMap.put(c.AccountId, c);
    }

    else if(Trigger.isUpdate && Trigger.oldMap.get(c.Id).Phone != c.Phone){
    conMap.put(c.AccountId, c);
    }
    }
    }

    List accList = [Select Id, Phone from Account where Id IN: conMap.keySet()];

    List accsToUpdate = new List();

    for(Account a: accList){

    Contact c = conMap.get(a.Id);

    if(c.Primary_Contact__c == true){
    a.Phone = c.Phone;
    a.Id = c.AccountId;
    accsToUpdate.add(a);
    }
    else break;

    }
    update accsToUpdate;

    }

    }

    • @salesforcein5minutes
      @salesforcein5minutes  29 วันที่ผ่านมา

      The code is good! But will one account only have one contact?? One account can have multiple contact.
      So u must have map< id, list < contact >> if you are goong to check primary contact at the last.
      But instead if you check if the contact is primary at first itself then u can use map because we are already sure that one account will have one primary contact

  • @GAI1408
    @GAI1408 13 วันที่ผ่านมา +1

    Hi Bro What about this code Please review Beacuse it is working Perfectly on my org
    I also Write Code Using Map just like you but It is Updating Phone in Account when Both Active and Mobile phone Change
    Can you please give Any Suggestions??
    Using Set
    Public static void UpdateAccountphoneActive (Listconlist,MapMapsOld){
    Setaccid = new Set();
    Listaclist = New List();


    for(Contact con : conlist){
    if((con.Active__c && con.MobilePhone != MapsOld.get(con.id).MobilePhone) || (con.Active__c != MapsOld.get(con.id).Active__c)){
    accid.add(con.AccountId);

    }



    }

    for(Account ac :[Select id,Name,Phone,(Select id,Name,MobilePhone,Active__c,AccountId from Contacts) from Account Where Id in : accid ]){
    for(Contact con : ac.Contacts){
    if(con.AccountId!=null && con.MobilePhone!=null & Con.Active__c){
    ac.Phone = con.MobilePhone;
    aclist.add(ac);
    }

    }
    }
    Update aclist;




    }
    Using Map
    public static void updatePhoneOnaccActive(Listclist,Mapoldmap){
    MapmapS = new Map();
    listacclist= new List();
    for(Contact con : clist){
    if(con.Active__c){
    if(con.MobilePhone!=oldmap.get(con.id).MobilePhone){
    mapS.put(con.AccountId,con) ;
    }
    }

    }
    if(mapS.Size()>0){
    for(Account ac : [Select id,Name,Phone from Account where Id IN :mapS.keyset()]){
    if(mapS.containsKey(ac.id)){
    ac.Phone = mapS.get(ac.id).MobilePhone;
    acclist.add(ac);
    }
    }
    if(acclist.size()>0){
    update acclist;
    }

    }



    }

    • @salesforcein5minutes
      @salesforcein5minutes  13 วันที่ผ่านมา

      @@GAI1408 hii, the map looks more bulkified and will. Work efficiently in case if there is large data upload

    • @GAI1408
      @GAI1408 10 วันที่ผ่านมา

      @@salesforcein5minutes just confirm the set one Is correct or need improvement