I'm creating a process to take records from a parent object and child object records and recreate them into different parent and child objects. This is for budgeting where we take a snapshot of records from "live" Salesforce and populate them into a static object that does not get updated so the budget stays the same.
I created a batch job that takes the 10,000 parent object records in 200 record batches and successfully creates them into the new object. The problem is I cannot figure out how to create the new 90,000 child records using a External Id.
My thought process is batch through the parents, then start another batch to get the child records. My batch method looks like this:
public static void createRevenueSchedule(List<Revenue_Pipeline_Schedule__c> scope)
{
List<Revenue_Schedules__c> revScheduleToInsert = new List<Revenue_Schedules__c>();
for(Revenue_Pipeline_Schedule__c rp : scope)
{
revScheduleToInsert.add(new Revenue_Schedules__c(
Amount__c = rp.Amount__c,
Date__c = rp.Date__c,
Project_Revenue__r.External_Id__c = rp.Revenue_Pipeline__c //Id from old object is External Id for new object
));
}
try{
insert revScheduleToInsert;
} catch (DmlException ex) {
ApexPages.addMessages(ex);
System.debug('The following Exception has occured: ' + ex.getMessage() + ' at line ' + ex.getLineNumber());
}
}
The compile error I get says the field does not exist at the external Id line:
Field does not exist: Project_Revenue__r.External_Id__c on Revenue_Schedules__c
Can you reference the external Id field without querying all the parent records?