2

目前我正在尝试使用 S4/HANA SDK 来创建TimeSheetEntries,但我遇到了一些问题。我收到这种异常: Cannot cast class java.util.HashMap to class com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.manageworkforcetimesheet.TimeSheetDataFields 当我正在执行createTimeSheetEntry方法时。难道我做错了什么?我附上了一个简单的例子,我是如何使用 API 的,如果你能指出我正确的方向,我将非常感谢你的努力。

 public void createHardCodedEntry() throws ODataException {
    Calendar c = Calendar.getInstance();
    c.set(2017, Calendar.DECEMBER, Calendar.MONDAY);
    TimeSheetEntry entry = TimeSheetEntry.builder().timeSheetDate(c)
            //.timeSheetIsExecutedInTestRun(false)
            //.timeSheetIsReleasedOnSave(true)
            .timeSheetOperation("C")
            .timeSheetStatus("20")
            .personWorkAgreementExternalID("ADMINISTRATOR")
            .personWorkAgreement("50000033")
            .companyCode("1010")
            .timeSheetDataFields(TimeSheetDataFields.builder()
                    .timeSheetTaskLevel("NONE")
                    .timeSheetTaskType("ADMI")
                    .recordedHours(new BigDecimal(12))
                    .recordedQuantity(new BigDecimal(12))
                    .timeSheetTaskComponent("WORK")
                    .controllingArea("A000")
                    .hoursUnitOfMeasure("H")
                    .build())
            .build();

    ErpConfigContext erpConfigContext = new ErpConfigContext("S4HANA_CLOUD"); //this is the name of the destination configured in SCP
    TimeSheetEntry savedEntry = new DefaultManageWorkforceTimesheetService().createTimeSheetEntry(entry).execute(erpConfigContext);
}
4

2 回答 2

0

最新版本的 SAP S/4HANA Cloud SDK 修复了结构化字段反序列化失败的问题。要更新,请<dependencyManagement>在您的根 Maven POM 文件中找到 并将<version>forsdk-bom增加到1.9.2

<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <!-- ... -->

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.sap.cloud.s4hana</groupId>
                <artifactId>sdk-bom</artifactId>
                <version>1.9.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- ... -->

</project>

在编译期间,Maven 将自动从 Maven 中央存储库中获取更新的版本。只需mvn clean install在您的本地工作目录中运行,以确保将新的依赖项传播到您的所有项目模块。

于 2018-02-16T12:51:46.717 回答
0

在问题得到修复之前,您可以通过利用 OData 查询构建器 API 和 GSON 进行反序列化来使用解决方法。

// use GSON for deserialization steps
final Gson gson = new Gson();

// invoke "toQuery()" to work with OData query builder API
final TimeSheetEntry savedEntry = new DefaultManageWorkforceTimesheetService()
    .createTimeSheetEntry(entry)
    .toQuery()
    .execute(new ErpConfigContext("S4HANA_CLOUD"))
    .as(TimeSheetEntry.class);

// deserialize nested TimeSheetDataFields by reading values from the "custom fields"
savedEntry.setTimeSheetDataFields(
        gson.fromJson(
            gson.toJsonTree(savedEntry.getTimeSheetDataFields().getCustomFields()),
            TimeSheetDataFields.class
        ));

// continue to work with "savedEntry"
于 2018-02-13T08:35:58.933 回答