0

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?

2
3

To specify the parent by External ID, you have to create an sObject:

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 = new Revenue_Schedule__c(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());
    }            
}

Adjust the object/field names as appropriate.

2
  • the new SObject is the parent object? – Dan Wooding Nov 27 '17 at 16:27
  • @DanWooding the bit on line 10 is the change I proposed. I didn't know what the parent object is, so of course it should be adjusted appropriately, but this is how you do it in Apex Code. LookupRelationship = new ParentType(externalIdField=externalidValue) – sfdcfox Nov 27 '17 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.