Wednesday, January 25, 2017

Insert Multiple Parent and Child Records with External Id

Insert Multiple Parent and Child Records with External Id


I was referring this "Creating Parent and Child Records in a Single Statement Using Foreign Keys" page and thought, what if I get a requirement to insert multiple parents and child records in a single insert statement.
I have prepared a code snippet to insert multiple Account and Contact records in a single statement.
I have also created External Id at Account object to create reference from Contact record.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
List<SObject> lstAccount = new List<SObject>(); 
//holds list of Contact
List<SObject> lstContacts = new List<SObject>(); 
//final list for insertion
SObject[] sobjList = new List<SObject>(); 
String strInsertError = '';

//holds accountId and External Id in a Map
Map<String,String> accountContactMap = new Map<String,String>();
 
//loop through the salesProductsMap and create Sales Asset
for(Integer i=0;i<5;i++)
{
  Account accountObj = new Account(); 
 //generate random number and use that as External Id
  accountObj.External_Id__c =String.ValueOf(Integer.valueOf(math.rint(math.random()*1000000))); 
  accountObj.Name = 'ACCT_NM' + String.valueOf(i);
  accountContactMap.put(accountObj.Name,accountObj.External_Id__c);
  
  //finally add object to the list
  lstAccount.add(accountObj);
}
 
//loop through to create Contact record
for(Integer i=0;i<5;i++)
{
  Contact contactObj = new Contact(); 
  contactObj.LastName = 'CONT_NM' + String.valueOf(i); 
  // Create the parent reference.
  //check the Account Name in the map and retrieve the External Id from map
  if(accountContactMap.containsKey('ACCT_NM' + String.valueOf(i)))
  {
   Account conReference = new Account(
   External_Id__c =accountContactMap.get('ACCT_NM' + String.valueOf(i))); 
   contactObj.Account = conReference; 
  }  
  lstContacts.add(contactObj); 
} 
//add account and contact list
sobjList.addAll(lstAccount);
sobjList.addAll(lstContacts);

// Create the Account and the Contact in single DML.
Database.SaveResult[] results = Database.insert(sobjList);
// Check results.
for (Integer i = 0; i < results.size(); i++) {
  if (results[i].isSuccess()) 
  {
  System.debug('Successfully created ID: ' + results[i].getId());
  } 
  else 
  {
   System.debug('Error: could not create sobject ' + 'for array element ' + i + '.');
   strInsertError = 'The error reported was: ' + results[i].getErrors()[0].getMessage() + '\n';
   System.debug(strInsertError);
  }
} 
System.debug('strInsertError=' + strInsertError);

Results

Querying the newly created account and contact will display the results like this:
Under each account record, there will be a contact record.
4_MultipleRecordsInsertExternal Id_PIC1

2 comments:

  1. Cloud-based CRM helps in different roles of storing and retrieving data. In Cloud Computing, Salesforce is one of the major things to be noted provides applications includes ERP, CRM etc., Thanks for your clear explanation of the relationship between Cloud and Salesforce.
    Regards:
    Salesforce Training in Chennai
    Salesforce Training

    ReplyDelete
  2. Child records are not inserting...

    ReplyDelete