2

I have a record collection for Object A, which I loop over and for each record in that collection I create a new record for object B. These records are assigned to a second record collection.

At the end of the loop I create the records from the second record collection.

I now want to use the ID's from the newly created records and update the existing records, so that they have a lookup to these newly created records.

I'm struggling to understand, how I would iterate over the second record collection (i.e within a loop) which now has established ID's per record, and at the same time, iterate over the first record collection so as to assign the corresponding ID of the new record, to the existing record from the first collection set.

i.e

Collection Set A Record 1A Record 2A

Collection Set B Record 1B - Linked to Record 1A Record 2B - Linked to Record 2B

I attempted to access the first loops 'loop variable' in the second loop so as to set the ID, but I realise now, you can only access a loop variable within that specific loop.

Flow Logic

0

I'm not sure I understand your requirements, but from what I understand I would start this way :

flow

  1. get you object A the way you want, store in MyObject variable
  2. get its children, save in a variable objectAChildren
  3. delete ID from MyObject, assuming you want to clone it in step 4
  4. create Object B, ie cloning MyObject. Id of record Object B is now assigned to MyObject.Id
  5. Loop through objectAChildren, name the local variable CurrentAChild
  6. Save the CurrentAChild.Id to curChildId
  7. update CurrentAChild :

    CurrentAChild.ParentRelationField = MyObject.Id
    CurrentAChild.Id = null

  8. create the child for B, using CurrentAChild variable as data source

  9. update record (object A child) whose ID is curChildId, set YourLookupRelationField__c to CurrentAChild.Id

Your object B has now children : clones of obj A children
Your object A children now have a lookup pointing to theirB child counterpart.

  • Hi Emmanuel, I dont think my original post was that clear, but essentially what I was: - start with 1 record collection - clone it - and relate the new record collection individual records to their cloned counterpart, both ways. i.e a lookup from each to the other I was trying to avoid putting any DML (update, create) in the loop, but rather collecting them to a record collection and then actioning the DML outside the loop. Also what is curChildId in your example, I get lost at point 6. – C. Ricker Aug 15 at 9:21
  • It's the id of the current child, the loop variable. In other words it's the id of the model for the new B child to be created. I keep track of it so that I can later update the matching record with the id of the new B child. To set the relationship. – Emmanuel BRUNO Aug 15 at 9:23
0

Another solution a bit more complicated but without any DML in loops :

flow

I used Accounts and Contacts, and the Description field to simulate the lookup.

  1. get you object A the way you want, store in MyObject variable
  2. get its children, save in a variable objectAChildren
  3. delete ID from MyObject, assuming you want to clone it in step 4
  4. create Object B, ie cloning MyObject. Id of record Object B is now assigned to MyObject.Id
  5. Loop through objectAChildren, name the local variable CurrentAChild
  6. Add the CurrentAChild record to a variable, a list of Contacts named ARecords
  7. Update the current child :
    {!CurrentAChild.Description} = {!CurrentAChild.Id} //your lookup field
    {!CurrentAChild.AccountId}={!MyObject.Id} // MyObject.Id is now ObjectB.Id {!CurrentAChild.Id}=null
    Be sure to set Id to null AFTER you assigned it to Description (or in your case the lookup field).
  8. Add the modified current child to a BRecords List variable :
    {!BRecords} Add {!CurrentAChild}

End of the loop, you have 2 lists, one with the source children objects, one with the objects to clone.

  1. Create the children for object B : Create records, Mulitple, {!BRecords}
  2. Loop through BRecords, local variable {!currentBChild}
  3. Inner loop through ARecords, local variable {!currentAChild}
  4. Check if the current B child is a clone of the current A child, remember you saved A.Id in B.Description (or lookup field)

decision

  1. update the source child lookup field :
    {!CurrentAChild.Description} = {!currentBChild.Id}
  2. Add the child to a list variable named AChildrenToUpdate (obviously you could skip this step and later update the ARecords list but that's how I did it to get things clear) :
    {!AChildrenToUpdate} = {!CurrentAChild}

  3. Don't forget to plug the decision default outcome to the loop

  4. Finally update the {!AChildrenToUpdate} list.

You now have an object B which is a clone of Object A, B children are clones of A children, and each A child point to its counterpart B child, and each B child to its counterpart A child.

Hope it's clear enough.

  • not very far from what you did by the way :) – Emmanuel BRUNO Aug 15 at 10: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.