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
Sagar_SFDCSagar_SFDC 

Advance Apex superbadge challenge 2 Issue(Challenge Not yet complete... here's what's wrong: Ensure that orderHelper runs as the system.)

User-added image

Stuck in challenge 2 with the above issue. Below is my code.

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 with 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.ACTIVATED_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]);
        
        AggregateResult[] groupedResult = [SELECT Product2Id, SUM(Quantity) activatedQuantity FROM OrderItem
                                             WHERE Product2Id IN :productMap.keySet() GROUP BY Product2Id];
        
        //ToDo: Loop through a query that aggregates the OrderItems related to the Products in the ProductMap keyset
        for(AggregateResult ar : groupedResult){
            productMap.get((String)ar.get('Product2Id')).Quantity_Ordered__c = Integer.valueOf(ar.get('activatedQuantity'));
        }
        
        //ToDo: Perform an update on the records in the productMap
        update productMap.values();
    }

}
 
NagendraNagendra (Salesforce Developers) 
Hi Sagar,

The below code will work 100%.

Order Trigger:
/**
 * @name orderTrigger
 * @description
 **/
trigger OrderTrigger on Order (after update) {

  if (Trigger.new != null) {
    OrderHelper.AfterUpdate(Trigger.new, Trigger.old);
  }

}
Order Helper 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(); } } }
 For more information please check with below link from the forums community. Thanks,
Nagendra

 
Sagar_SFDCSagar_SFDC
Hi Nagendra,

I have checked your code.

It is same as my code even i tried using your code now but still getting the same issue.

Thanks,
Sagar sindhi
Samol SengSamol Seng
Remove the "with sharing" from the orderHelper class declaration fix the issue for me.
 
lynn lau 2lynn lau 2
@Samol Seng, thanks, it working now!
harishm reddyharishm reddy

Please guide to fix this error 
User-added image
code i have used please suggest me exact error
Trigger code:

trigger orderTrigger on Order (after update) {
    OrderHelper.AfterUpdate(Trigger.New, Trigger.Old);
}

class code:

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 && oldList[i].Status != Constants.ACTIVATED_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]);
        
        AggregateResult[] groupedResult = [SELECT Product2Id, SUM(Quantity) activatedQuantity FROM OrderItem
                                             WHERE Product2Id IN :productMap.keySet() GROUP BY Product2Id];
        
        //ToDo: Loop through a query that aggregates the OrderItems related to the Products in the ProductMap keyset
        for(AggregateResult ar : groupedResult){
            productMap.get((String)ar.get('Product2Id')).Quantity_Ordered__c = Integer.valueOf(ar.get('activatedQuantity'));
        }
        
        //ToDo: Perform an update on the records in the productMap
        update productMap.values();
    }

}
Sruthi PSruthi P

Add without sharing to the class as shaown below will resolve your issue.


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.ACTIVATED_ORDER_STATUS ){
                orderIds.add(newList[i].Id);
            }
        }
        if(orderIds.size() > 0)
            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){
        Map<Id,Product2> productMap;
        List<Id> productIds = new List<Id>();
        try{
            for(OrderItem OP : [SELECT Id, Product2Id, OrderId FROM OrderItem WHERE OrderId IN: activatedOrderIds]){
                if(OP.Product2Id != null)
                    productIds.add(OP.Product2Id);
            }
        }catch(EXception ex){
        
        }
        if(productIds.size() > 0){
            //ToDo: Declare a Map named "productMap" of Ids to Product2 records
            productMap =  new Map<Id,Product2>([Select id,Quantity_Ordered__c from product2 where id in :productIds]);
        
        }
        
        if(!productMap.isEmpty()){
            AggregateResult[] groupedResult = [Select Product2Id,sum(Quantity) totalQuantity 
                                                from OrderItem 
                                                where product2Id in :productMap.keySet() group by product2Id];
        
        
            for(AggregateResult result : groupedResult){
                productMap.get((String)result.get('Product2Id')).Quantity_Ordered__c = Integer.valueOf(result.get('totalQuantity'));
            }
            
            update productMap.values();
        }
    }

}