5

As the title suggests, using the Composite SOjbect Tree request, can you upsert records using External IDs?

  • This is an X-Y Problem. The specific question is answered by "no", but there are ways to get where you want to go. I suggest revising your question to ask about the "X" (the problem) instead of the "Y" (a specific, non-viable solution to X). – sfdcfox Aug 23 at 11:58
3

POST to /services/data/v46.0/composite/ where single requests are PATCHes by external id should work like a charm.

I had to anonymise this request so there might be typos but it simulates an e-commerce sync to salesforce. Upserts "dummy" account, contact (no person accounts in this org and I don't know if the guy exists already so upsert to be safe), product and asset. Set to be one all-or-nothing transaction so on error full rollback. (in this scenario SF doesn't even hold the e-commerce product catalog untill it's really needed to record the sale).

Just use your external id fields.

{
    "allOrNone" : true,
    "compositeRequest" : [{
        "method" : "PATCH",
        "referenceId" : "account1",
        "url" : "/services/data/v46.0/sobjects/Account/ExternalId__c/ext_123",
        "body" : {
            "name": "John Doe"
        }
    },
    {
        "method" : "PATCH",
        "referenceId" : "contact1",
        "url" : "/services/data/v46.0/sobjects/Contact/ExternalId__c/ext_123-1",
        "body" : {
            "firstname": "John",
            "lastname" : "Doe",
            "Account" : {
                "ExternalId__c" : "ext_123"
            }
        }
    },
    {
        "method" : "PATCH",
        "referenceId" : "product1",
        "url" : "/services/data/v46.0/sobjects/Product2/ExternalId__c/ext_12",
        "body" : {
            "name": "GenWatt generator",
            "description" : "Lorem ipsum dolor sit amet blah blah blah..."
        }
    },
    {
        "method" : "PATCH",
        "referenceId" : "purchase1",
        "url" : "/services/data/v46.0/sobjects/Asset/ExternalId__c/O-00001",
        "body" : {
            "name": "generator 20% off",
            "Contact" : {
                "ExternalId__c" : "ext_123-1"
            },
            "Product2" : {
                "ExternalId__c" : "ext_12"
            }
        }
    }]
}
  • Just to confirm though this is the standard composite API right? Not the sObject Tree? – Deployment Failure Aug 27 at 16:07
  • tree needs POST but to use upsert and external IDs you'd need PATCH. Try it, it's been a while since I was experimenting with it but it didn't work for me. The net result should be very similar, if SF sees 10 single PATCHes against same sObject type it'll flatten them into one DML. Look for "chunks" in developer.salesforce.com/docs/atlas.en-us.api_rest.meta/…. Should work close enough to what you need? – eyescream Aug 27 at 18:21

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.