cloud-sdk-js icon indicating copy to clipboard operation
cloud-sdk-js copied to clipboard

Patch API for Business Partner

Open hoandreas opened this issue 9 months ago • 6 comments

Describe the Question

Hello,

So I followed the installation and generation steps in this documentation: https://api.sap.com/api/API_BUSINESS_PARTNER/cloud-sdk/JavaScript

And I have imported the client to consume the service via the generated typed client library. The get request was successful, but when I try to use the update function, I always get the .getCustomFields is not a function error message. And if I also include the date fields, it will throw momentInstance.unix is not a function error.

I followed this documentation for the update implementation. My code looks like this:

const {
  opApiBusinessPartnerSrv,
} = require("./generated/business-partner/OP_API_BUSINESS_PARTNER_SRV/service");
const { businessPartnerApi } = opApiBusinessPartnerSrv();

// and this is how the update function being called
      const updateResult = await businessPartnerApi
          .requestBuilder()
          .update(payload);

I tried building the entity first before sending the payload like this:

      const payload = await businessPartnerApi
          .entityBuilder()
          .fromJson(sanitizedPayload);

But the generated payload is an empty object. The original payload before being built is obtained from the fiori app via req.data. Am I missing some steps or how do I use this update function?

Thanks guys.

hoandreas avatar Feb 03 '25 06:02 hoandreas

Conceptually you make the right calls, but it is hard to determine the core of your issue with the information you provided.

Can you share:

  • Your node version
  • SAP Cloud SDK version
  • Any of the actual payload's contents

tomfrenken avatar Feb 03 '25 13:02 tomfrenken

Alternatively, you can try creating the entity on your own instead of passing a payload and see if you still receive errors, this would point at an erroneous payload.

import { apiBusinessPartner } from "./generated/API_BUSINESS_PARTNER/service"
const { businessPartnerApi } = apiBusinessPartner()
const payload = businessPartnerApi.entityBuilder().build();
const updateResult = await businessPartnerApi
    .requestBuilder()
    .update(payload)
    .execute({ destinationName: "MyDestination" });

tomfrenken avatar Feb 03 '25 13:02 tomfrenken

hello @tomfrenken, thank you for your reply. My Node version is v20.18.0 and these are the SAP cloud SDK that I use

dependencies:
    "@cap-js-community/odata-v2-adapter": "^1.13.7",
    "@sap-cloud-sdk/core": "^1.54.2",
    "@sap-cloud-sdk/http-client": "^3.22.2",
    "@sap-cloud-sdk/odata-v2": "^3.24.0",
    "@sap-cloud-sdk/odata-v4": "^3.22.2",
    "@sap-cloud-sdk/openapi": "^3.25.0",
    "@sap-cloud-sdk/openapi-generator": "^3.25.0",
    "@sap-cloud-sdk/resilience": "^3.22.2",

devDependencies:
    "@sap-cloud-sdk/generator": "^3.24.0",

And here's the example payload that I send:

