apex specialist super badge step 2 - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Ramani SreedharalaRamani Sreedharala 

apex specialist super badge step 2

Hi,

Please help me.

I am facing below error while trying to complete advanced apex specialist super badge step 2.

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.

--------
orderTrigger

/**
 * @name orderTrigger
 * @description
**/
trigger orderTrigger on Order(after update) {
    try{
        if(Trigger.isUpdate && Trigger.New != null){
            OrderHelper.AfterUpdate(Trigger.New,Trigger.old);
        }
    }catch(Exception e){
        System.debug('Exception in trigger -- '+e.getMessage());
    }
}
-------------------
OrderHelper

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> orderIds = new Set<Id>();
        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 ){
                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>();
        Set<Id> productIds = new Set<Id>();
        
        //ToDo: Loop through a query of OrderItems related to the activatedOrderIds
        for ( OrderItem oi : [SELECT Id, Product2Id, Product2.Quantity_Ordered__c, Quantity FROM OrderItem WHERE OrderId IN :activatedOrderIds]){
            productIds.add(oi.Product2Id);
        }
        
        //ToDo: Populate the map with the Id of the related Product2 as the key and Product2 record as the value
        productMap = new Map<Id, Product2>([Select Id, Quantity_Ordered__c FROM Product2 WHERE Id IN :productIds]);
        
        //ToDo: Loop through a query that aggregates the OrderItems related to the Products in the ProductMap keyset
    for(AggregateResult ar : [SELECT Product2Id, SUM(Quantity) activatedQuantity FROM OrderItem
         WHERE Product2Id IN :productMap.keySet() GROUP BY Product2Id]){
              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();
        }
    }
    
}
Best Answer chosen by Aishwarya (salesforce.com) 
Mira ShahMira Shah
Hello Ramani,
Please see below link of similiar issue. 
https://developer.salesforce.com/forums/?id=9060G0000005cdbQAA
https://lucascalegari.com/apex-specialist-superbadge-challenge-2/
https://developer.salesforce.com/forums/?id=9060G0000005YMlQAM

All Answers

Mira ShahMira Shah
Hello Ramani,
Please see below link of similiar issue. 
https://developer.salesforce.com/forums/?id=9060G0000005cdbQAA
https://lucascalegari.com/apex-specialist-superbadge-challenge-2/
https://developer.salesforce.com/forums/?id=9060G0000005YMlQAM
This was selected as the best answer
Ramani SreedharalaRamani Sreedharala
Hi Mira,

Thanks for your reply.

In the first link, they mentioned one error in the last. I got that error. no one adviced on that error.
Remaining two links are for apex specialist not for advanced apex specialist.

Please advice.
Atqua SiddiqueAtqua Siddique
Hi Mira ,

The second provided link is not working and giving a 404 error, can you please provide some other link that can help me out?