public class DuplicateProductChecker {
public class PricebookEntry {
public String Id { get; set; }
public Decimal UnitPrice { get; set; }
public String Product2Id { get; set; }
public PricebookEntry(String Id, Decimal UnitPrice, String Product2Id) {
this.Id = Id;
this.UnitPrice = UnitPrice;
this.Product2Id = Product2Id;
}
}
public static Set<String> getProduct2Ids(List<PricebookEntry> objectList) {
Set<String> product2Ids = new Set<String>();
for (PricebookEntry entry : objectList) {
product2Ids.add(entry.Product2Id);
}
return product2Ids;
}
public static List<String> findDuplicates(List<PricebookEntry> objectList1, List<PricebookEntry> objectList2) {
Set<String> set1 = getProduct2Ids(objectList1);
Set<String> set2 = getProduct2Ids(objectList2);
set1.retainAll(set2);
return new List<String>(set1);
}
public static void main() {
// Sample data
List<PricebookEntry> objectList1 = new List<PricebookEntry>{
new PricebookEntry('01uIo000000nM1ZIAU', 720.00, '01t5i000007WIHkAAO'),
new PricebookEntry('01uIo000000nHpiIAE', 1300.00, '01tIo000001W5MlIAK'),
new PricebookEntry('01uIo000000nHpjIAE', 650.00, '01tIo000001W3YLIA0'),
new PricebookEntry('01uIo000000nHpkIAE', 1500.00, '01tIo000001W5MrIAK'),
new PricebookEntry('01uIo000000nHplIAE', 750.00, '01tIo000001W5MqIAK'),
new PricebookEntry('01uIo000000nHpmIAE', 110.00, '01tIo000001W5NoIAK'),
new PricebookEntry('01uIo000000nHpnIAE', -100.00, '01tIo000001W5O3IAK')
};
List<PricebookEntry> objectList2 = new List<PricebookEntry>{
new PricebookEntry('01uIo000000nIwYIAU', 100.00, '01tIo000001WdGqIAK'),
new PricebookEntry('01u5i000004hWm0AAE', 595.00, '01t5i000007WIHkAAO'),
new PricebookEntry('01uJ3000000ui9SIAQ', 695.00, '01tJ3000000rDGuIAM'),
new PricebookEntry('01uIo000000nHokIAE', 1345.00, '01tIo000001W5MlIAK'),
new PricebookEntry('01uIo000000nHouIAE', 1520.00, '01tIo000001W5MrIAK'),
new PricebookEntry('01uIo000000nHl2IAE', 695.00, '01tIo000001W3YLIA0'),
new PricebookEntry('01uIo000000nHozIAE', 120.00, '01tIo000001W5NoIAK'),
new PricebookEntry('01uIo000000nHp0IAE', -100.00, '01tIo000001W5O3IAK'),
new PricebookEntry('01uIo000000nHopIAE', 775.00, '01tIo000001W5MqIAK')
};
// Finding duplicates
List<String> duplicates = findDuplicates(objectList1, objectList2);
System.debug('Duplicate Product2Id: ' + duplicates);
}
}
cHVibGljIGNsYXNzIER1cGxpY2F0ZVByb2R1Y3RDaGVja2VyIHsKICAgIHB1YmxpYyBjbGFzcyBQcmljZWJvb2tFbnRyeSB7CiAgICAgICAgcHVibGljIFN0cmluZyBJZCB7IGdldDsgc2V0OyB9CiAgICAgICAgcHVibGljIERlY2ltYWwgVW5pdFByaWNlIHsgZ2V0OyBzZXQ7IH0KICAgICAgICBwdWJsaWMgU3RyaW5nIFByb2R1Y3QySWQgeyBnZXQ7IHNldDsgfQogICAgICAgIAogICAgICAgIHB1YmxpYyBQcmljZWJvb2tFbnRyeShTdHJpbmcgSWQsIERlY2ltYWwgVW5pdFByaWNlLCBTdHJpbmcgUHJvZHVjdDJJZCkgewogICAgICAgICAgICB0aGlzLklkID0gSWQ7CiAgICAgICAgICAgIHRoaXMuVW5pdFByaWNlID0gVW5pdFByaWNlOwogICAgICAgICAgICB0aGlzLlByb2R1Y3QySWQgPSBQcm9kdWN0MklkOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIFNldDxTdHJpbmc+IGdldFByb2R1Y3QySWRzKExpc3Q8UHJpY2Vib29rRW50cnk+IG9iamVjdExpc3QpIHsKICAgICAgICBTZXQ8U3RyaW5nPiBwcm9kdWN0MklkcyA9IG5ldyBTZXQ8U3RyaW5nPigpOwogICAgICAgIGZvciAoUHJpY2Vib29rRW50cnkgZW50cnkgOiBvYmplY3RMaXN0KSB7CiAgICAgICAgICAgIHByb2R1Y3QySWRzLmFkZChlbnRyeS5Qcm9kdWN0MklkKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHByb2R1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