如何为外键字段提供嵌套的SObject

时间:2018-10-16 07:57:53

标签: php rest salesforce

我正在尝试通过php rest api在Salesforce的USSD App中创造新的机会。我正在解析记录类型ID,而不是记录类型名称。以下是用于创建新机会的代码段。

public function log_case_opp( $sobject = 'Opportunity', $recordType = '012D00000003H8DIAU', $name='XXXXXX', $account, $stage, $currency='KES - Kenyan Shilling', $close_date ) {

  $url = "$this->instance_url/services/data/v24.0/sobjects/{$sobject}/";

  $content = json_encode(array("Name" => "{$name}",
                               "RecordType" => "{$recordType}",
                               "Account" => "{$account}",
                               "StageName" => "{$stage}",
                               "CurrencyIsoCode" => "{$currency}",
                               "CloseDate" => "{$close_date}"
                              ));

  $curl = curl_init($url);
  curl_setopt($curl, CURLOPT_HEADER, false);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER,
          array("Authorization: OAuth $this->access_token",
              "Content-type: application/json"));
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

  $json_response = curl_exec($curl);

  $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

  if ($status == 400 && $json_response['error_description'] == 'expired authorization code') {
      //access code has been expired
      die('ERROR : new code required');

  }elseif ( $status != 201 ) {

      die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
  }

  curl_close($curl);

  $response = json_decode($json_response, true);

  return $response["id"];

}

尝试创造机会时,出现以下错误

[{"message":"The value provided for foreign key reference RecordType is not a nested SObject","errorCode":"INVALID_FIELD"}]

请帮助您做错了什么或没有做

1 个答案:

答案 0 :(得分:0)

我已经能够通过将ID添加到参考字段中来解决此错误,即将$ content代码块更改为:

$content = json_encode(array("Name" => "{$name}",
                               "RecordTypeId" => "{$recordType}",
                               "AccountId" => "{$account}",
                               "StageName" => "{$stage}",
                               "CurrencyIsoCode" => "{$currency}",
                               "CloseDate" => "{$close_date}"
                              ));