{
  __metadata: {
    id: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')",
    uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')",
    type: 'API_BUSINESS_PARTNER.A_BusinessPartnerType'
  },
  BusinessPartner: '28',
  Customer: '123123qwe',
  Supplier: '28',
  AcademicTitle: '123123qwe',
  AuthorizationGroup: '',
  BusinessPartnerCategory: '1',
  BusinessPartnerFullName: ' T Test pzo Kred.1 Kreditor 1',
  BusinessPartnerGrouping: '0001',
  BusinessPartnerName: ' T Test pzo Kred.1 Kreditor 1',
  BusinessPartnerUUID: 'a3f8e2c1-4b76-4e5d-9c2d-1f8e4a5b6c7d',
  CorrespondenceLanguage: 'DE',
  CreatedByUser: 'SAMPLE_USR',
  CreationDate: '2023-05-09T00:00:00.000Z',
  CreationTime: null,
  FirstName: '',
  FormOfAddress: '0001',
  Industry: '',
  InternationalLocationNumber1: '0',
  InternationalLocationNumber2: '0',
  IsFemale: true,
  IsMale: false,
  IsNaturalPerson: '',
  IsSexUnknown: false,
  Language: '',
  LastChangeDate: '2023-05-10T00:00:00.000Z',
  LastChangeTime: null,
  LastChangedByUser: 'SAMPLE_USR',
  LastName: 'T Test pzo Kred.1 Kreditor 1',
  LegalForm: '',
  OrganizationBPName1: '',
  OrganizationBPName2: '',
  OrganizationBPName3: '',
  OrganizationBPName4: '',
  OrganizationFoundationDate: null,
  OrganizationLiquidationDate: null,
  SearchTerm1: 'PZO',
  AdditionalLastName: '',
  BirthDate: null,
  BusinessPartnerIsBlocked: false,
  BusinessPartnerType: '',
  ETag: 'SAMPLE_USR20250111111111',
  GroupBusinessPartnerName1: '',
  GroupBusinessPartnerName2: '',
  IndependentAddressID: '',
  InternationalLocationNumber3: '0',
  MiddleName: '',
  NameCountry: '',
  NameFormat: '',
  PersonFullName: '',
  PersonNumber: '23710',
  IsMarkedForArchiving: false,
  BusinessPartnerIDByExtSystem: '',
  TradingPartner: '',
  to_BuPaIdentification: {
    __deferred: {
      uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')/to_BuPaIdentification"
    }
  },
  to_BuPaIndustry: {
    __deferred: {
      uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')/to_BuPaIndustry"
    }
  },
  to_BusinessPartnerAddress: {
    __deferred: {
      uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')/to_BusinessPartnerAddress"
    }
  },
  to_BusinessPartnerBank: {
    __deferred: {
      uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')/to_BusinessPartnerBank"
    }
  },
  to_BusinessPartnerContact: {
    __deferred: {
      uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')/to_BusinessPartnerContact"
    }
  },
  to_BusinessPartnerRole: {
    __deferred: {
      uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')/to_BusinessPartnerRole"
    }
  },
  to_BusinessPartnerTax: {
    __deferred: {
      uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')/to_BusinessPartnerTax"
    }
  },
  to_Customer: {
    __deferred: {
      uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')/to_Customer"
    }
  },
  to_Supplier: {
    __deferred: {
      uri: "http://baseUrl.local:4004/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner('28')/to_Supplier"
    }
  }
}

Alternatively, you can try creating the entity on your own instead of passing a payload and see if you still receive errors, this would point at an erroneous payload.

But if I create the entity that way, how do I pass the payload that contains the updated data to hit the patch API?

hoandreas avatar Feb 04 '25 04:02 hoandreas

Facing same error while sending date in the update payload: Uncaught momentInstance.unix is not a function","stacktrace":["TypeError: momentInstance.unix is not a function","at serializeFromMoment (/home/vcap/deps/0/node_modules/@sap-cloud-sdk/odata-v2/dist/de-serializers/converters.js:62:38)

jamairaja avatar Apr 03 '25 14:04 jamairaja

Hi @hoandreas ,

Since you're getting your payload from an OData response, you need to convert it to an entity in the SDK. Entity deserialization is what you're looking for.

import { defaultDeSerializers, entityDeserializer } from '@sap-cloud-sdk/odata-v2';
const { opApiBusinessPartnerSrv } = require("./generated/business-partner/OP_API_BUSINESS_PARTNER_SRV/service");

const { businessPartnerApi } = opApiBusinessPartnerSrv();

const deserializedEntity = entityDeserializer(
  defaultDeSerializers
).deserializeEntity(sanitizedPayload, businessPartnerApi);

// perform any update and then call:
  const updateResult = await businessPartnerApi
          .requestBuilder()
          .update(deserializedEntity);

Let me know if this works.

deekshas8 avatar Apr 09 '25 08:04 deekshas8

Hi hoandreas,

We have a fix for momentInstance.unix is not a function. DateTime fields expects a momentjs instance to be passed in the payload. Check the document: https://sap.github.io/cloud-sdk/docs/js/features/odata/execute-request#conversion-of-odata-types

So you have to add momenjs plugin in your project and format your date accordingly.

jamairaja avatar Apr 09 '25 10:04 jamairaja

Closing this issue due to inactivity.

KavithaSiva avatar Sep 30 '25 13:09 KavithaSiva