0

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 convertion. 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);
    
?>

When I run above code, the issue is:

string(148) "[{"errorCode":"APEX_ERROR","message":"System.TypeException: Invalid conversion from runtime type List to Map\n\n(BMCServiceDesk)"}]"

Anyone know how can I structure the json?

Below, the structure of the body that I did on postman (and works).

{
  "Fields": [
    {
      "Name": "requestDefinitionId",
      "Value": "a3Hf0000000lTNaEAM"
    },
    {
      "Name": "client",
      "Value": "0053j00000A7rWLAAZ"
    }
  ],
  "Answers": [
    {
      "QuestionId": "a3Df0000000qI63EAE",
      "Values": [ "Reclamação" ]
    },
    {
      "QuestionId": "a3Df0000000qHvsEAE",
      "Values": [ "Solicitação de Serviço aberta por qualquer integração web" ]
    },
    {
      "QuestionId": "a3Df0000000qHwREAU",
      "Values": ["Web"]
    }
      ]
}
3
  • What do your Remedyforce documents say is the expected structure of the payload? – David Reed Apr 19 at 14:22
  • Hi David. I paste the body that I used in postman, maybe it helps. – Barcat Apr 19 at 14:29
  • And the body that is in postman I followed the instructions on the docs, but there have no info about how can I convert it to pass from php. – Barcat Apr 19 at 14:30
0

So the error is that you're passing a List (of some type), but expecting a Map.

Let's look at your PHP.

$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',
      ],
    ],
  ],
]");

If you're not passing any other parameters, then I'd think you should be fine with just making $params a string (json) instead of an array, but I don't think this is an issue.

The issue is how you're enclosing your JSON.

Right now, you have $params = array("[<omitted>]");. square brackets in JSON indicate it's an array. You want to make the outermost JSON entity an object, not an array. Use curly braces ({ and }) instead.

To make your JSON generated by php match your test payload from postman, you'll also want to remove the numbered indices and turn things into objects instead of arrays.

e.g. things like this

'Fields' => [
    0 => [
      'Name' => 'requestDefinitionId',
      'Value' => 'a3Hf0000000lTNaEAM',
    ],
    1 => [
      'Name' => 'client',
      'Value' => '0053j00000A7rWLAAZ',
    ],
  ],

should become

'Fields' => [
    {
      'Name' => 'requestDefinitionId',
      'Value' => 'a3Hf0000000lTNaEAM',
    },
    {
      'Name' => 'client',
      'Value' => '0053j00000A7rWLAAZ',
    }
  ],
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);
    
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.