+ Start a Discussion

Quick question about inserting records.

Hello, I will make this quick (thanks in advance for any help).  I have two objects, Sold_Account__c (SA) and Particpation_Detail__c (PD).  Using apex I am looking at list of SA records and determining if a corresponding PDs need to be created.  However, once a PD has been inserted I need to be able to add the ID for the PD to the corresponding SA record (and then of course update all of those records as well).  Currently I am putting all of the SAs that need to have a PD created for them into a List<Sold_Account__c>, then cycling through that list ( For() ) of SAs to create a List<Participation_Detail__c> of PDs to insert (using contextual data to define values for fields on each PD, etc).  My question is how do I create and then insert the list of PDs in a way where I can keep some sort of association between them and the corresponding SA so I can then add the PD's ID to the field on the SA and the update them?


I know I can't be the first person to need to do this.  Any help would be appreciated.





Do I just need to make a 'wrapper' (or whatever the term is) class (lets call it SAtoPDConnector() )that holds both the already created SA and the 'to be created' PD.  And once the list of PDs is inserted I can basically say:

public class SAtoPDConnector{
public Sold_Accounts__c sa = new Sold_Accounts__c();
public Participation_Detail__c pd= new Participation_Detail__c();
List<SAtoPDConnector> connections = new List<SAtoPDConnector>();
List<Participation_Detail__c> pdToBeCreated = new List<Participation_Detail__c>();
//code code code blah blah blah to populate the two lists above appropriately (basically creating a PD and then adding it to both the list of PDs to created and then creating a new connector and setting its SA = to the corresponding SA and the PD = to the PD just added to the list to be created, then adding the class to list of 'connections') and then do this:

Insert (pdToBeCreated);
List<Sold_Accounts__c> soldAccountsToUpdate = new List<Sold_Accounts__c>();
For(SAtoPDConnector currentConnection :connections){
    Sa.Participation_Detail__c = pd.id;     soldAccountsToUpdate.add(sa);
Update soldAccountsToUpdate;

Is that how this is done?  I guess the real basis of this question is whether apex understands that the PD in the list to be inserted and the one in the list of 'connections'  are the same 'entity' and so once one is inserted if it will automatically assign the ID returned to the corresponding PD in the wrapper class.


Jeremy Stender


If I'm understanding your requirement correctly....


First you capture the list of SAs as you are doing now.


Instead of building a list of PDs, create a Map<ID, Participation_Detail__c> where the ID field is the ID of the SA.

Each PD you create is placed in the map.

You then insert the PDs by using the Map Values() method as parameter to the insert.

You then loop through the Map elements and set the PD ID for each entry into the corresponding SA.


It will look something like this (pseudo code):


List<Sold_Account__c> salist = query or function to obtain SAs to update

Map<ID, Sold_Account__c> samap = new Map<ID, Sold_Account__c>(salist);

Map<ID, Participation_Detail__c> satopdmap = new Map<ID, Participation_Detail__c>();

for(Sold_Account__c sa: salist)


   satopdmap.put(sa.id, new pd with fields set the way you want them)


insert satopdmap.values();  // Insert PDs

for(ID said: satopdmap.keyset())


  samap.get(said).pdid = satopdmap.get(said).id;




This is about as efficient a solution as you'll get.

If you have larger numbers of accounts or the possibility that a large number of accounts will need PDs, you'll need to analyze and refactor the code for limits. Limits to watch for include # of SA records retrieved, script lines during the processing, and selective SOQL on the query to find SA records that need a PD.




Thanks Dan!  I came up with my other solution (in my edits above) but I was thinking about doing something like your solution initially, but I didn't know about the values() method on Maps (go figure how I missed that pretty important little feature). 


Thanks again, that will work perfectly! 


(4 months programming apex/VF now and counting!)