0

I have a use case where i have a list of orderItems which contains duplicates let us say:

OrderItem:{Product2__c=01t6F0000CCXXXXXX, Id=8026F00000EqzYYYYY}, 
OrderItem:{Product2__c=01t6F00000CCXXXXXX, Id=8026F00000Eqzba2AB}, 
OrderItem:{Product2__c=01t6F00000CCXXXXXX, Id=8026F00000Eqrbs3AB}, 
OrderItem:{Product2__c=01t6F000009kSSSSSS, Id=8026F00000Eqzx1RRR} 

here first 3 orderitems are duplicate based on the field Product2__c so i want to make a seperate list of id's for duplicate records and unique records i have tried n implemented the below code but its not working as expected.

 List<OrderItem> ListOI = new List<OrderItem>();<br/>
Set<String> DuplicateId = new Set<String>();<br/>
List<OrderItem> Item = [Select Id,Product2__c from Orderitem where OrderId =:OrderMap.keySet()];<br/>
        for(OrderItem itemm : Item)
         {
             if(!DuplicateId.contains(itemm.Product2__c ))
             {
                 ListOI.add(itemm);
                 DuplicateId.add(itemm.Id);
             } else {
                 DuplicateId.add(itemm.Id);
             }
         } 

can any one suggest me how to implement this in proper way

7
  • @identigral thanks for your response the link which you have send is absolutely valid but if you look at my issue duplication i am checking for the field "Product2__c" and i want to fetch that "Id" of that duplicate record in a seperate list.
    – vishal
    Commented Aug 6, 2020 at 18:56
  • Can you explain what you are expecting to happen versus what is actually happening?
    – Zach
    Commented Aug 6, 2020 at 19:34
  • @zach i am expecting that what ever duplicate records are there based on "Product2__c" i want to fetch that record "Id" in a seperate list.
    – vishal
    Commented Aug 6, 2020 at 19:36
  • What's not working as expected?
    – Zach
    Commented Aug 6, 2020 at 19:41
  • @zach the logic which i mentioned fetches the product id for which i cannot perform the update activity so i require the "Id" field value of that duplicate records to perform further operation. if not any other approach is also fine as my approach may be wrong.
    – vishal
    Commented Aug 6, 2020 at 19:48

1 Answer 1

0

How about this:

Set<String> DuplicateId = new Set<String>();
Map<Id,OrderItem> uniqueItem = new map<Id,ordereditem >;

        for(OrderItem itemm : [Select Id,Product2__c from Orderitem where OrderId  in :OrderMap.keySet()])
         {
             if(!uniqueItem.containsKey(itemm.Product2__c ))
             {
                 UniqueItem.put(itemm.Product2__c,itemm);
                 
             } else {
                 DuplicateId.add(itemm.Id);
             }
         } 

Then use the values to get the unique ones

Not the answer you're looking for? Browse other questions tagged .