function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jagadeep kjagadeep k 

Challenge Not yet complete... here's what's wrong: Ensure that you call the correct method from orderTrigger.

In Advanced Apex Specialist Challenge 2
Challenge Not yet complete... here's what's wrong: Ensure that you call the correct method from orderTrigger.
My Trigger and helper class code I am pasting it here
trigger orderTrigger on Order (after update) {
 
    if(Trigger.new != null){   
            OrderHelper.AfterUpdate(Trigger.new,Trigger.old);       
    }
}
OrderHelper class
public class OrderHelper {
        /**
         * @name AfterUpdate
         * @description
         * @param List<Order> newList
         * @param List<Order> oldList
         * @return void
        **/
        public static void AfterUpdate(List<Order> newList, List<Order> oldList){
            Set<Id> activatedOrderIds = new Set<Id>();
            //Create list of OrderIds
            for ( Integer i=0; i<newList.size(); i++ ){
                if ((newList[i].Status == Constants.ACTIVATED_ORDER_STATUS && newList[i].ActivatedDate != null)
                    && oldList[i].Status == Constants.DRAFT_ORDER_STATUS){
                    activatedOrderIds.add(newList[i].Id);
                }
            }
            RollUpOrderItems(activatedOrderIds);
        }
        /**
         * @name RollUpOrderItems
         * @description Given a set of Activated Order ids, query the child Order Items and related Products to calculate Inventory levels
         * @param Set<Id> activatedOrderIds
         * @return void
        **/
        public static void RollUpOrderItems(Set<Id> activatedOrderIds){
            //ToDo: Declare a Map named "productMap" of Ids to Product2 records
            Map<Id, Product2> productMap = new Map<Id, Product2>();
            Set<Id> productIds = new Set<Id>();
            //ToDo: Loop through a query of OrderItems related to the activatedOrderIds
            List<OrderItem> items = [SELECT Id, Product2Id, Quantity
                                    FROM OrderItem
                                    WHERE OrderId In :activatedOrderIds];
            for(OrderItem oi : items) {
                //ToDo: Populate the map with the Id of the related Product2 as the key and Product2 record as the value
                productIds.add(oi.Product2Id);
            }
            productMap = new Map<Id, Product2>([SELECT Id, Quantity_Ordered__c FROM Product2 WHERE Id IN :productIds]);
            AggregateResult[] groupedResults = [SELECT Product2Id, SUM(Quantity) activatedQuantity
                                                 FROM OrderItem
                                                 WHERE Product2Id In :productMap.keySet() GROUP BY Product2Id];
            for (AggregateResult ar : groupedResults)  {
                productMap.get((String) ar.get('Product2Id')).Quantity_Ordered__c = Integer.valueOf(ar.get('activatedQuantity'));
            }
            //ToDo: Perform an update on the records in the productMap
            if(productMap!=null && productMap.size()>0){
                update productMap.values();
            }
        }
    }
NagendraNagendra (Salesforce Developers) 
Hi Jagadeep,

Sorry for this issue you are encountering.

May I suggest you please check with below code snippet using which I have successfully passed the challenge.

