0

The requirement is to inserting parent record and and creating association with child while in Child Insert trigger.

Based on this link : https://sfcure.com/2017/11/20/inserting-updating-parent-and-child-records-in-a-single-transcation-using-external-id-field/

  1. after insert on child, insert parent record with external id
  2. update child association based on external id of parent

However receiving error "Illegal assignment from Individual_Email_Summary__c to Id" .

Below is clip of it on after insert of child record. It is written in forloop for bulkification .Below clip is from anonymous window for just one record. Individual_Email_Summary__c : Parent et4ae5__IndividualEmailResult__c : Child

List<Individual_Email_Summary__c> AggrToinsert = new List<Individual_Email_Summary__c>();
List<et4ae5__IndividualEmailResult__c> AggrToUpdate = new List<et4ae5__IndividualEmailResult__c>();
      
AggrToinsert.add(new Individual_Email_Summary__c(
    Contact__c = '0030m00000UZ6RF', 
    Updated_Month__c = system.today(),
    MyExtId__c = '0030m00000UZ6RF_Ex'
));

AggrToUpdate.add(new et4ae5__IndividualEmailResult__c(
    id = 'a370m000000QWKY',
    Individual_Email_Summary__c = new Individual_Email_Summary__c(
        MyExtId__c='0030m00000UZ6RF_Ex'
    )
));

// Currently failing here  with "Illegal assignment from Individual_Email_Summary__c to Id"          
Database.SaveResult[] srInsertList = Database.insert (AggrToinsert, false);
Database.SaveResult[] srupdateList = Database.update(AggrToUpdate);

Any thoughts on resolving this issue will be appreciated

2
  • Have you marked the external id field as unique? – Aakriti.G Jul 8 '20 at 14:47
  • Thanks Aakriti for quick response. The External id itself is unique as it mentioned in setting "Set this field as the unique record identifier from an external system". The error response is Illegal assignment from Individual_Email_Summary__c to Id" (third line from bottom) . It is same as in that link however not working for me. – athi Jul 8 '20 at 14:58
0

First things first, I don't think this project you're working on is a good fit for this pattern (relate child to parent using an external id). It would be a good fit if:

  • Your parent record(s) already exist
  • And already have an external id
  • And you can somehow know what external Id you need to use for any given child record (without querying)

Given your explanation so far, you're really not getting any of the benefit of this pattern.

If you're creating parent records in the trigger for your child object, you will have access to the Ids of the parent records, and it'd be much better to simply set the Id of the parent record like normal (but in a before insert trigger so you can avoid more DML and trigger execution).

If you're creating child records in the trigger for your parent object, you'll have access to the parent record Ids in the after insert context, and should just set the relationship Id on your child records before you try to insert them.

With that out of the way...

Fixing the error

The big thing (and there are more than one) that this particular blog post entirely glosses over is that to make this work, you need to set the parent foreign key reference on the child SObject rather than the relationship field itself. Like your error says, you're trying to assign an SObject instance to an Id field (which doesn't work).

Changing the line

Individual_Email_Summary__c = new Individual_Email_Summary__c(
    MyExtId__c='0030m00000UZ6RF_Ex'
)

to

Individual_Email_Summary__r = new Individual_Email_Summary__c(
    MyExtId__c='0030m00000UZ6RF_Ex'
)

should resolve the error.

Individual_Email_Summary__c holds the Id of the related record
Individual_Email_Summary__r holds a reference to an SObject

For standard relationships (like the one between Opportunity and Account), the AccountId field contains the Id, and we just drop "Id" (so, Account) if we need the SObject reference (either when setting the reference, as you're doing here, or when reading from the reference when working with the result from a query where you queried data on a related object).

1
  • Thank you so much Derek. Individual_Email_Summary__r did the trick. Above logic worked fine for single insert of child record. I am yet to test for bulk insert and keep posted. Appreciate your quick response – athi Jul 8 '20 at 18:50

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.