1

When creating records and relating them using foreign key is there a way to make this non critical, Take an example from http://bobbuzzard.blogspot.co.uk/2012/03/create-parent-and-child-records-in-one.html

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'));
insert new List<Sobject>{acc, cont};

This would create both the Account and Contact and relate them together.

...suppose the Account already existed so we were just creating a Contact...

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

...now lets assume we did this but the Account didn't exists, as standard if the FK relationship does not exist an "INVALID_FIELD" exception is thrown:

INVALID_FIELD, Foreign key external ID: Blog Acc 8 not found

Can this constraint be disabled so the Contact record would be created without the relationship?

0

So you are asking if it is possible to add the foreign key on insert, but not yet have loaded the record it points to? I would say no, because Salesforce doesn't actually store the external foreign key. It looks up the external ID, finds the Salesforce ID of the record, and stores that in the foreign key field.

If you must do something like this regularly, a possibility is to create a hidden text field on Contact where you store the external foreign key and import it into there. Later on, when that Account is loaded, you can use Apex to read in the foreign key and make the connection to the appropriate Account.

| improve this answer | |

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.