3

I'm trying to create an Account, Contact, Opportunity and then link the Contact to the Opportunity in a single composite/tree api call.

Is it possible to link the OpportunityContactRole to the Contact just using it's ref?

My json looks like:

{
    "records" :[{
        "attributes" : {"type" : "Account", "referenceId" : "ref1"},
        "name" : "SampleAccount1",
        "Contacts" : {
            "records" : [{
                "attributes" : {"type" : "Contact", "referenceId" : "ref2"},
                "lastname" : "Smith",
                "title" : "President",
                "email" : "[email protected]"
            },{
                "attributes" : {"type" : "Contact", "referenceId" : "ref3"},
                "lastname" : "Evans",
                "title" : "Vice President",
                "email" : "[email protected]"
            }]
        },
        "Opportunities": {
            "records" : [{
                "attributes" : {"type" : "Opportunity", "referenceId" : "ref4"},
                "Name": "x",
                "StageName": "x",
                "CloseDate": "2017-01-01",
                "OpportunityContactRoles": {
                    "records" : [{
                        "attributes" : {
                            "type" : "OpportunityContactRole", 
                            "referenceId" : "ref5"
                        },
                        "ContactId": "#ref2"
                    }]
                }
            }]
        }
    }]
}

The error response is:

{
  "hasErrors": true,
  "results": [
    {
      "referenceId": "ref5",
      "errors": [
        {
          "statusCode": "MALFORMED_ID",
          "message": "Contact ID: id value of incorrect type: #ref2",
          "fields": [
            "ContactId"
          ]
        }
      ]
    }
  ]
}

Cheers

2
  • Perhaps this post will help? I imagine the approach would be the same via the API.
    – Adrian Larson
    Commented Aug 18, 2016 at 14:45
  • 1
    I saw those but they don't help. I think this is a limitation with the composite tree endpoint. Commented Aug 18, 2016 at 14:50

1 Answer 1

1

Not with using the Tree API, you will have to use the Composite API.

Tree API doesn’t let you use reference variables of previously created records as parent ids for child records, it is all inferred by the master-detail relationship of the parent records hierarchy you configure as the tree. Technically you can create an OpportunityContactRole with Tree API provided that you know ahead of time the value of either the contact or opportunity id.

The Composite API does allow you to reference previously created records such that you can reference both a Contact ID and Opportunity ID.

I presented about this very topic at Midwest Dreamin 17.

Blog: https://douglascayers.com/2017/08/13/tour-of-salesforce-rest-apis-for-multiple-dml-operations-in-single-api-request/

Video: https://youtu.be/ILRGWlo6SeU

Code: https://github.com/DouglasCAyers/sfdc-rest-apis-for-multiple-dml/blob/master/app.js#L226

You must log in to answer this question.

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