How to Delete records using Odata Dynamics NAV 2017 web services

随声附和 提交于 2019-12-06 04:18:58

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());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!