繁体   English   中英

在s4sdk中创建TimeSheetEntry时发生异常

[英]Exception while creating TimeSheetEntry in s4sdk

当前,我正在尝试使用S4 / HANA SDK创建TimeSheetEntries但是遇到了一些问题。 我收到这种异常:我执行createTimeSheetEntry方法时, Cannot cast class java.util.HashMap to class com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.manageworkforcetimesheet.TimeSheetDataFields 难道我做错了什么? 我附有一个简单的示例,说明我如何使用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);
}

在解决问题之前,您可以利用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"

结构化字段反序列化失败的问题已在最新版本的SAP S / 4HANA Cloud SDK中修复。 要进行更新,请在根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>

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

暂无
暂无

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

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