-2

After updated the code, now I can see the error string(148) "[{"errorCode":"APEX_ERROR","message":"System.TypeException: Invalid conversion from runtime type List to Map\n\n(BMCServiceDesk)"}]"

Just to explain: I am creating an integration to open a Service Request in my salesforce instance (remedyforce application).

I created the file below, I've had several errors and corrected it until I didn't give any more errors, but now I can see only invalid conversion. Maybe because my json.

<?php 

$url = 'https://URL.my.salesforce.com/services/apexrest/BMCServiceDesk/1.0/ServiceRequest/';
$ch = curl_init($url);

$sf_auth = 'Bearer XXXXXX';

$params = array("[
  'Fields' => [
    0 => [
      'Name' => 'requestDefinitionId',
      'Value' => 'a3Hf0000000lTNaEAM',
    ],
    1 => [
      'Name' => 'client',
      'Value' => '0053j00000A7rWLAAZ',
    ],
  ],
  'Answers' => [
    0 => [
      'QuestionId' => 'a3Df0000000qI63EAE',
      'Values' => [
        0 => 'Reclamação',
      ],
    ],
    1 => [
      'QuestionId' => 'a3Df0000000qHvsEAE',
      'Values' => [
        0 => 'Solicitação de Serviço aberta por qualquer integração web',
      ],
    ],
    2 => [
      'QuestionId' => 'a3Df0000000qHwREAU',
      'Values' => [
        0 => 'Web',
      ],
    ],
  ],
]");
           
    $data_string = json_encode($params);
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'Content-type: application/json',
        'Authorization:' . $sf_auth,
    ));

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    //execute post
    $result = curl_exec($ch);
    
    //close connection
    curl_close($ch);
    
    $response = curl_exec($ch);
    
    echo curl_errno($ch) . '<br/>';
    echo curl_error($ch) . '<br/>';

    var_dump($result);
    
?>
Barcat
  • 163
  • 16
  • 1
    Well your `$params` object must match the below format, which it currently does not even come close to. And it must actually be encoded as JSON, which is _not_ what `http_build_query` does. – CBroe Apr 19 '21 at 11:51
  • Tks, CBroe, I edited my post, with the last code. I changed e removed http_build_query and use json encode on my json data. But zero effect! – Barcat Apr 19 '21 at 12:34
  • 1
    Of course _just_ JSON-encoding what is still a massively different data structure to begin with, does not fix the problem. You need to fix the data structure you have in $params, to match the structure of the JSON you want. – CBroe Apr 19 '21 at 12:37

1 Answers1

0

After made the changes, it is ok now.

So I'll leave this code if anyone that uses Remedyforce need a base to start this kind of integration.

<?php 
    // Inicia
$url = 'https://MYURL--trnmnto.my.salesforce.com/services/apexrest/BMCServiceDesk/1.0/ServiceRequest/';
$ch = curl_init($url);

$sf_auth = 'Bearer XXXXXXXXX';

$params = ("{\"Fields\":[{\"Name\":\"requestDefinitionId\",\"Value\":\"a3Hf0000000lTNaEAM\"},{\"Name\":\"client\",\"Value\":\"0053j00000A7rWLAAZ\"}],\"Answers\":[{\"QuestionId\":\"a3Df0000000qI63EAE\",\"Values\":[\"Reclama\u00e7\u00e3o\"]},{\"QuestionId\":\"a3Df0000000qHvsEAE\",\"Values\":[\"Solicita\u00e7\u00e3o de Servi\u00e7o aberta por qualquer integra\u00e7\u00e3o web\"]},{\"QuestionId\":\"a3Df0000000qHwREAU\",\"Values\":[\"Web\"]}]}");
           
    $data_string = json_encode($params);
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/json',
        'Content-type: application/json',
        'Authorization:' . $sf_auth,
    ));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    //execute post
    $result = curl_exec($ch);
    //close connection
    curl_close($ch);
    
    $response = json_decode(curl_exec($ch), true);

    echo $response;
    
    echo curl_errno($ch) . '<br/>';
    echo curl_error($ch) . '<br/>';

    var_dump($result);
    
?>
Barcat
  • 163
  • 16