Apex Triggers - 56 (Trigger Interview Scenario)

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

КОМЕНТАРІ • 13

  • @parthipan0345
    @parthipan0345 Місяць тому +1

    Great.. it will work for single account but its wont work for bulk records because it will store second highest amount for account getting in list. We may need to use map to store accountid and second highest amount.

    • @sfdcninjas
      @sfdcninjas  Місяць тому +1

      Hi buddy, yes you are correct in upcoming video i am going to show for bulk records as well. But can you please comment your approach it will help me and others to learn new things.
      Thanks

    • @gaurangi8834
      @gaurangi8834 25 днів тому

      Can we use it ?
      for(Account acc: [select id,(select id, Amount from Opportunities order by Amount desc offset 1 limit 1) from Account where Id IN:accids]){
      Account accrec = new Account ();
      accrec.id=acc.id;
      Accrec.description=acc.opportunities.Amount;}

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

    I am waiting for that please try as soon as possible

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

      The triggers question are available on trigger no 54 and that scenarios still pending.......

    • @sfdcninjas
      @sfdcninjas  29 днів тому

      Sure pranay, sorry for the delay

  • @gauravjoshi3879
    @gauravjoshi3879 21 день тому

    Sir i am facing issue in a trigger "trigger will fire when we will update opportunity to closed won and it will create order and order item as that are in opportunity line item " when i am using try and catch block it is working fine but when i am not using i am not getting expected result please help
    trigger CreateOrderOnOpportunityWon on Opportunity (after update) {
    List ordersToInsert = new List();
    List orderItemsToInsert = new List();
    Set opportunityIds = new Set();


    for (Opportunity opp : Trigger.new) {
    if (opp.IsWon && Trigger.oldMap.get(opp.Id).StageName != 'Closed Won') {
    System.debug('Opportunity moved to Closed Won: ' + opp.Id);
    opportunityIds.add(opp.Id);
    }
    }

    System.debug('Opportunity Ids moved to Closed Won: ' + opportunityIds);

    if (!opportunityIds.isEmpty()) {
    try {

    List opportunityLineItems = [SELECT OpportunityId, Product2Id, Quantity, UnitPrice, PricebookEntryId FROM OpportunityLineItem WHERE OpportunityId IN :opportunityIds];

    System.debug('Queried OpportunityLineItems: ' + opportunityLineItems);
    List opportunities = [SELECT Id, AccountId, ContractId, Pricebook2Id FROM Opportunity WHERE Id IN :opportunityIds];

    System.debug('Queried Opportunities: ' + opportunities);


    Map oppOrderMap = new Map();

    for (Opportunity opp : opportunities) {
    Order newOrder = new Order();
    newOrder.AccountId = opp.AccountId;
    newOrder.ContractId = opp.ContractId;
    newOrder.EffectiveDate = Date.today();
    newOrder.Status = 'Draft';
    newOrder.Pricebook2Id = opp.Pricebook2Id;
    ordersToInsert.add(newOrder);
    oppOrderMap.put(opp.Id, newOrder);

    System.debug('Prepared Order to insert: ' + newOrder);
    }

    if (!ordersToInsert.isEmpty()) {
    try {
    insert ordersToInsert;
    System.debug('Orders inserted successfully: ' + ordersToInsert);


    for (OpportunityLineItem oli : opportunityLineItems) {
    Order newOrder = oppOrderMap.get(oli.OpportunityId);

    if (newOrder != null) {
    OrderItem newOrderItem = new OrderItem();
    newOrderItem.Product2Id = oli.Product2Id;
    newOrderItem.Quantity = oli.Quantity;
    newOrderItem.UnitPrice = oli.UnitPrice;
    newOrderItem.PricebookEntryId = oli.PricebookEntryId; // Add PricebookEntryId
    newOrderItem.OrderId = newOrder.Id;
    orderItemsToInsert.add(newOrderItem);

    System.debug('Prepared OrderItem to insert: ' + newOrderItem);
    } else {
    System.debug('No matching order found for OpportunityLineItem: ' + oli);
    }
    }

    if (!orderItemsToInsert.isEmpty()) {
    try {
    insert orderItemsToInsert;
    System.debug('OrderItems inserted successfully: ' + orderItemsToInsert);
    } catch (DmlException dme) {
    System.debug('Error inserting OrderItems: ' + dme.getMessage());
    System.debug('DML Error Details: ' + dme.getDmlMessage(0));
    System.debug('DML Error Status Code: ' + dme.getDmlStatusCode(0));
    System.debug('DML Error Fields: ' + dme.getDmlFields(0));
    }
    } else {
    System.debug('No OrderItems to insert');
    }
    } catch (DmlException dme) {
    System.debug('Error inserting Orders: ' + dme.getMessage());
    System.debug('DML Error Details: ' + dme.getDmlMessage(0));
    System.debug('DML Error Status Code: ' + dme.getDmlStatusCode(0));
    System.debug('DML Error Fields: ' + dme.getDmlFields(0));
    }
    } else {
    System.debug('No Orders to insert');
    }
    } catch (Exception e) {
    System.debug('Error in trigger execution: ' + e.getMessage());
    System.debug('Exception Stack Trace: ' + e.getStackTraceString());
    }
    } else {
    System.debug('No Opportunities moved to Closed Won');
    }
    }

  • @prasadmuthyala9625
    @prasadmuthyala9625 29 днів тому

    If we use order by and offset can we decrease lines of code??

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

    Hello ninja, I send you Two trigger scenarios but now as well it will not solved