Search code examples
phpapidynamics-crm-2011microsoft-dynamicsnavigation-properties

Create incident in dynamics crm using api in php


Im trying to create case in dynamics CRM using php.For that I can see that title,description and customer is required.So that I tried below code:

  $authHeader = 'Authorization:' . $type.' '.$access_token;
    //Request for incidents
  $data = array("title"=>"api_incident_title",
            "description" =>"api_incident_description",
    "primaryContactid" =>"https://vonageholdings.crm.dynamics.com/api/data/v8.0/accounts(ebaf25a6-f131-e611-80f8-c4346bac3990)"
        );
    //URL
    $url ='https://vonageholdings.crm.dynamics.com/api/data/v8.0/incidents';
    //request for incidents
    $data_string = json_encode($data);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_VERBOSE, 1);
    curl_setopt($curl, CURLOPT_HEADER, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array($authHeader,
            'Content-Type:application/json','Accept:application/json;'));
    
    

It's showing "code":"","message":"You should specify a parent contact or account." Im trying to use navigation property.But I can't find the exact property to send customerId.

I have tried with following links: link1link2link3

I'm trying for a long time.It's too frustrated.

After I tried @Alex comment,I referred create incidents with following request,

$data = array('primarycontactid@odata.bind' =>"https://xxxx.crm.dynamics.com/api/data/v8.0/contacts(4bafdfb0-08d7-e511-80eb-c4346bac3990)",
        'incident_customer_accounts'=>array("title"=>"case_account","description" =>"case")
        );

It shows A node of type 'StartObject' was read from the JSON reader when trying to read the contents of the navigation property 'incident_customer_accounts'; however, a 'StartArray' node was expected. this error.

Now I think our request is correct but format is mismatching.


Solution

  • Finally case is create using below request in php

    $data = array("title"=>"test",
            "description" =>"case",
            "customerid_contact@odata.bind" =>"/contacts(c18df8d6-74d9-e511-80eb-c4346bac3990)"
            );
    
     $url ='https://yyyyy.crm.dynamics.com/api/data/v8.0/incidents';