Created
March 10, 2020 19:48
-
-
Save justin-lyon/2b6215b7321b134d8051a06493590d78 to your computer and use it in GitHub Desktop.
Single insert Save multiple records with relationships using External ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a guid for each record. | |
String guid1 = generateGuid(); | |
String guid2 = generateGuid(); | |
String guid3 = generateGuid(); | |
// Assign a guid as external id to each record | |
Map<String, MyObject__c> objects = new Map<String, MyObject__c>(); | |
objects.put(guid1, new MyObject(GUID__c = guid1)); | |
objects.put(guid2, new MyObject(GUID__c = guid2)); | |
objects.put(guid3, new MyObject(GUID__c = guid3)); | |
// Set the Relationship object to the Parent record. Not the Parent ID. | |
objects.get(guid1).ParentMyObj__r = objects.get(guid2); | |
objects.get(guid2).ParentMyObj__r = objects.get(guid3); | |
// Insert the Records | |
insert objects.values(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment