Search code examples
web-servicesodatadynamics-navdynamics-nav-2016

How to Delete records using Odata Dynamics NAV 2017 web services


I was developed a php application that connects to Microsoft Dynamics NAV 2017 OData Web Services, I can read (GET), and create (POST) with no problems,but for delete I receive the error 405, Microsoft say that it is possible to delete :

https://msdn.microsoft.com/es-es/library/dd355398(v=nav.90).aspx

https://msdn.microsoft.com/en-us/library/dn182582(v=nav.90).aspx

I check the page in Dynamics NAV that have a correct property InsertAllowed, ModifyAllowed, or DeleteAllowed, is set to Yes, and I have permissions to delete

After try with postman recevie the same error:

Delete Error Odata debug with postman

Can someone help me? Thanks


Solution

  • Finally I found the solution!! , I write myself to help another who is with the same problem:

    You only have to add the identifier on the request url, in my case the identifier of the customer table ('/Customer(No='.$identifier.')')

    This is a sample code in PHP with guzzle and table customer of Dynamics NAV:

     $client = new GuzzleHttpClient();
     $uri=env('HTTP_URIBASE', '');
     $apiRequest = $client->request('DELETE', $uri.'/Customer(No='.$identifier.')',[
            'auth' => [env('HTTP_USERNAME', 'usuari'),env('HTTP_PASSWORD', ''), 'ntlm' ],
            'headers' => ['Content-Type' => 'application/json', 
                           'Accept' => 'application/json']
      ]);
      $content = json_decode($apiRequest->getBody()->getContents());
    

    for updates (PATCH) I have to first read the etag of the reccord (@odata.etag), and add on the headers (If-Match value) for update:

     $client = new GuzzleHttpClient();
     $uri=env('HTTP_URIBASE', '');
     // get the recordset of the customer
     $apiRequest = $client->request('GET', $uri.'/Customer(No='.$identifier.')',[
                'auth' => [env('HTTP_USERNAME', 'usuari'),env('HTTP_PASSWORD', ''), 'ntlm' ]     
                ]);
     $content = json_decode($apiRequest->getBody()->getContents());
     $etag= $content->{'@odata.etag'};
    
     // update description of the customer
     $apiRequest = $client->request('PATCH', $uri.'/Customer(No='.$identifier.')',[
            'auth' => [env('HTTP_USERNAME', 'usuari'),env('HTTP_PASSWORD', ''), 'ntlm' ],
            'headers' => ['Content-Type' => 'application/json', 
                           'Accept' => 'application/json',
                           'If-Match' =>$etag ],
            'body'    => '{"Name":"'.$missatge.'"}' 
             ]);
     $content = json_decode($apiRequest->getBody()->getContents());