2

I'm stuck on retrieving account for particular orders. My final requirement is to build a list with all orders details and the account it belongs to. So here's my story so far...

I have 2 new custom objects, SUBSCR_SYSTEMS__c and orders__c. SUBSCR_SYSTEMS__c holds the legacy ids. I need to a build a list of orders comparing few fields in SUBSCR_SYSTEMS__c.

  1. lst_PartnersAcc contains partner ids as customer legacy numbers and it's related accounts (which is my target to display and I'm unable to).
  2. orders contains a list of orders that I'm displaying on VF. But here, account__c is empty. So it need to be updated programmatically with values from lst_PartnersAcc.

    public class OrderTrackingPartners{
    
    public List<orders__c> orders {get; set;}
    public OrderTrackingPartners() {   
    }
    
    
      public list<orders__c> getstart() {
     // territory
    
     Map<Id,UserTerritory> UserTerritoryCurrentUserMap = new    Map<Id,UserTerritory>([Select u.UserId, u.TerritoryId, u.IsActive, u.Id  From UserTerritory u Where u.isActive=true and u.userId =: UserInfo.getUserId()]);
    set<Id> TerritoryIdSet = new set<Id>();
    for(UserTerritory ut:UserTerritoryCurrentUserMap.values())
    {
      TerritoryIdSet.add(ut.TerritoryId);
    }    
    
    list<Group> map_group = [Select Id, RelatedId from Group where Type='Territory' AND RelatedId IN : TerritoryIdSet];
    
    
    //get legacy customer number from logged in user info
    
    //***Get Partner ids ***//
     List<SUBSCR_SYSTEMS__c> lst_PartnersAcc = [SELECT LEGACY_CUST_NUM__c,Account__c
                                             FROM SUBSCR_SYSTEMS__c WHERE Account__c IN                                                  
                                             (Select  AccountId from AccountShare where ( UserOrGroupId IN : map_group OR  UserOrGroupId =:UserInfo.getUserId()) AND RowCause IN ('Territory', 'TerritoryManual', 'TerritoryRule'))
                                             ];
    
    Set*String*tempList = new Set*String*();
    //Set <Id>tempListAcc = new Set<Id>();
     for(SUBSCR_SYSTEMS__c s : lst_PartnersAcc) {
    tempList.add(s.LEGACY_CUST_NUM__c);
    // tempListAcc.add(s.Account__c);
     }
    
    //***Retreive all orders***//
    
     List<orders__c> orders =[SELECT Orders__c,id, customer_order_number__c,name,order_no__c,account__c,account__r.name,number_of_line_items__c,number_of_items_open__c,lines_items_shipped__c,number_of_items_delivered__c,order_date__c,Bill_to__c,Payer__c,Ship_To__c,Sold_to__c FROM orders__c   
                             WHERE  Bill_to__c IN: tempList OR
                                    Payer__c IN: tempList OR 
                                    Ship_To__c IN: tempList OR
                                    Sold_to__c IN: tempList
                                 ]; 
    
     if(orders.size()>0) {            
        if(String.isNotBlank(orders[0].Ship_To__c))   { 
        List<SUBSCR_SYSTEMS__c> Acc=[SELECT Account__c FROM SUBSCR_SYSTEMS__c where LEGACY_CUST_NUM__c=:orders[0].Ship_To__c];
    
    
        for(orders__c go: orders){
        go.account__c=Acc[0].Account__c;
        }
    
           system.debug('-----finale-------'+Acc);
       } 
    } 
    
    return orders;                                
    }                                
    
    }
    

From the VF output, I can see that correct orders are being retrieved, but I'm unable to get the associated accounts to a particular order.

Any help or suggestion would be great! Thanks

