Apex Triggers - 47 (Accenture Interview Scenario)

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025

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

  • @abhijit14820
    @abhijit14820 11 หลายเดือนก่อน +2

    Thank you for another trigger scenario sir

    • @sfdcninjas
      @sfdcninjas  11 หลายเดือนก่อน +1

      Thanks brother

  • @VishnuReddy-c2q
    @VishnuReddy-c2q 10 หลายเดือนก่อน +2

    In SOQL can we use ORDER BY Amount DESC LIMIT 1.....?

  • @sameerqadri9877
    @sameerqadri9877 11 หลายเดือนก่อน +1

    Thx a lot man you helped as a lot !!!
    But you posted more than 47 trigger scenarios and how much Experience persons will get this type of questions in the interview as a developer ??
    2+ or what ? how much

  • @abhijit14820
    @abhijit14820 11 หลายเดือนก่อน +3

    Sir could you please explain how to implement below trigger.
    This was given by Tripathi sir. He said he will ask you to implement it.
    Please sir if possible do video on this. I tried it but didn't get desired result.
    There is a secondary owner look up field to user on account object every time an object is created will have to create a share record and share that particular account with the secondary owner and when accounts get updated this field is change we have to remove previously shared record which got created earlier and we have to create a new record with the updated value on it.
    Thank you in advance!

    • @sfdcninjas
      @sfdcninjas  11 หลายเดือนก่อน

      Sure bhai if Mohit sir said to implement it i will create a video for sure but it will take time to create a video on it

    • @abhijit14820
      @abhijit14820 11 หลายเดือนก่อน

      @@sfdcninjas Thank you sir! I will wait for it.

    • @deeps1015
      @deeps1015 10 หลายเดือนก่อน

      @@abhijit14820 try this
      trigger ShareAccountWithSecondaryOwner on Account (after insert, after update) {
      List sharesToInsert = new List();
      List sharesToDelete = new List();
      // Map to hold the secondary owner Id for each account
      Map accountIdToSecondaryOwnerId = new Map();
      // Collect secondary owner Ids for newly inserted or updated accounts
      for (Account acc : Trigger.new) {
      // Check if the secondary owner lookup field has changed
      if (Trigger.isInsert || acc.Secondary_Owner__c != Trigger.oldMap.get(acc.Id).Secondary_Owner__c) {
      accountIdToSecondaryOwnerId.put(acc.Id, acc.Secondary_Owner__c);
      }
      }
      // Query existing account shares for accounts that are being updated
      List existingShares = [SELECT Id, AccountId FROM AccountShare WHERE AccountId IN :accountIdToSecondaryOwnerId.keySet()];
      // Delete existing shares for updated accounts
      for (AccountShare share : existingShares) {
      sharesToDelete.add(new AccountShare(Id = share.Id));
      }
      // Insert new shares for updated accounts
      for (Id accountId : accountIdToSecondaryOwnerId.keySet()) {
      sharesToInsert.add(new AccountShare(
      AccountId = accountId,
      UserOrGroupId = accountIdToSecondaryOwnerId.get(accountId),
      AccountAccessLevel = 'Read'
      ));
      }
      // Perform DML operations
      if (!sharesToDelete.isEmpty()) {
      delete sharesToDelete;
      }
      if (!sharesToInsert.isEmpty()) {
      insert sharesToInsert;
      }
      }

    • @deeps1015
      @deeps1015 10 หลายเดือนก่อน +1

      @@abhijit14820 try this. source: chat GPT
      trigger ShareAccountWithSecondaryOwner on Account (after insert, after update) {
      List sharesToInsert = new List();
      List sharesToDelete = new List();
      // Map to hold the secondary owner Id for each account
      Map accountIdToSecondaryOwnerId = new Map();
      // Collect secondary owner Ids for newly inserted or updated accounts
      for (Account acc : Trigger.new) {
      // Check if the secondary owner lookup field has changed
      if (Trigger.isInsert || acc.Secondary_Owner__c != Trigger.oldMap.get(acc.Id).Secondary_Owner__c) {
      accountIdToSecondaryOwnerId.put(acc.Id, acc.Secondary_Owner__c);
      }
      }
      // Query existing account shares for accounts that are being updated
      List existingShares = [SELECT Id, AccountId FROM AccountShare WHERE AccountId IN :accountIdToSecondaryOwnerId.keySet()];
      // Delete existing shares for updated accounts
      for (AccountShare share : existingShares) {
      sharesToDelete.add(new AccountShare(Id = share.Id));
      }
      // Insert new shares for updated accounts
      for (Id accountId : accountIdToSecondaryOwnerId.keySet()) {
      sharesToInsert.add(new AccountShare(
      AccountId = accountId,
      UserOrGroupId = accountIdToSecondaryOwnerId.get(accountId),
      AccountAccessLevel = 'Read'
      ));
      }
      // Perform DML operations
      if (!sharesToDelete.isEmpty()) {
      delete sharesToDelete;
      }
      if (!sharesToInsert.isEmpty()) {
      insert sharesToInsert;
      }
      }

  • @sainathkide3230
    @sainathkide3230 10 หลายเดือนก่อน +1

    Can we use parent child query with descending amount and then using first opportunity we cane store it in map ( account id , opp[o].amount )?

  • @hekkelkelseheklalallkh8161
    @hekkelkelseheklalallkh8161 10 หลายเดือนก่อน +1

    Hi, I have a small doubt, does your code work in the scenario where An Account has only one related opportunity and that opportunity is reparented from this Account to another account........ For the new account, your code works fine, but I have doubt whether for old account the field will be updated to empty as I think the oppMap.get(IDs).name will return System.nullpointer , attempt to de-reference a null object exception, correct me if I am wrong, and explain me the scenario please.

    • @alishalinski
      @alishalinski 10 หลายเดือนก่อน

      Maybe to avoid that null we can use oppMap.KeySet() instead of accIds, which there will be only accs with opps
      And also probably we will need to check on update account trigger for accs with no opps where we can just make blank the description field
      Also probably it will be better to check null for newOpp .AccountId before adding it to accIds set

    • @alishalinski
      @alishalinski 10 หลายเดือนก่อน

      Maybe to avoid that null we can use oppMap.KeySet() instead of accIds, which there will be only accs with opps
      And also probably we will need to check on update account trigger for accs with no opps where we can just make blank the description field
      Also probably it will be better to check null for newOpp .AccountId before adding it to accIds set

  • @goldylodhi2116
    @goldylodhi2116 11 หลายเดือนก่อน +1

    Very well explained sir

  • @madhumohan2952
    @madhumohan2952 10 หลายเดือนก่อน +2

    can any one say as if we are avoiding aggregate query , so can we have this query to fetch the highest amount like : list opplist = [select id , amount, accountId from amount where accountid IN:accids and order by amount desc limit 1];

    • @Tushar.S.Kitchen
      @Tushar.S.Kitchen 7 หลายเดือนก่อน

      Yes just add NULLS LAST in your Soql and It will work.
      list Opplist = [SELECT Id,Name,Amount,AccountId from Opportunity where AccountId IN :AccIds ORDER By Amount DESC NULLS LAST Limit 1];

    • @sedentaryhooman
      @sedentaryhooman 5 หลายเดือนก่อน

      @@Tushar.S.Kitchen You cannot use LIMIT 1 here.. if you change the accountId on an opp, your query will return opp record for a single account and the opp related to other acc will be left out.

  • @MukheshKummithi
    @MukheshKummithi 9 หลายเดือนก่อน

    Hi Sir
    I got a scenario for trigger in NTTData interview
    You have a Parent Object Order and its child OrderLineItems. On Order Object you have a custom field
    Number_of_Line_Items.When you insert a new record of the parent object, based on the value in Number_of_Line_Items field, insert
    that many OrderLineItems? Can you Please help me sir to solve this

    • @sfdcninjas
      @sfdcninjas  9 หลายเดือนก่อน

      Sure you will get a video on it this week

    • @MukheshKummithi
      @MukheshKummithi 9 หลายเดือนก่อน

      @@sfdcninjas Thank you sir
      I am practising the scenarios and the explanation was good

    • @sfdcninjas
      @sfdcninjas  9 หลายเดือนก่อน

      keep watching and supporting bro

  • @vipulugle1316
    @vipulugle1316 10 หลายเดือนก่อน

    When the account is updated, send an email to the account owner with the details of contact modified between the last update of account vs current update.
    NTT Data interview question.
    Pls make a solution video on this scenario, I request.🙏

    • @sfdcninjas
      @sfdcninjas  10 หลายเดือนก่อน +1

      Hi vipul thanks for providing scenario i will create a video on it for sure but please wait it might take time

    • @vipulugle1316
      @vipulugle1316 10 หลายเดือนก่อน

      Ok sure. Thanks!😊

    • @vipulugle1316
      @vipulugle1316 9 หลายเดือนก่อน

      Create video on this scenario too sir

    • @sfdcninjas
      @sfdcninjas  9 หลายเดือนก่อน

      You will get it tomorrow bro

  • @ShwetaBanne
    @ShwetaBanne 10 หลายเดือนก่อน

    whenever the contact is created if contact has any related account then if account doesn't have any related opportunity it will create new opp for that account , if account has any related opportunity then the sum of amount of opportunity stored in related account custom field - asked in interview .can you give solution

    • @sfdcninjas
      @sfdcninjas  10 หลายเดือนก่อน

      Hi sure , in which company’s interview it was asked?

    • @ShwetaBanne
      @ShwetaBanne 10 หลายเดือนก่อน

      Astrait IT services

    • @sfdcninjas
      @sfdcninjas  10 หลายเดือนก่อน

      Sure i will create a video on it but you have to wait for.
      is that ok with you?

    • @awesomekj5812
      @awesomekj5812 10 หลายเดือนก่อน

      @@ShwetaBanne why would an unknown company ask such a difficult trigger question ? Is it to show potential candidate down ?

    • @kumareshghosh5593
      @kumareshghosh5593 10 หลายเดือนก่อน

      bro here is your solution, thanks for sharing a scenario:
      handler class:
      public with sharing class conAccOppController {
      //S:2- whenever the contact is created if contact has any related account then if account doesn't have any related opportunity it will create new opp for that account , if account has any related opportunity then the sum of amount of opportunity stored in related account custom field - asked in interview
      public static void afterInsert(list newCons){
      set accIds=new set();
      for(contact con:newCons){
      if(con.AccountId!=null){
      accIds.add(con.AccountId);
      }
      }
      //account with no opportunities:
      list accWithNoOppor = [select id,name from account where id in :accIds and id not in (select accountId from opportunity)];
      List newOpps=new List();
      for(Account acc:accWithNoOppor){
      newOpps.add(new Opportunity(name='Opp created from '+acc.name+' account',accountId=acc.id,CloseDate=date.today()+10,stageName='Prospecting'));
      }
      if(newOpps.size()!=0){
      insert newOpps;
      }
      aggregateResult[] agg=[select accountId,sum(amount) sumAmounts from opportunity group by accountId];
      list accAmUpdate=new List();
      for(aggregateResult agg2:agg){
      string ids=(String) agg2.get('accountId');
      decimal totalAmounts=(decimal) agg2.get('sumAmounts');
      account acc=new account(id=ids,Total_Opportunity_Amount__c=totalAmounts);
      accAmUpdate.add(acc);
      }
      if(accAmUpdate.size()!=0){
      update accAmUpdate;
      }
      }
      }
      trigger:
      trigger accConOpp on Contact (after insert) {
      conAccOppController.afterInsert(Trigger.new);
      }

  • @smitsism
    @smitsism 10 หลายเดือนก่อน

    yOU ARE DOINGAMAZING, CAN YOU HELP I NMORE AMAZON LEVEL INTERVIEW?

  • @sainikbasu4486
    @sainikbasu4486 11 วันที่ผ่านมา

    This apex class code is to long

  • @himadripaul7332
    @himadripaul7332 10 หลายเดือนก่อน +1

    Sir, you changed the approach this is similar to trigger 16 where you used sub query any reason for that ???

    • @sfdcninjas
      @sfdcninjas  10 หลายเดือนก่อน +1

      Hi buddy , using subquery is not a good practice in apex

  • @madhumohan2952
    @madhumohan2952 10 หลายเดือนก่อน

    can you please say sir will this work ?? i am not able to get the output , can anyone say where it is going wrong?
    set accids = new set();
    for(opportunity opp:opplist)
    {
    if(oldmap== null && opp.amount!=null && opp.AccountId != null)
    {
    accids.add(opp.AccountId);
    }
    else if(oldmap!=null)
    {
    if(oldmap.get(opp.id).accountId != opp.AccountId)
    {
    accids.add(opp.accountId);
    accids.add(oldmap.get(opp.id).accountId);
    }
    else if(opp.Amount != oldmap.get(opp.id).amount)
    {
    accids.add(opp.AccountId);
    }
    }

    }
    system.debug('accids' +accids);

    list acclist = [select id , description from account where id in:accids];
    list opplist1 = [select id ,amount, accountId from opportunity where accountID in:accids order by amount desc limit 1];
    system.debug('highest opportunity' +opplist1);
    map stringmap= new map();
    for(opportunity opp: opplist1)
    {
    stringmap.put(opp.AccountId, opp.name);
    }
    system.debug('stringmap' +stringmap);


    list acclist1= new list();
    for(account acc: acclist)
    {
    if(stringmap.containskey(acc.id))
    {
    acc.Description = stringmap.get(acc.id);
    acclist1.add(acc);
    }
    }
    if(acclist1.size()>0)
    {
    update acclist1;
    }

    • @sedentaryhooman
      @sedentaryhooman 5 หลายเดือนก่อน

      @madhumohan2952 - the problem lies in your for loop where you are iterating the opplist1... when this for loop finishes its iteration, the map will be having the lower amount opp name set for the respective account because your key will be overriding for every for loop iteration.. you can have this inside a if-block and only populate your map when its not having the particular key as an account ID.

  • @_DheerajIPPILI
    @_DheerajIPPILI 7 หลายเดือนก่อน +1

    Trigger - Solution Approach
    trigger AccDes on Opportunity (after insert, after update, after delete,after undelete) {
    if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
    AccountOpp.handleAfterInsertUpdate(Trigger.new);
    }
    if (Trigger.isAfter && Trigger.isDelete) {
    AccountOpp.handleAfterDelete(Trigger.old);
    }
    if(Trigger.isAfter && Trigger.isUndelete)
    {
    AccountOpp.handleAfterUndelete(Trigger.new);
    }
    }
    Handler Class -
    public class AccountOpp {
    public static void handleAfterInsertUpdate(List newOpList) {
    Set accIds = new Set();
    for (Opportunity o : newOpList) {
    if (o.Amount != null) {
    accIds.add(o.AccountId);
    }
    }
    if (!accIds.isEmpty()) {
    List accList = [SELECT Id, Name, Description,
    (SELECT Id, Amount, Name
    FROM Opportunities
    ORDER BY Amount DESC
    LIMIT 1)
    FROM Account
    WHERE Id IN :accIds];
    if (!accList.isEmpty()) {
    for (Account a : accList) {
    if (!a.Opportunities.isEmpty()) {
    a.Description = a.Opportunities[0].Name;
    }
    else
    {
    a.Description = null;
    }
    }
    update accList;
    }
    }
    }
    public static void handleAfterInsertUpdate(List newOpList, List oldOpList) {
    handleAfterInsertUpdate(newOpList); // Reuse the same method for update
    }

    public static void handleAfterDelete (List oplist)
    {
    handleAfterInsertUpdate(oplist); // Reuse the same method for updat

    }
    public static void handleAfterUndelete (List oplist)
    {
    handleAfterInsertUpdate(oplist); // Reuse the same method for update
    }
    }