I have a list of rules that are a detail record for requirements. If any two rules have the same parent I would like to remove one of them from the list (doesn't matter which one). I thought I could do this by creating a set, but am unsure of how to create the set based on the master-detail id, rather than the object id?

List<ProgramEligibilityRules__c> progrule = [SELECT Id, Requirement__c FROM ProgramEligibilityRules__c WHERE Id IN: reqs]; 

This code will do it

Set<Id> uniqueReqIds = mew Set<Id>();
Map<Id, ProgramEligibilityRules__c> mapForUnique = new Map<Id, ProgramEligibilityRules__c>();

for (ProgramEligibilityRules__c> rule : [SELECT Id, Requirement__c FROM ProgramEligibilityRules__c WHERE Id IN: reqs]) {
  mapForUnique.put(rule.Requirement__c, rule);


The first Set will give you the unique reqs IDs. The Map will put one of the associated ProgramEligibilityRules__c record (the last) as the value.

Obviously, you don't need both. I just added them so you can pick the one you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.