0

I have created a custom object which is connected to the Account Object via Master-Detail. The custom object stores reviews for the Accounts.

The reviews are given on a website and saved via REST API.

While trying to save the review I get the following error:

"required fields missing: [Account_Connection__c]"

Account_Connection__c is my Master Detail Field. How can I refer to an account that has e.g a field CustomerID with value 1234? I am stuck with this for days...

enter image description here

1
  • 1
    Look at composite resources
    – Elijah
    Commented Aug 23, 2022 at 10:15

2 Answers 2

0

You can use composite api here.

In the below example, We have Account and Order__c and we create Junction object as Student_Orders__c in the same request.

Use of POST or PATCH depends on you, but we used patch so that any other requests should always be updated using externalId passed.

{
  "allOrNone": true,
  "compositeRequest": [
    {
      "method": "PATCH",
      "url": "/services/data/v48.0/sobjects/Account/Premium_ID__c/cghjmhuiokuhi456Auto",
      "referenceId": "RefStudentcghjmhuiokuhi456Auto",
      "body": {
        "FirstName": "Automation cghjmhuiokuhi456Auto",
        "LastName": "User cghjmhuiokuhi456Auto"
      }
    },
    {
      "method": "PATCH",
      "url": "/services/data/v48.0/composite/sobjects/Order__c/Order_No__c",
      "referenceId": "orderList",
      "body": {
        "allOrNone": true,
        "records": [
          {
            "attributes": {
              "type": "Order__c",
              "referenceId": "order1"
            },
            "Order_No__c": "TestOrder",
            "Name": "Order : K12 course-1st Order"
          }
        ]
      }
    },
    {
      "method": "PATCH",
      "url": "/services/data/v52.0/composite/sobjects/Student_Orders__c/Reference_Id__c",
      "referenceId": "studentOrderList",
      "body": {
        "allOrNone": true,
        "records": [
          {
            "attributes": {
              "type": "Student_Orders__c",
              "referenceId": "studentOrder1"
            },
            "Name": "Student Order : cghjmhuiokuhi456Auto 1",
            "Order__c": "@{orderList[0].id}",
            "Student__c": "@{RefStudentcghjmhuiokuhi456Auto.id}",
            "Reference_Id__c": "@{orderList[0].id}-@{RefStudentcghjmhuiokuhi456Auto.id}"
          }
        ]
      }
    }
  ]
}
0

I finally managed to achieve what I wanted.

In my POST Request I referenced the customerID field as following (note that I am using Guzzle with PHP):

$postArray = array(
    'AccountConnection__r' => json_decode(' 
        {
            "customerID__c" : "' . $reviewData['customerID'] . '"
        }
    '),
    //All other fields
);

AccountConnection__c is the name of my Master-Detail field set on reviews.

We need to enter the related object so we use AccountConnection__r and tell Salesforce to relate our child to an account with given ID.

I stored many values in an array, otherwise it might look like this:

$postArray = array(
    'AccountConnection__r' => json_decode(' 
        {
            "customerID__c" : "12345"
        }
    '),
    //All other fields
);

The request then gets sent like this:

$request = $client->post( 'services/data/v55.0/sobjects/Reviews__c/', [
    'json' => $postArray,
    RequestOptions::HEADERS => [
        'Authorization' => 'Bearer ' . $accessToken,
    ]
]);

You must log in to answer this question.

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