2

I'm trying to insert two master-detail lists at the same time. I'm looking through Bob Buzzard's blog post: http://bobbuzzard.blogspot.com/2012/03/create-parent-and-child-records-in-one.html Where he uses this snipit to create an Account and Contact.

Account acc=new Account(Name='Blog Acc 8', Master_Id__c='Blog Acc 8');
Contact cont=new Contact(FirstName='Bob', LastName='Buzzard', Account=new Account(Master_Id__c='Blog Acc 8'));

I think I'm doing the same thing but I get this error when I try to instantiate parent:

Illegal assignment from Revenue_Pipeline__c to Id

I have one method where I'm creating the parent record:

public static void actions(List<Project_Submission__c> projSubList)
{
    List<Revenue_Pipeline__c> revPipeToUpsert = new List<Revenue_Pipeline__c>();
    List<Revenue_Pipeline_Schedule__c> revPipeSchedule = new List<Revenue_Pipeline_Schedule__c>();

    for(Project_Submission__c ps : projSubList)
    {
        Date launch = ps.Target_Launch_Date__c.toStartOfMonth();
        String fy = String.valueOf(launch.year());
        Date endDate = getfiscalEndDate(fy);
        Date startDate = getfiscalStartDate(fy);

        if(ps.Year_1_Commercial_Budget__c != null)
        {
            String ext = ps.Id + '-' + 'CommercialBudget';

            Revenue_Pipeline__c revPipe = new Revenue_Pipeline__c();
            revPipe.External_Id__c = ext;
            revPipe.NPD_Forecast_Category__c = 'Commercial Budget';

            revPipeSchedule.addAll(createschedule(ps.Id, launch, endDate, ps.Year_1_Commercial_Budget__c, ps.CurrencyIsoCode, ext));
        }

Then another method where I am creating the children

public static List<Revenue_Pipeline_Schedule__c> createSchedule(Id projSubId, Date startDate, Date endDate, Decimal price, String cur, String ext)
{
    List<Revenue_Pipeline_Schedule__c> revenueScheduleList = new List<Revenue_Pipeline_Schedule__c>();

        Integer numOfMonths = startDate.monthsBetween(endDate) + 1;
        Decimal amount = price/numOfMonths;

        for(Integer i=0; i < numOfMonths; i++) 
        {
            Revenue_Pipeline_Schedule__c revSchedule = new Revenue_Pipeline_Schedule__c(
                Amount__c = amount,
                Date__c = startDate.addMonths(i),
                //Name = projSubId + ' - ' + Date__c.year() + '-' + Date__c.month(),
                CurrencyIsoCode = cur,
                Revenue_Pipeline__c = new Revenue_Pipeline__c(External_Id__c = ext)); <----- Error Here
            revenueScheduleList.add(revSchedule);           
        }
    return revenueScheduleList;
}

What am I doing wrong?

5

You should be using the relationship name instead of the object name.

Replace below code which is giving the error

Revenue_Pipeline__c = new Revenue_Pipeline__c(External_Id__c = ext));

With

Revenue_Pipeline__r = new Revenue_Pipeline__c(External_Id__c = ext));

Note the __r in the above statement.

  • +1. Didn't realize at the first place that the relationship name was incorrect. – Jayant Das Apr 18 at 21:12
1

The approach you are using: Relating Records by Using an External ID has a pre-requisite that the parent record should exist.

In your current code, it seems you haven't yet inserted the parent Revenue_Pipeline__c record and are trying to reference the External Id field while creating the record for the child Revenue_Pipeline__c, and the primary issue for the error is that you have the field name incorrect. It should be the relationship name of the field in this case.

In order for the approach below to work, you will need to ensure that the Revenue_Pipeline__c record has been inserted with the External Id value and that you will need to specify the relationship name for parent.

Your modified code should look as:

Revenue_Pipeline_Schedule__c revSchedule = 
    new Revenue_Pipeline_Schedule__c(
         ...
        // make sure a record for Revenue_Pipeline__c exists with External_Id__c = ext
         // and use the relationship name here __r
         Revenue_Pipeline__r = new Revenue_Pipeline__c(External_Id__c = ext)); 

As for how you can insert parent child records in the same flow, refer this from the linked documentation:

If the parent record doesn’t exist, you can create it with a separate DML statement or by using the same DML statement as shown in Creating Parent and Child Records in a Single Statement Using Foreign Keys.

  • the compiler doesn't know that the exists or not, shouldn't it still compile with the logic? – Dan Wooding Apr 18 at 19:06
  • As long as the fields are correct and the relationship defined, there should not be a compile time error for sure. – Jayant Das Apr 18 at 19:14
  • @DanWooding I just realized that you didn't have the relationship name while assigning it, and that was the reason of the issue. I have updated the answer to reflect that. – Jayant Das Apr 18 at 19:18

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.