0

I'm new to programming so I apologize if I a missing a the concept here. I'm trying to respect bulkification when writing this class and was curious how you can do so given this scenario.

What I am going to do is feed a list of leadIds into the method and I want to do create a record if no record was found and use an existing an record if one was found. I know I need to query the existing record, but how do I go about adding those leads who did not have an existing record while still respecting bulk?

I understand that what I have below is not allowed but this is where my head went...

public class createLNBRecords {

private List<Lead_Not_Booked_Summary__c> createSummaryRecord(List<Lead> leadIds){
    List<Lead_Not_Booked_Summary__c> foundSummaries = [SELECT Id
                                                       FROM Lead_Not_Booked_Summary__c
                                                       Where Lead__c IN :leadIds];
    
    List<Lead> noSummary = [SELECT Id
                            FROM leadIds
                            Where NOT IN :foundSummaries];
    
}

}

1

You can in a variety of ways. You could create a wrapper:

class SummarySearchResults {
  Lead_Not_Booked_Summary__c[] foundSummaries;
  Lead[] noSummaries;
}
SummarySearchResults createSummaryRecord(Lead[] leadIds) {
  SummarySearchResults results = new SummarySearchResults();
  results.foundSummaries = [SELECT ... ];
  results.noSummaries = [SELECT ... ];
  return results;
}

Or just return a list of lists:

return List<List<sObject>> createSummaryRecord(Lead[] leadIds) {
  Lead_Not_Booked_Summary__c[] foundSummaries = [SELECT ... ];
  Lead[] noSummaries = [SELECT ... ];
  return new List<sObject> { foundSummaries, noSummaries };
}

Or a Map, or any other alternative structure you want.

3
  • thanks for your answer. What i'm still not understanding is how in either of these cases the noSummaries list is being populated. I shouldn't be able to query the lead because I won't have any criteria on the lead record that tells me no summary exists.
    – Cameron
    Apr 6 at 20:46
  • @Cameron SELECT Id FROM Lead WHERE Id = :leadIds AND Id NOT IN (SELECT Lead FROM Lead_Not_Booked_Summary__c WHERE Lead__c = :leadIds)
    – sfdcfox
    Apr 6 at 21:12
  • Okay I understand now thanks!
    – Cameron
    Apr 6 at 22:12

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.