Hello everyone,
I have a question about insert entity with Web API to Micosoft CRM online 2016.
package jzhao.crm.odata.example; import java.io.IOException; import org.apache.olingo.client.api.ODataClient; import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest; import org.apache.olingo.client.api.communication.response.ODataEntityCreateResponse; import org.apache.olingo.client.api.domain.ClientEntity; import org.apache.olingo.client.api.http.HttpClientException; import org.apache.olingo.client.api.uri.URIBuilder; import org.apache.olingo.client.core.ODataClientFactory; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.http.HttpHeader; public class ClientInputTest { ODataClient client = ODataClientFactory.getClient(); // String serviceRoot = "https://{yours}.api.crm.dynamics.com/api/data/v8.1"; String serviceRoot = "change to yours"; String accessToken = "change to your token"; public void testInsertEntity() { ClientEntity entity = client.getObjectFactory().newEntity(new FullQualifiedName("Microsoft.Dynamics.CRM.account")); entity.getProperties().add(client.getObjectFactory().newPrimitiveProperty("name", client.getObjectFactory().newPrimitiveValueBuilder().buildString("Test insert entity"))); URIBuilder uriBuilder = client.newURIBuilder(serviceRoot).appendEntitySetSegment("accounts"); ODataEntityCreateRequest<ClientEntity> entityCreateRequest = client.getCUDRequestFactory() .getEntityCreateRequest(uriBuilder.build(), entity); entityCreateRequest.addCustomHeader(HttpHeader.AUTHORIZATION, "Bearer " + accessToken); ODataEntityCreateResponse<ClientEntity> reponse = entityCreateRequest.execute(); if (reponse.getStatusCode() == 201) { // created } else { throw new HttpClientException(reponse.getStatusMessage()); } } public static void main(String[] args) throws IOException { ClientInputTest test = new ClientInputTest(); test.testInsertEntity(); }
I use think kind of java code to test insert entity. But it fails with:
Exception in thread "main" org.apache.olingo.client.api.communication.ODataClientErrorException: entity [HTTP/1.1 400 Bad Request] at org.apache.olingo.client.core.communication.header.ODataErrorResponseChecker.checkResponse(ODataErrorResponseChecker.java:75) at org.apache.olingo.client.core.communication.request.AbstractRequest.checkResponse(AbstractRequest.java:54) at org.apache.olingo.client.core.communication.request.AbstractODataRequest.doExecute(AbstractODataRequest.java:310) at org.apache.olingo.client.core.communication.request.cud.ODataEntityCreateRequestImpl.execute(ODataEntityCreateRequestImpl.java:88)
Dependenty:
<project xmlns="maven.apache.org/.../4.0.0" xmlns:xsi="www.w3.org/.../XMLSchema-instance" xsi:schemaLocation="maven.apache.org/.../4.0.0 maven.apache.org/.../maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jzhao.test</groupId> <artifactId>mscrm-odata-example</artifactId> <version>1.0-20170328</version> <packaging>jar</packaging> <name>mscrm-odata-example</name> <properties> <odata.version>4.3.0</odata.version> </properties> <dependencies> <dependency> <groupId>org.apache.olingo</groupId> <artifactId>odata-client-core</artifactId> <version>${odata.version}</version> </dependency> <dependency> <groupId>org.apache.olingo</groupId> <artifactId>odata-client-api</artifactId> <version>${odata.version}</version> </dependency> <dependency> <groupId>org.apache.olingo</groupId> <artifactId>odata-commons-api</artifactId> <version>${odata.version}</version> </dependency> <dependency> <groupId>org.apache.olingo</groupId> <artifactId>odata-commons-core</artifactId> <version>${odata.version}</version> </dependency> </dependencies> </project>
Any one who get this kind of issue before ?
Many thanks!
*This post is locked for comments