EDIT after @mihai's suggestion..

     Map<String,orders__c> mapOfOrders = new Map<String,orders__c>();

     List<orders__c> orders = [SELECT Ship_To__c,Orders__c,id, customer_order_number__c,name,order_no__c,account__c,account__r.name,number_of_line_items__c,number_of_items_open__c,lines_items_shipped__c,number_of_items_delivered__c,order_date__c,Bill_to__c,Payer__c,Sold_to__c FROM orders__c   
                             WHERE  Bill_to__c IN: tempList OR
                                    Payer__c IN: tempList OR 
                                    Ship_To__c IN: tempList OR
                                    Sold_to__c IN: tempList
                                 ]; 
   for(orders__c order : orders){
    mapOfOrders.put(order.Ship_To__c, order);
}
  List<SUBSCR_SYSTEMS__c> subList = [Select Account__c from SUBSC_SYSTEMS__c Where LEGACY_CUST_NUM__c=: orders.keySet()];
for(SUBSCR_SYSTEMS__c sub : subList){
   if(String.isNotBlank(orders.get(sub.LEGACY_CUST_NUM__c))){
       orders.get(sub.LEGACY_CUST_NUM__c).Account__c=sub.Account__c ;
   //since my aim is to copy account__c from SUBSCR_SYSTEMS__c to orders__c. Both has same field name.
   }
}

ERROR: Error: Compile Error: Method does not exist or incorrect signature: [List].keySet(). As far as I know keySet accepts a set of orders.

1
    Map<String,orders__c) mapOfOrders = new Map<String,orders__c>();
    Set<Id> ordersIds = new Set<Id>();

    List<orders__c> orders = [SELECT Ship_To__c,Orders__c,id, customer_order_number__c,name,order_no__c,account__c,account__r.name,number_of_line_items__c,number_of_items_open__c,lines_items_shipped__c,number_of_items_delivered__c,order_date__c,Bill_to__c,Payer__c,Sold_to__c FROM orders__c   
                                 WHERE  Bill_to__c IN: tempList OR
                                        Payer__c IN: tempList OR 
                                        Ship_To__c IN: tempList OR
                                        Sold_to__c IN: tempList
                                     ]; 
    for(orders__c order : orders){
        mapOfOrders.put(order.Ship_To__c, order);
        ordersIds.add(order.Id);
    }
    List<SUBSCR_SYSTEMS__c> subList = [Select Account__c from SUBSC_SYSTEMS__c Where LEGACY_CUST_NUM__c IN :ordersIds];
    for(SUBSCR_SYSTEMS__c sub : subList){
       if(String.isNotBlank(mapOfOrders.get(sub.LEGACY_CUST_NUM__c))){
            sub.Account__c =  mapOfOrders.get(sub.LEGACY_CUST_NUM__c).Account__c;
       }
    }

Let me know if it helps, basically looped through the orders and put them into a map and the key of the map is the Ship_To__c which I'm using it further to get the relevant information in the other for loop.

|improve this answer|||||
  • Thanks @mihai-neogoe for your wonderful reply. Account__c field from SUBSC_SYSTEMS__c is available but account__c from orders__c is empty. All I need to display is a list of orders with account name from SUBSC_SYSTEMS__c. I did understand what you tried to show me :) – MnZ Jan 27 '15 at 14:40
  • 1
    No worries. This is the sort of pattern I use when I try to avoid nested for loops and Maps are easy to use. Just apply the same logic as I showed you to your code as you know it better. Try it and let me know if it works. – Mihai Neagoe Jan 27 '15 at 14:46
  • Hi @mihai I tried your code to meet my requirement. But I faced problems. I have updated my question with an EDIT. Thanks – MnZ Jan 28 '15 at 16:16
  • 1
    Hello, sorry my bad the list doesn't have the method keySet(), only maps. What I've done is I created a set called ordersIds and then change the SOQL query to select all the records IN :ordersIds. I'm populating the set in the for loop where I populate the map. You can see my updated answer. – Mihai Neagoe Jan 28 '15 at 16:23
  • Hi @Mihai. I get the same kind of error as Error: Compile Error: Method does not exist or incorrect signature: [List<orders__c>].get(String) for the final statement----> sub.Account__c = orders.get(sub.LEGACY_CUST_NUM__c).Account__c; Meanwhile I'm trying to work with the map syntax. – MnZ Jan 29 '15 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.