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.
- 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).
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.