Print this page

Setting ParentID for related records when using Test.loadData()

Knowledge Article Number 000181909
Description
Using the Test.loadData approach, how would one go about inserting records that are related to each other?
Resolution
Typically using Data Loader, one would insert a group of records and then turn right around and extract the ids of the records just inserted. Those ids would then be used in other related record inserts.

The options with setting ParentID using Test.loadData() technique are as follows:
 
1) Setting ParentID for related record (If system protected fields need to be manipulated)
 
Using this Test.loadData() technique the only way to do this is by manually entering ID for case record and then entering the same ID as ParentID for CaseComment record. Check the csv files that is attached with the KB.
 
This is manual, but since this is the only way to test system protected field, currently there is no direct way of relating the 2 records. 

Sample code: 
 
@isTest 
private class caseUtil{ 
static testmethod void testLoadData(){ 
List<sObject> ls = Test.loadData(Case.sObjectType,'testCases'); 
List<Case> cList = new List<Case>(); 
 
for(Integer i=0;i<ls.size();i++){ 
cList.add((Case)ls[i]); 
 
System.debug('Case List:'+cList); 
 
for(Case c : cList){ 
String cStatus = c.Status; 
DateTime cDate = c.CreatedDate; 
 
System.debug('Case Id: ' + c.Id); 
System.debug('Case Status: ' + cStatus); 
System.debug('Case Date: ' + cDate); 
 
 
List<sObject> cs = Test.loadData(CaseComment.sObjectType,'testComments'); 
List<CaseComment> ccList = new List<CaseComment>(); 
 
for(Integer i=0;i<cs.size();i++){ 
ccList.add((CaseComment)cs[i]); 
 
System.debug('Case comment List:' + ccList); 
 
for(CaseComment cc: ccList){ 
System.debug('Comment ID: '+cc.Id); 
System.debug('Comment Date: '+cc.CreatedDate); 
 
System.debug('Parent ID:' + cc.parentId); 

 
Note: Refer to KB article on "Setting system protected fields for test code coverage" to understand how system protected fields are manipulated to achieve test coverage.

2) Setting ParentID for related record (If not manipulating system protected fields)
 
Previous approach is if we want to modify system protected fields on case comment object. If you don't want to manipulate the system protected fields on CaseComment object then you can relate the records directly. 
 
Follow the code below: 
 
@isTest 
private class caseUtil{ 
static testmethod void testLoadData(){ 
List<sObject> ls = Test.loadData(Case.sObjectType,'testCases'); 
List<Case> cList = new List<Case>(); 
 
for(Integer i=0;i<ls.size();i++){ 
cList.add((Case)ls[i]); 
 
System.debug('Case List:'+cList); 
 
String ID; 
 
for(Case c : cList){ 
String cStatus = c.Status; 
DateTime cDate = c.CreatedDate; 
ID = c.Id; 
 
System.debug('Case Id: ' + c.Id); 
System.debug('Case Status: ' + cStatus); 
System.debug('Case Date: ' + cDate); 
 
CaseComment cc = new CaseComment(); 
cc.ParentId = ID; 
insert cc; 
}
}




Attachments
Name Type Size
testCases.csv
0KB
testComments.csv
0KB

promote demote