1

How can we upsert (insert if not found, update if found) a record in the object, querying by a particular field (in our case: UserID__c)? We tried to PATCH the following request URL:

$userId = $_POST['User_ID__c'];
$url = "{$instance_url}/services/data/v55.0/sobjects/Foo_Member__c/User_ID__c/{$userId}";
$request = $client->request('PATCH', $url, [
     'headers' => [
           'Authorization' => "OAuth {$access_token}"
     ],
     'form_params' => $_POST
]);

The error we are getting back is:

Client error: PATCH https://[...].salesforce.com/services/data/v55.0/sobjects/Foo_Member__c/User_ID__c/42 resulted in a 404 Not Found response: [{"errorCode":"NOT_FOUND","message":"Provided external ID field does not exist or is not accessible: User_ID__c"}]

How can we upsert the record by User_ID__c and update it with the data?

5
  • Is User_Id__c marked as an external Id field? Did you create this field in the org that you're trying to do this in, or was it deployed to the org? If it was deployed, did you remember to set the FLS (otherwise, nobody, not even sysadmins, can use the field)?
    – Derek F
    Sep 1, 2022 at 12:47
  • We created the field inside the org in the custom object's field and relationship area. Is that the correct way to do it?
    – Platoscave
    Sep 1, 2022 at 12:50
  • That doesn't exactly answer my question, and that was not the only question. Did you deploy this field to the org you're trying to use the REST API in, yes/no? Is it an "external id" field, yes/no? If it was deployed to the org, did you set FLS for it in the new org, yes/no?
    – Derek F
    Sep 1, 2022 at 12:53
  • Sorry, I"m not very familiar with Salesforce, could you please explain what you mean by 'deploy this field' in the first question. No, the external id checkbox is not ticked in the User_Id__c field settings. And sorry, but I don't know about setting Field-level Security(?) Does that need to be set to something specific?
    – Platoscave
    Sep 1, 2022 at 13:16
  • @DerekF We only created it as a field of the object. Tomorrow we will check out how to make it external id. Sep 1, 2022 at 13:51

1 Answer 1

1

Primary issue

Based on our back-and-forth in the comments, I think that the primary issue here is that you haven't marked your User_Id__c field as an "External Id" (by checking the "External Id" checkbox when creating or editing the field definition).

You can't use any arbitrary field to get the "insert if doesn't exist, otherwise update" behavior of an Upsert. It either needs to be the standard Id field on the object, or a field marked as an "External Id". Your external Id also needs to be unique across all of the records on its SObject for an upsert to work (otherwise Salesforce has no idea which record you actually want to update). Insert or Update (Upsert) a Record Using an External Id goes into some more detail.

Potential secondary issue

Field Level Security (FLS for short) is a thing. It's stored on user's profiles (i.e. the actual profile records like "Sales User" or "System Administrator"), but it's accessible from the field definition on the SObject as well (the "Set Field-Level Security" button).

When you create a new field, you're presented the opportunity to set the FLS.

While you can create new SObjects, new Fields, new Validation rules, etc... (pretty much everything except for code) directly in your production org, that's not good practice from a change-management perspective. Instead, you should generally be making (and testing) your customizations in a sandbox "org" and then "deploy" it to your production org.

Deployments are how metadata/customizations are moved from one org to another. Deployments can be done through one of several mechanisms:

  • A "Change Set", created through Salesforce's Web UI
  • Using an xml file and the Metadata API (usually with a tool like Apache ANT, or using the Salesforce CLI via sfdx force:metadata:deploy)
  • Using the Salesforce CLI and specifying an entire project, or a subset of the components via sfdx force:source:deploy -p ./my-project/ or sfdx force:source:deploy -m CustomField:MyObject__c.MyField__c

If you do deploy a field to another org, the FLS does not get deployed along with it. The field itself will be deployed, but nobody (not even System Administrators) will be able to use the field in the org that it was deployed to. They'll be able to see that the field exists in the Web UI, but basically nothing else until the FLS is set for the field in the new org.

If you can see a field in the Web UI, but get an error like

field does not exist. check spelling

then that's generally an issue with not having the FLS set (or you spelled it wrong, or didn't include __c at the end for a custom field).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .