简体   繁体   English

在s4sdk中创建TimeSheetEntry时发生异常

[英]Exception while creating TimeSheetEntry in s4sdk

Currently I am trying to use the S4/HANA SDK to create TimeSheetEntries but I have encountered some problems. 当前,我正在尝试使用S4 / HANA SDK创建TimeSheetEntries但是遇到了一些问题。 I am receiving this kind of exception: Cannot cast class java.util.HashMap to class com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.manageworkforcetimesheet.TimeSheetDataFields while I am executing createTimeSheetEntry method. 我收到这种异常:我执行createTimeSheetEntry方法时, Cannot cast class java.util.HashMap to class com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.manageworkforcetimesheet.TimeSheetDataFields Am I doing something wrong? 难道我做错了什么? I attached a simple example how I am using the API and if you could point me in the right direction I would really appreciate your efforts. 我附有一个简单的示例,说明我如何使用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);
}

Until the problem is patched, you could use a workaround by taking advantage of the OData query builder API and GSON for deserialization. 在解决问题之前,您可以利用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"

This problem of failing structured field deserialization has been fixed in the latest version of SAP S/4HANA Cloud SDK. 结构化字段反序列化失败的问题已在最新版本的SAP S / 4HANA Cloud SDK中修复。 To update, please find the <dependencyManagement> in your root Maven POM file and increment the <version> for sdk-bom to 1.9.2 要进行更新,请在根Maven POM文件中找到<dependencyManagement>并将sdk-bom<version>增至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>

During compilation Maven will automatically fetch the updated version from the Maven Central Repository. 在编译期间,Maven将自动从Maven中央存储库中获取更新的版本。 Simply run mvn clean install in your local working directory to make sure the new dependencies are propagated to all of your project modules. 只需在本地工作目录中运行mvn clean install即可确保将新的依赖项传播到所有项目模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM