question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection.upsert for Inserting New Records Using Id as the External ID

See original GitHub issue

I encountered an error METHOD_NOT_ALLOWED: HTTP Method 'PATCH' not allowed. Allowed are GET,HEAD,POST when connection.sobject('Account').upsert({Name: 'foo'}, 'Id').

I found description below in developer guide. https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_upsert.htm

Inserting New Records Using Id as the External ID This example uses the POST method as a special case to insert a record where the Id field is treated as the external ID. Because the value of Id is null, it’s omitted from the request. This pattern is useful when you’re writing code to upsert multiple records by different external IDs and you don’t want to request a separate resource. POST using Id is available in API version 37.0 and later.

So if external ID field is Id and Id is not present, HTTP method for upsert should be POST.

These lines: https://github.com/jsforce/jsforce/blob/a99a38b55346388f53a47d8b2e5afcace43c232b/lib/connection.js#L754

      var url = [ self._baseUrl(), "sobjects", sobjectType, extIdField, extId ].join('/');
      return self.request({
        method : 'PATCH',
        url : url,

should be like:

      var url = _.compact([ self._baseUrl(), "sobjects", sobjectType, extIdField, extId ]).join('/');
      return self.request({
        method : (extIdField.toUpperCase() === 'ID' && extId == null) ? 'POST' : 'PATCH',
        url : url,

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:6
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bsansonecommented, Sep 12, 2018

@stomita Hi, this is the current use-case I am looking to use upsert for:

In my api, I am making a request to update a master object in SalesForce, when this request is made, there may or may not exist a related detail object to this master object. I determine this by making a query to check for that detail object where the Master-Detail field is equal to the master object Id, e.g. SELECT Id FROM DetailObject__c WHERE MasterDetailField__c = 'MasterObjectId'; If no records are returned I am creating that detail object, if records are returned than I know I can update the detail object.

So instead of conditionally checking if there are records returned myself and either making a create or update call, I wanted to use upsert to do it for me, using @tarot 's suggested change, I was able to get this to work:

const record = {
    Id: detailObjectRecordExists ? detailObjectId : null,
    Field1__c: "foo",
    Field2__c: "bar"
};

if (!detailObjectRecordExists) {
    record.MasterDetailField__c = MasterObjectId;
}

conn.sobject("DetailObject__c").upsert(record, "Id", function(err, ret) {
    // ...
});
0reactions
edelaunacommented, Oct 11, 2019

Your payload is missing the external field as such the request URL is breaking - I submitted PR#931

In the mean time try adding a slash to the extIdField field so that it reads:

connection.sobject('Account').upsert({Name: 'foo'}, 'Id/')

or adding Id to your record (doesn’t work if Id is null or " " unfortunately).

connection.sobject('Account').upsert({Name: 'foo', Id:'123'}, 'Id/')

Read more comments on GitHub >

github_iconTop Results From Across the Web

Insert or Update (Upsert) a Record Using an External ID
You can use the sObject Rows by External ID resource to create records or update existing records (upsert) based on the value of...
Read more >
updating a records using external Id in Apex
You have to specify the field which you want to use an external id. So your syntax will be upsert flist Externald__c;.
Read more >
Power of Upsert and External IDs (Part 1) - Douglas C. Ayers
The upsert operation intelligently performs an insert or update of a record by matching against a unique identifier (such as the ID field...
Read more >
All about Upsert and External ID in Dataloader and Apex
It can be used to identify if record exists or not and record automatically inserted or updated using upsert operation · Launch Dataloader ......
Read more >
How to use external Id to Upsert data in salesforce using Talend
We can directly upsert data using external field. (Upsert is combination of insert and update, if the record already exists in the system...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found