0
votes

I am trying to use the REST lightning API for salesforce. So far I can have it connect succesfully and get info on some things, however I am struggling to get it to actually create new records. Below is the code, I have excluding my connection code and getting my Bearer token, as both those work fine and don't impact the second half of creating a record.

<?php

             $url = $instance_url.'/services/data/v20.0/sobjects/Account/';
             $headers = array(

                'Content-Type: application/json'
            );

        $data = array(
            'Name' => "AccountHEX"

        );


            $ch2 = curl_init();

            curl_setopt($ch2,CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_POST, 1);

            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

            curl_setopt($ch2,CURLOPT_SSL_VERIFYPEER, false);

            curl_setopt($ch2,CURLOPT_RETURNTRANSFER, true);

            $head = 'Authorization: Bearer '.$access_token;

            curl_setopt($ch2, CURLOPT_HTTPHEADER, array($head));



            //execute post

            $result = null;

            $result = curl_exec($ch2);

             echo $result;

?>

The result I get seems to be just the info on the account object:

{
   "objectDescribe":{
      "activateable":false,
      "createable":true,
      "custom":false,
      "customSetting":false,
      "deletable":true,
      "deprecatedAndHidden":false,
      "feedEnabled":true,
      "keyPrefix":"001",
      "label":"Account",
      "labelPlural":"Accounts",
      "layoutable":true,
      "mergeable":true,
      "name":"Account",
      "queryable":true,
      "replicateable":true,
      "retrieveable":true,
      "searchable":true,
      "triggerable":true,
      "undeletable":true,
      "updateable":true,
      "urls":{
         "rowTemplate":"/services/data/v20.0/sobjects/Account/{ID}",
         "describe":"/services/data/v20.0/sobjects/Account/describe",
         "sobject":"/services/data/v20.0/sobjects/Account"
      }
   },
   "recentItems":[

   ]
}

So it is treating it more as a query rather than a creation. I have tried a couple different $data arrangments. Incluidng just doing name right away, and putting it inside the fields array.

Trying to do this bassed on this:

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_create.htm

Any ideas how to get it to create the record?

1

1 Answers

0
votes

What you're receiving is the Account sObject describe, which is the return value for a GET request. You need to make a POST request to create the sObject.

Your body data does not need to be nested in a fields key. Your JSON should look like the example from the documentation,

{
  "Name" : "Express Logistics and Transport"
}

with all fields at the top level.

Lastly, API v20.0 is extremely old. I would recommend declaring the latest API version in your endpoint URL, v46.0. Using old API versions can result in unexpected behavior and in certain fields being unavailable to you.