You need to sign in to do that
Don't have an account?
List to remove duplicates
All
Here is my query
orderHistListRec33 = [Select Name,Account__c,Order_Status__c, Case_Number__c, Product_Description__r.Is_Package__c, Product_Description__r.Name,CreatedDate
From Order_History__c order by createdDate dsc ];
I get
List is
OrderHistoryNumber (Name): 123 Product_Description__r.Name:Branding CreatdeDate : 6/25/13
OrderHistoryNumber(Name) : 124 Product_Description__r.Name:CHAT CreatdeDate : 6/25/13
OrderHistoryNumber (Name): 125 Product_Description__r.Name:Branding CreatdeDate : 6/22/13
OrderHistoryNumber(Name) : 126 Product_Description__r.Name:CHAT CreatdeDate : 6/22/13
I want to eleiminate duplicate Product_Description__r.Name and get most recent i.e.
OrderHistoryNumber (Name): 123 Product_Description__r.Name:Branding CreatdeDate : 6/25/13
OrderHistoryNumber(Name) : 124 Product_Description__r.Name:CHAT CreatdeDate : 6/25/13
how do i modify my query .
I would recommend creating a map of Product descriptions as the key and orders as the value, and compare the order that you are trying to existing keys in the map. If the key already exists, compare the two created dates, and keep the newest one. Then you can update the list with the newly filtered values.
orderHistListRec33 = [Select Name,Account__c,Order_Status__c, Case_Number__c, Product_Description__r.Is_Package__c, Product_Description__r.Name,CreatedDate
From Order_History__c order by createdDate dsc ];
Map<String, Order_History__c> orderMap = new Map<String, Order_History__c>();
for(Order_History__c o : orderHistListRec33){
if(orderMap.containsKey(o.Product_Description__r) == true){
Order_History__c tmp = orderMap.get(o.Product_Description__r);
if(tmp.CreatedDate < o.CreatedDate){
orderMap.remove(o.Product_Drescription__r);
orderMap.put(o.Product_Drescription__r, o);
}
} else {
orderMap.put(o.Product_Drescription__r, o);
}
}
List<Order_History__c> newList = orderMap.values();
The key will contain Product_Description__r.Name, while entering a new entry in the map just check if the record is laredy there if yes comapre the createdDate and replace the value accordingly
Follow like this to avoid duplicates
map<string,Order_History__c> maps=new map<string,Order_History__c>();
For(Order_History__c oh:[Select Name,Account__c,Order_Status__c, Case_Number__c, Product_Description__r.Is_Package__c, Product_Description__r.Name,CreatedDate
From Order_History__c order by createdDate desc ])
{
if(!maps.containsKey(oh.Product_Description__r.Name))
{
maps.put(oh.Product_Description__r.Name,oh);
}
}
Then finally retrieve all mapvalues
HI Sam Ready
I tried the code I dont why but when i debug i still get the duplicate values. Dont know where i am doing wrong. Here is the code.
I'm not sure why that is happening....
Try changing this
to this