Search code examples
javasap-cloud-sdks4hana

How to create a contact of a sourcing project using SAP Cloud SDK and a generated OData v4 client?


I generated an ODATA v4 client for the SAP S/4HANA Sourcing Project API like described here.

All in all it works quite well. I can query, update or delete sourcing project data, milestones and contacts using batch requests, e.g. like this:

OPSOURCINGPROJECT0001Service sourcingProjectService = new DefaultOPSOURCINGPROJECT0001Service()
    .withServicePath("/sap/opu/odata4/sap/api_sourcingproject/srvd_a2x/sap/sourcingproject/0001");

BatchRequestBuilder batchRequestBuilder = sourcingProjectService.batch();

batchRequestBuilder.addChangeset(sourcingProjectService.deleteSourcingProjectContact(sapContact1));
batchRequestBuilder.addChangeset(sourcingProjectService
    .updateSourcingProjectContact(sapContact).includingFields(Contact.BUSINESS_PARTNER));
batchRequestBuilder.addChangeset(sourcingProjectService
    .updateSourcingProject(sourcingProject).includingFields(SourcingProject.PURCHASER_RESPONSIBLE));

BatchResponse batchResponse = batchRequestBuilder
    .withHeader("prefer", "odata.continue-on-error")
    .withQueryParameter("saml2", "disabled")
    .execute(destination);

However I can't figure out, how to create a new contact like it is described for the javascript version of the client, see here.

How can I do that in a batch request? There is no createSourcingProjectContact-method in the generated service-class.

I tried to use the CreateRequestBuilder directly cheating a bit like this:

private CreateRequestBuilder<Contact> createSourcingProjectContact(SourcingProject sourcingProject, Contact sapContact) {
    String servicePath = config.getSapServicePathSourcingProject();
    String resourceEntity = "SourcingProject(" + sourcingProject.getSourcingProjectUUID() + ")/_SourcingProjectContact";
    return new CreateRequestBuilder<>(servicePath, sapContact, resourceEntity);
}

But when I add the result to the batch using the batchRequestBuilder it will escape the URI of the corresponding post request and transform ( to %28 for example which renders that part of the batch request invalid as far as the SAP-partner-system is concerned.

It also does not work to send the create-requests outside of a batch, since still the SDK will encode the '/' in the 'resourceEntity' and such in the resulting URI.

Any help would be highly appreciated, since it is a pressing matter. :-)


Solution

  • Please use the forEntity API and pass it on to the BatchRequestBuilder after the create request of sourcing project data.

    sourcingProjectService.forEntity( sourcingProject ) // The project to which contact should be added.
        .navigateTo( theNavigationPropety ) // Should be something like SourcingProject.TO_SOURCING_PROJECT_CONTACT
        .create( contact ); //Supply your contact object here