function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sf_davesf_dave 

Changing an External ID Value

All,

The existing web portal I'm integrating with Salesforce has primary keys in a table that I use as the External ID in Salesforce.  Our web portal, however, CHANGES some of these primary keys.  Note these keys do remain unique, but I was wondering if there is a way in salesforce to change these External IDs through upsert.

I assume through update I could just
update Contact set CompanyId__c=100 where CompanyId__c=75;

but I was hoping I could modify this through an upsert some how.

1. Is that possible?
2. If I can't do this through upsert, how would one do it?

Thanks
-Dave
RickyGRickyG
Dave -

I don't have a definitive answer, but my guess is that the update would not work.  When you specify an external key, the Force.com platform uses the value in that key in the parent record to establish the parentage of the new child.  However, the relationship field points to the internal Salesforce ID of the parent. 

But the upsert could not determine that the newly changed record already existed in the Force.com object, so it would just do an insert.

I don't think there is a way around this, but I am checking.  My guess is that you would have to find the record in the Force.com object, delete it, and then do an upsert with the new version.  If you have a unique value on the child, this would not be that hard to code.  If you don't, you might want to think about getting one :smileywink:

Hope this helps.  Your use case is at least a good illustration of what can happen when some of the foundations of your data design are allowed to shift.

sf_davesf_dave
Thanks for the reply!

What about this scenario...What if we have 2 External ID's on a Contact:
CompanyId__c
and
Email__c

and having a Contact:
contact->CompanyId__c = 100;
contact->Email__c = "example@example.com";

Then allowing a contact to be upserted by its company id:
->upsert('CompanyId__c', contacts[])

setting the CompanyId__c to 100 and the email to example@example.com only allowing a later upsert to try and change one of the values
contact->CompanyId__c = 75;
contact->Email__c = "example@example.com";

->upsert('Email__c', contacts[])

this way the external id of Email__c allows upserting to occur, but we try to change a previously set External ID?

or is an External ID NEVER CHANGABLE after it is set....ever.

Thanks
-Dave


RickyGRickyG
Dave -

It's not a matter of whether you can change an external ID - it's how the external ID is used.

The external ID is used to identify the parent for the relationship.  After the parent is identified, the relationship field in the child is set to the ID of the parent.  At this point, the external ID has fulfilled its purpose, at least as far as the relationship.

You could drop the external ID at this point, and it would not affect the relationship at all.

When you go to do an upsert, the Force.com platform has to identify whether the target record exists or not.  The external key is not used here.

If you want to change the relationship, you can use the external key to once again identify the proper parent, but you cannot use upsert to identify if the child record exists (causing an update) or not (causing an insert).  So you have to drop the child and re-insert it.

Hope this helps.
sf_davesf_dave
Thanks Ricky,

I'm sorry but I still don't quite follow 100%

When you upsert something by specifying its External ID I assume it goes something like this:
Receive Upsert request for a Type of Contact with an External ID Column of CompanyId__c
The value for CompanyId__c for this contact is 100
does 100 exist?
Yes -> Update the record
No -> Insert the record

This doesn't have to do with any Parent / Child relationships of what this contact belongs to.

If I go into the actual Contact and click Edit and change the value for the External ID, I can subsequently Upsert on that record based on the NEW External ID.

So This isn't about associating records to other records, this is about best practices to change the Value of a "Custom Field External ID Type" from the API.

Am I missing something?

Thanks
-Dave


SuperfellSuperfell
You can change an externalId value, just not through an upsert call (as there aren't sepearate fields for the value to find, and the value to update). You'll have to do an update call using the SFDC Id as the pk to update the externalId field.
sf_davesf_dave
Thank you Simon,

I figured as much...I just wanted to get a confirmation before I spent hours trying to draft up test cases / API interfaces.

Thanks again
-Dave