How to limit number of parent records by their related child records?


Question.

I am having some trouble limiting the number of parent records inserted into a new org with a limited number of child records.

{
   objects: [
     {
         "query": "SELECT Id FROM Object_A__c",
         // Other object props
     },
     {
         "query": "SELECT Id, Object_A__c FROM Object_B__c LIMIT 10",
         // Other object props
     }
   ]
}

How do I limit the imported Object_A__c records to only the parents of the 10 selected Object_B__c records?


Answer.

Set Object_A__c to master=false.

The Object_B__c remains a master object and it will get 10 records as per its SOQL statement.

The Object_A__c becomes a slave object and it will get only those records, which are related to the given 10 Object_B__c master records:


   objects: [
     {
         "query": "SELECT Id FROM Object_A__c",
          "master": false // This will select all Object_A__c's records which are the parent of the given 10 Object_B__c's records  
         // Other object props
     },
     {
          "query": "SELECT Id, Object_A__c FROM Object_B__c LIMIT 10",
          // Other object props
     }
   ]
}
Last updated on 17th May 2023