OrderTrigger:
/**
 * @name orderTrigger
 * @description
**/
trigger orderTrigger on Order (after update) {
    OrderHelper.AfterUpdate(Trigger.New, Trigger.Old);
}
OrderHelper:
public without sharing class OrderHelper {
    /**
        * @name AfterUpdate
        * @description 
        * @param List<Order> newList
        * @param List<Order> oldList
        * @return void
    **/
    public static void AfterUpdate(List<Order> newList, List<Order> oldList){
        Set<Id> orderIds = new Set<Id>();
        for ( Integer i=0; i< newList.size(); i++ ){
            if ( newList[i].Status == Constants.ACTIVATED_ORDER_STATUS && oldList[i].Status == Constants.DRAFT_ORDER_STATUS ){
                orderIds.add(newList[i].Id);
            }
        }
        RollUpOrderItems(orderIds);
    }
    
    /**
        * @name RollUpOrderItems
        * @description Given a set of Activated Order ids, query the child Order Items and related Products to calculate Inventory levels
        * @param Set<Id> activatedOrderIds
        * @return void
    **/
    public static void RollUpOrderItems(Set<Id> activatedOrderIds){
        //ToDo: Declare a Map named "productMap" of Ids to Product2 records
        Map<Id, Product2> productMap = new Map<Id, Product2>(); 
        for(OrderItem orderLine : [SELECT Id, Product2Id, Product2.Quantity_Ordered__c, Quantity, Order.ActivatedDate
                                   FROM OrderItem WHERE OrderId IN : activatedOrderIds]){ 
                                  if(!productMap.containsKey(orderLine.Product2Id))
                                      productMap.put(orderLine.Product2Id, new Product2(Id =orderLine.Product2Id, Quantity_Ordered__c=0)); 
        }
        
        for(AggregateResult ag : [SELECT Sum(Quantity), Product2Id FROM OrderItem WHERE Product2Id IN : productMap.keySet() Group By Product2Id]){
            Id product2Id = (Id)ag.get('Product2Id');
            Product2 prod = productMap.get(product2Id);
            prod.Quantity_Ordered__c = (Decimal)ag.get('expr0');
            productMap.put(product2Id , prod);
        }
        
        /*for(OrderItem orderLine : [SELECT Id, Product2Id, Product2.Quantity_Ordered__c, Quantity, Order.ActivatedDate
                                   FROM OrderItem WHERE OrderId IN : activatedOrderIds]){ 
                                       if(orderLine.Order.ActivatedDate != null){ 
                                           if(!productMap.containsKey(orderLine.Product2Id)){ 
                                               Product2 prod = new Product2(); 
                                               prod.Id = orderLine.Product2Id; 
                                               Decimal prevQuantity = 0; 
                                               if(orderLine.Product2.Quantity_Ordered__c !=null) 
                                                   prevQuantity = orderLine.Product2.Quantity_Ordered__c; 
                                               prod.Quantity_Ordered__c = prevQuantity + orderLine.Quantity; 
                                               productMap.put( orderLine.Product2Id , prod); 
                                           }else{ 
                                               Product2 existingProd = productMap.get(orderLine.Product2Id); 
                                               existingProd.Quantity_Ordered__c = existingProd.Quantity_Ordered__c + orderLine.Quantity; 
                                               productMap.put(orderLine.Product2Id, existingProd); 
                                           } 
                                       }else if(orderLine.Order.ActivatedDate == null){ 
                                           if(!productMap.containsKey(orderLine.Product2Id)){ 
                                               Product2 prod = new Product2(); 
                                               prod.Id = orderLine.Product2Id; 
                                               Decimal prevQuantity = 0; 
                                               if(orderLine.Product2.Quantity_Ordered__c !=null) 
                                                   prevQuantity = orderLine.Product2.Quantity_Ordered__c; 
                                               prod.Quantity_Ordered__c = prevQuantity - orderLine.Quantity; 
                                               productMap.put( orderLine.Product2Id , prod); 
                                           }else{ 
                                               Product2 existingProd = productMap.get(orderLine.Product2Id); 
                                               existingProd.Quantity_Ordered__c = existingProd.Quantity_Ordered__c - orderLine.Quantity; 
                                               productMap.put(orderLine.Product2Id, existingProd); 
                                           } 
                                       } 
                                   } */
        try { 
            if(productMap.values() != null && productMap.values().size() > 0){ 
                update productMap.values(); 
            } 
        }catch ( Exception e ){ 
            System.debug('#### Exception Executed : '+e.getStackTraceString()); 
        } 
    }
    
}
Hope this should help you pass the challenge.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra

 
jagadeep kjagadeep k
Hi Nagendra,
I am getting another error
Challenge Not yet complete... here's what's wrong:
Ensure that the Quantity Ordered field on the Product object is correctly calculated when an Order is Activated.
Could you guide me what to do with this error.
Thanks,
Jagadeep
Matthew Zwalsky 2Matthew Zwalsky 2

I was receiving the same error.

It seems this was fixed when I casted the aggregate result to a decimal instead of an integer.

Doesn't really make sense because I don't believe it would be possible to order a 'half product', but who knows

chirag patel 189chirag patel 189
Please make sure your if condition looks like below when you are creating list of activated order list.
if ( newList[i].Status == Constants.ACTIVATED_ORDER_STATUS && oldList[i].Status == Constants.DRAFT_ORDER_STATUS ){
orderIds.add(newList[i].Id);
}
ANAND KAPKARANAND KAPKAR
Hi @Chirag Patel, I am getting an error
Challenge Not yet complete... here's what's wrong:
Ensure that the Quantity Ordered field on the Product object is correctly calculated when an Order is Activated.
Could you guide me on what to do with this error?
Thanks,