Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Imagine a Custom SObject X with a lookup field for Custom SObject Y. Y has an external ID field EID. As described in this answer, I can create a record X like this:

Y y = new Y(EID = 1);
X x = new X(Y__r = y);
insert x;

However, this fails if no Y record with EID = 1 exists.

To prevent this failure we could create a set of all external ids:

Set<Decimal> externalIds = new Set<Decimal>();
List<Y> ys = [SELECT EID FROM Y];
for (Y y : ys) {
X x = new X();
if (externalIds.contains(1)) {
    Y y = new Y(EID = 1);
    x.Y__r = y;
insert x;

However, this won't scale. If you have e.g. 500,000 Y records, you could never create this set.

Also, the solution has to scale for thousands of records of X, so directly querying the given EID won't work either.

Is there a way to directly bind X to Y if and only if a record of Y exists that has the given EID?

share|improve this question
up vote 3 down vote accepted

What about using saveResults to see whats happened? All depends on whether you want to check before insert that the data is valid; in your 'trivial' case looking at EID and whether it exists or not. Or whether you want to try it and see what the results is; this can be useful when validation rules, triggers, etc... may come into play.

An example maybe:-

Y y = new Y(EID = 1);
X x = new X(Y__r = y);

Database.SaveResult[] srList = Database.insert(x,false);
for (Database.SaveResult sr : srList) {
    if (sr.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted x');
    else {
       System.debug('Failed to insert x');
share|improve this answer
Interesting! Can we verify that the unknown external id is the cause for the failure? (In my real scenario I have about 5 different lookup fields, so I'd like to be able to clear only those that have unknown values) – Jolanda Verhoef Mar 25 '14 at 12:46
Have a look here… and see if the Database.Error objects have enough data for you. There is a getFields() method which may be useful in determining which field caused the error. – Richard Durrant Mar 25 '14 at 12:50

How about SOQL?

You can always ask if there is Object Y with such Id

//eidSet_FromX  - Set Of EId values from X object

 set<String> eidSet_FromY = new Set<String>();

for(Y someY : [SELECT EID FROM Y WHERE EID IN : eidSet_FromX]){
share|improve this answer
This seems like the only workable solution. It's a pity that we have to loop through the X records twice (1st time to retrieve the eids, 2nd time to set the lookup field). – Jolanda Verhoef Mar 25 '14 at 12:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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