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
Adil_SFDCAdil_SFDC 

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 .

SamReadySamReady

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();

Avidev9Avidev9
What you can do is create a Map<String, Order_History__c > and put all the value there.

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
AKS018AKS018

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

Adil_SFDCAdil_SFDC

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.

trigger orderHistoryModified on Case(after update) {
Map<Id,Case> oldMap = Trigger.OldMap;
map<Id,Case> newMap = Trigger.newMap;
set<Id> caseIds = new set<Id>();
list<Order_History__c> orderHistList = new list<Order_History__c>();
list<Order_History__c> updateOHL = new list<Order_History__c>();

//Deliverable 33 Changes
set<Id> caseIdSet33 = new set<Id>();
set<Id> accIdSet33 = new set<Id>();
set<Id> aptsProdId = new set<Id>();
list<Order_History__c> orderHistListRec33 = new list<Order_History__c>();
list<Order_History__c> updateOHLRecToUpdate33 = new list<Order_History__c>();

system.debug('inside 33 ::'+trigger.new);
Map<Id,Case> mapAptsProd = new Map<Id,Case>([Select id,Status,(Select Product_Description__c From Case_Products__r), AccountId, 
                                                    Type 
                                               from case where id in : newMap.KeySet() and Status = 'Saved']);
    for(Case cas : mapAptsProd.values())
    {
        list<Case_Product__c> listCasProd = new list<Case_Product__c> ();
        listCasProd = cas.Case_Products__r;
        if(cas.Type == 'Cancel'  && oldMap.get(cas.Id).Status != 'Saved' && 
           cas.Status == 'Saved' && listCasProd.size() > 0)
        {
            system.debug('inside if condition :'+listCasProd);
            accIdSet33.add(cas.AccountId);
             system.debug('**accIdSet33'+accIdSet33);
            for(Case_Product__c tempCasProd: listCasProd){
                aptsProdId.add(tempCasProd.Product_Description__c); 
                 system.debug('$$aptsProdId'+aptsProdId); 
            }
        }
    }
    
    if(aptsProdId.size() > 0 && accIdSet33.size() > 0)
    {
        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 
                               where Account__c in :accIdSet33 
                                 and Product_Description__c in :aptsProdId
                                 and Order_Status__c in ('Cancelled','Cancellation Pending')order by createdDate desc ];
    
      system.debug('YYOrderHistlIST'+orderHistListRec33);
    }

Map<String, Order_History__c> orderMap = new Map<String, Order_History__c>();
for(Order_History__c o : orderHistListRec33){
    if(orderMap.containsKey(o.Product_Description__c) == true){
         Order_History__c tmp = orderMap.get(o.Product_Description__c); 
         if(tmp.CreatedDate < o.CreatedDate){
             orderMap.remove(o.Product_Description__c);
             orderMap.put(o.Product_Description__c, o);
         }
    } else {
        orderMap.put(o.Product_Description__c, o);
    }
}
 
List<Order_History__c> newList = orderMap.values();
   system.debug('AAAB'+newList);
   
   
  
    
     if(newList.size() > 0)
    {
        for(Order_History__c tempOH:newList){
            //Case tempCase = newMap.get(tempOH.Case_Number__c);
            tempOH.Scheduled_Cancel_Date__c = null;
            tempOH.Cancel_Reason__c = '';
            tempOH.Pending_Cancel_Notes__c = '';
            tempOH.Order_Status__c = 'Active';
            updateOHLRecToUpdate33.add(tempOH);
              system.debug('XXupdateOrderHist'+updateOHLRecToUpdate33);
        }
    }
   
  /*  
    if(orderHistListRec33.size() > 0)
    {
        for(Order_History__c tempOH: orderHistListRec33){
            //Case tempCase = newMap.get(tempOH.Case_Number__c);
            tempOH.Scheduled_Cancel_Date__c = null;
            tempOH.Cancel_Reason__c = '';
            tempOH.Pending_Cancel_Notes__c = '';
            tempOH.Order_Status__c = 'Active';
            updateOHLRecToUpdate33.add(tempOH);
              system.debug('XXupdateOrderHist'+updateOHLRecToUpdate33);
        }
    }*/
    
    
   
    if(updateOHLRecToUpdate33 != null && updateOHLRecToUpdate33.size()>0){
    	 system.debug('yyy'+updateOHLRecToUpdate33);
        update updateOHLRecToUpdate33;
                      system.debug('BBB'+updateOHLRecToUpdate33);
        
    }
    
}

 

SamReadySamReady

I'm not sure why that is happening....

 

Try changing this

 if(orderMap.containsKey(o.Product_Description__c) == true){
         Order_History__c tmp = orderMap.get(o.Product_Description__c); 
         if(tmp.CreatedDate < o.CreatedDate){
             orderMap.remove(o.Product_Description__c);
             orderMap.put(o.Product_Description__c, o);
         }
    } else {
        orderMap.put(o.Product_Description__c, o);
    }
to this
     if(!orderMap.containsKey(oh.Product_Description__c))
          {
                 orderMap.put(o.Product_Description__c,o);
          }
Adil_SFDCAdil_SFDC
I tried AKS018 Solution that seems to be working.