Apex Triggers - 31 (Infosys Interview Trigger Scenario)

Поділитися
Вставка
  • Опубліковано 6 чер 2023
  • 💻 Join the Titan Community : Discover Exclusive Insights on LinkedIn / mycompany
    💻 Explore the Power of Titan : Visit Our Official Website Now 🔗 titandxp.com/
    These are the 2 methods by which we can fetch Cases related to account in this video I have used aggregate query method to fetch related Cases.
    Method - 1 (Using Inner SOQL query)
    accList = [SELECT Id, (SELECT Id FROM Cases WHERE IsClosed = true) FROM Account WHERE Id IN :accIds];
    Integer closedCaseCount = acc.Cases.size();
    Method - 2 (Using Map)
    for (Case closedCase : [SELECT AccountId FROM Case WHERE AccountId IN :accIds AND IsClosed = true])
    {
    if (accountCaseCountMap.containsKey(closedCase.AccountId))
    {
    accountCaseCountMap.put(closedCase.AccountId, accountCaseCountMap.get(closedCase.AccountId) + 1);
    }
    else {
    accountCaseCountMap.put(closedCase.AccountId, 1);
    }
    }
    Integer closedCaseCount = accountCaseCountMap.containsKey(acc.Id) ? accountCaseCountMap.get(acc.Id) : 0;
  • Наука та технологія

КОМЕНТАРІ • 23

  • @TheSatyam69
    @TheSatyam69 2 місяці тому

    Great code until line 42. We are unnecessarily querying account again and looping it. We could have done it in 2-3 lines. You don’t need to query account again, you may create an instance and assign Id. Thanks.

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

    Can you also make videos on not only triggers but also on apex classes which are used for different functionalities. That will be very helpful.

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

      Hi , Yes i have that in my plan dont worry you wil get it soon

  • @AnmolAgrawal-sc5ll
    @AnmolAgrawal-sc5ll 5 місяців тому

    bro no doubt you are doing great but please give a good description of scenario here you are telling update accoun ton basis of closed cases so we are assuming it will be on account object you would have tp give better description like when we change or insert cases then update account this looses motivation bcoz our reasoning fails due to wrong questions

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

    Hello can you please let us know how you are getting this scenarios , if the questions are available in more content it will help to practice as our own as well

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

      Hi Prithvi, I have reached out to experienced professionals and individuals who have undergone interviews at various companies in order to gather insights and information regarding different scenarios.

  • @BalajiBalaji-jv1gt
    @BalajiBalaji-jv1gt 11 місяців тому

    When aggregating the case object is it not possible to write sub query of account.?

    • @sfdcninjas
      @sfdcninjas  10 місяців тому +1

      Hi buddy sorry for late reply actually it is not a good practice to use sub query

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

    @sfdc ninja
    At what experience level do they ask this type of questions?

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

      Hi Rajnish , this question was asked to a person with 1.8 years of experience.

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

      ​@@sfdcninjas
      Thanks for reply :)

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

    Write a Test Class For that Triggers.

    • @sfdcninjas
      @sfdcninjas  Рік тому +1

      Hi Venkatesh , yes buddy i have that in my plan there will be playlist of test classes for all trigger scenarios

    • @ankitdangre4790
      @ankitdangre4790 Рік тому +1

      Same

    • @sfdcninjas
      @sfdcninjas  Рік тому +1

      don’t worry guys there will be a series on test classes

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

      Sir plz make a video on interview questions asked in various IT companies .

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

    Please Can you share code

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

      Hi Hareesh , all codes will be available on website soon don't worry.

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

      @@sfdcninjas thanks

  • @utkarshsoni7938
    @utkarshsoni7938 Місяць тому

    Try the below one the below code will work in Bulk for the all the Trigger Scenario's.
    Trgr: ->
    ----
    trigger AccountRatingTrgr on Case(after insert, after update, after delete, after undelete){
    if(Trigger.isAfter){
    if(Trigger.isUpdate){
    AccountRatingTrgrCls.AccountRatingTrgrMethod(Trigger.New,Trigger.OldMap);
    }else if(Trigger.isDelete){
    AccountRatingTrgrCls.AccountRatingTrgrMethod(Trigger.Old,null);
    }else{
    AccountRatingTrgrCls.AccountRatingTrgrMethod(Trigger.New,null);
    }
    }
    }
    Modular Class: ->
    -------------
    public class AccountRatingTrgrCls{
    public static void AccountRatingTrgrMethod(List caseList,Map oldCaseMap){
    String accountRating;
    Set setOfAccountIds = new Set();
    Map accountMap = new Map();
    List accountList = new List();
    List accountListToUpdate = new List();
    if(caseList!=null && caseList.size()>0){
    for(Case cses : caseList){
    if(cses.IsClosed__c){
    if(oldCaseMap==null){
    setOfAccountIds.add(cses.AccountId);
    }else{
    if(cses.AccountId != oldCaseMap.get(cses.Id).AccountId){
    setOfAccountIds.add(cses.AccountId);
    setOfAccountIds.add(oldCaseMap.get(cses.Id).AccountId);
    }else{
    setOfAccountIds.add(cses.AccountId);
    }
    }
    }
    }
    if(setOfAccountIds.size()>0 && setOfAccountIds!=null){
    for(AggregateResult agr : [Select AccountId, Count(Id) countOfCaseIds FROM Case WHERE AccountId IN: setOfAccountIds GROUP BY AccountId]){
    accountMap.put((Id)agr.get('AccountId'),(Integer)agr.get('countOfCaseIds'));
    }
    }
    accountList = [Select Id,Rating FROM Account WHERE ID IN: setOfAccountIds];
    System.debug('accountMap: '+accountMap);
    if(accountList.size()>0 && accountList!=null){
    for(Account accounts : accountList){
    if(!accountMap.isEmpty() && accountMap!=null){
    accountRating='';
    if(accountMap.containsKey(accounts.Id)){
    if((Integer)accountMap.get(accounts.Id)==0){
    accountRating = '';
    }else if((Integer)accountMap.get(accounts.Id)>0 && (Integer)accountMap.get(accounts.Id)2 && (Integer)accountMap.get(accounts.Id)6){
    accountRating = 'Hot';
    }
    accountListToUpdate.add(new Account(Id=accounts.Id,Rating=(String)accountRating));
    }else{
    accountListToUpdate.add(new Account(Id=accounts.Id,Rating=''));
    }
    }else{
    accountListToUpdate.add(new Account(Id=accounts.Id,Rating=''));
    }
    }
    }
    if(accountListToUpdate.size()>0 && accountListToUpdate!=null){
    UPDATE accountListToUpdate;
    }
    }
    }
    }

  • @shubhamsri.1895
    @shubhamsri.1895 Рік тому +1

    in my Case Status field CLOSED picklist is not showing.
    Only New, Working, Escalated is visible.
    How to view Closed option?

    • @sfdcninjas
      @sfdcninjas  Рік тому +1

      Hi Shubham , I have already explained that issue in video please see from 14:00