Apex Triggers - 38 (Salesforce Trigger Interview Scenario)

Поділитися
Вставка
  • Опубліковано 5 вер 2024
  • Revolutionize your Salesforce experience with Titan's powerful products
    Create and automate custom document templates directly from Salesforce - titandxp.com/p...
    Share, manage, and track the progress of e-signatures directly from Salesforce - titandxp.com/p...
    Create, automate, and track custom web forms directly from Salesforce - titandxp.com/p...
    Design and deploy dynamic no-code web portals and applications from Salesforce - titandxp.com/p...
    Automate your entire organization’s processes with digital workflow - titandxp.com/p...
    Create, automate, and track custom web surveys directly from Salesforce - titandxp.com/p...

КОМЕНТАРІ • 8

  • @user-fd3cv1op4s
    @user-fd3cv1op4s 8 днів тому

    What if there are more than 1 contact associated with a type?

  • @thesorcerer111
    @thesorcerer111 3 дні тому

    Scenario: Whenever the Opportunity field "Type" gets updated then check the Opportunity's Account - if there is a Contact present with the same "Type" value as the Opportunity, then insert a new OpportunityContactRole and delete the existing OpportunityContactRole (for that Opportunity). In the Opportunity's Account - if there is no Contact present with the same "Type" value as the Opportunity then throw an error .
    Trigger OppTrigger on Opportunity(After Update ){
    TriggerHandler.updateMethod(Trigger.new, Trigger.oldMap );
    }
    public class TriggerHandler{
    public static void updateMethod(List oppList, Map oldMap ){
    Map accIdVsOpp = new Map();
    Set oppIds = new Set();
    for(Opportunity opp : oppList ){
    if(oldMap != null && opp.Type__c != null && oldMap.get(opp.Id).Type__c != opp.Type__c && opp.AccountId != null){
    accIdVsOpp.put(opp.AccountId, opp);
    oppIds.add(opp.Id);
    }
    }
    Map oppIdVsCon = new Map();
    if(!accIdVsOpp.isEmpty() ){
    for(Contact con : [Select Id, Type__c From Contact Where AccountId In: accIdVsOpp.keyset() ]){
    if(con.Type__c == accIdVsOpp.get(con.AccountId).Type ){
    oppIdVsCon.put(accIdVsOpp.get(con.AccountId).Id, con );
    }
    }
    }
    List ocrListToInsert = new List();
    if(!oppIdVsCon.isEmpty() ){
    for(Opportunity opp : oppList){
    if(oppIdVsCon.containsKey(opp.Id) ){
    //insert new OpportunityContactRole
    OpportunityContactRole ocr = new OpportunityContactRole();
    ocr.ContactId = oppIdVsCon.get(opp.Id).Id;
    ocr.OpportunityId = opp.Id;
    ocrListToInsert.add(ocr);
    }else{
    //show error
    opp.addError('There is no contact present with the type '+ opp.Type+'.') ;
    }
    }
    }
    if(ocrListToInsert.size() > 0 ){
    List ocrListExisting = [Select Id From OpportunityContactRole Where OpportunityId In: oppIds] ;
    delete ocrListExisting;
    insert ocrListToInsert;
    }
    }
    }
    Here is my code snippet of the problem. The question scenario was a bit tricky for me to understand so I have slightly modified the verbiage to make it a bit more simpler.

  • @RaviShankar1947
    @RaviShankar1947 11 місяців тому

    Why did you put limit 1 on contacts? We might get multiple opportunities in our trigger and thus multiple accounts, so we will get multiple contacts in the list that we have to maintain on a Map. On line no 36 you have to ensure you are getting contact only related to that account. Along with type we also need some filter to get only related contacts. Please clarify.

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

    Very nice sir ❤

  • @sarojpatil-zj9qv
    @sarojpatil-zj9qv 8 місяців тому

    hello sir, becoming difficult for me to write the code any suggestion how should i adapt ?know everything about triggers still not able to write it on my own.

    • @sfdcninjas
      @sfdcninjas  8 місяців тому

      Hi Buddy, there are some points you should focus on before writing any Apex trigger.
      1. Identify the object on which the trigger will operate. Determine the object undergoing changes that will trigger our code.
      2. Clarify the specific action required. Understand whether the trigger's purpose is to update a parent record based on a child record, update a child record based on a parent record, or perform updates on the same object. Consider the type of trigger that best suits your needs.
      and also do practice again and again i have a playlist on this channel which has 41 trigger scenarios go through every scenario write it in your system and definitely you will get clarification .
      and if you will face any problem then I will be there for you.