簡體   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