Empty context sent to RAP behaviour implementation

34 views Asked by At

Can someone help with the below Issue. I am trying to create a simple ui5 app based on a RAP ODATA service.

There is a form with 2 input fields and submit button to create the entry at the backend. The backend behaviour implementation does get called but its passing empty context. I have tried with different types of forms but the same issue with each one of them.

Controller

`sap.ui.define([
    'jquery.sap.global',
    'sap/ui/core/mvc/Controller',
    'sap/m/MessageToast',
    'sap/m/MessageBox',
    'sap/ui/model/Filter',
    'sap/ui/model/json/JSONModel',
    'sap/ui/core/routing/History',
    'sap/ui/model/odata/v2/ODataModel'
], function (jQuery, Controller, MessageToast, MessageBox, Filter, JSONModel, History, ODataModel) {
    "use strict";
    var counter;

    return Controller.extend("questionaire.controller.CreateQuestion", {

        onInit: function () {
            var oModel, oView;

            this.oModel = new sap.ui.model.odata.ODataModel("/sap/opu/odata/sap/ZSB_QUESTIONAIRE/", true);

            oView = this.getView();

            oView.setModel(this.oModel);

            this.oRouter = sap.ui.core.UIComponent.getRouterFor(this);

            this.oRouter
                .getRoute("CreateQuestion")
                .attachPatternMatched(this._onRouteMatched, this);
        },

        _onRouteMatched: function (oEvent) {
            // debugger;
            // this.oModel.setDeferredGroups(["CreateQuestion"]);
            this.oModel.setDefaultBindingMode("TwoWay");

            var question = this.getView().byId("_IDGenInput1").getValue();              //Get the question
            // var anstype = this.getView().byId("IDAnswerType").getValue();        //get the selected answer type


            var oEntry = {};
            oEntry.QuestionLabel = question;

            this.oContext = this.oModel.createEntry("/questions", oEntry, {
                // groupID: "CreateQuestion",
                success: function (oData) {
                    console.log(oData)
                },
                error: function (error) {
                    console.log(error)
                },
            });
            
            this.getView().byId("QuestionForm").setBindingContext(this.oContext);
        },

        onNavPress() {
            const oHistory = History.getInstance();
            const sPreviousHash = oHistory.getPreviousHash();
            const oRouter = this.getOwnerComponent().getRouter();
            if (sPreviousHash != undefined) {
                window.history.go(-1);
            } else {
                const oRouter = this.getOwnerComponent().getRouter();
                oRouter.navTo("Home");
            }
        },

        onSaveQuestion() {
            debugger;
            
            this.getView().getModel().submitChanges();
        },

        onCancelQuestion() {
            this.onNavPress();
        }
    });
});`

Form

` <mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns:l="sap.ui.layout" xmlns:f="sap.ui.layout.form"   xmlns:core="sap.ui.core" xmlns="sap.m" controllerName="questionaire.controller.CreateQuestion">
    <VBox id="_IDGenVBox1" class="sapUiSmallMargin">
        <f:Form id="QuestionForm" editable="true" ariaLabelledBy="Title1">
            <f:toolbar>
                <Toolbar id="TB1">
                    <Title id="Title1" text="Create Question"/>
                    <ToolbarSpacer id="_IDGenToolbarSpacer1"/>
                    <Button id="_IDGenButton1" icon="sap-icon://settings"/>
                    <Button id="_IDGenButton2" icon="sap-icon://drop-down-list"/>
                </Toolbar>
            </f:toolbar>
            <f:layout>
                <f:ResponsiveGridLayout id="_IDGenResponsiveGridLayout1" labelSpanXL="4" labelSpanL="3" labelSpanM="4" labelSpanS="12" adjustLabelSpan="false" emptySpanXL="0" emptySpanL="4" emptySpanM="0" emptySpanS="0" columnsXL="2" columnsL="1" columnsM="1" singleContainerFullSize="false"/>
            </f:layout>
            <f:formContainers>
                <f:FormContainer id="_IDGenFormContainer1" ariaLabelledBy="Title2">
                    <f:toolbar>
                        <Toolbar id="_IDGenToolbar1">
                            <Title id="Title2" text="Question"/>
                            <ToolbarSpacer id="_IDGenToolbarSpacer2"/>
                            <Button id="_IDGenButton3" icon="sap-icon://settings"/>
                        </Toolbar>
                    </f:toolbar>
                    <f:formElements>
                        <f:FormElement id="_IDGenFormElement1" label="Question ID">
                            <f:fields>
                                <Input value="{QuestionId}" id="IDQuestionId" width="5%" editable="false"/>
                            </f:fields>
                        </f:FormElement>
                        <f:FormElement id="_IDGenFormElement2" label="Question">
    <f:fields>
    <Input id="_IDGenInput1" value="{QuestionLabel}"/>
                            </f:fields>
                        </f:FormElement>
        <f:FormElement id="_IDGenFormElement4" label="Answer Type">
    <f:fields>
    <Select width="100%"  id="IDAnswerType" selectedKey="{AnswerType}" maxWidth="25%">
                                    <items>
                                    <core:Item id="_IDGenItem1" text="YesNo"/>
                                    <core:Item id="_IDGenItem2" text="ShortText"/>
                                    <core:Item id="_IDGenItem3" text="Date"/>
                                    <core:Item id="_IDGenItem4" text="MultilineFreeText"/>
                                    <core:Item id="_IDGenItem5" text="Money"/>
                                    <core:Item id="_IDGenItem6" text="Percentage"/>
                                    </items>
                                </Select>
                            </f:fields>
                        </f:FormElement>
                    </f:formElements>
                </f:FormContainer>
            </f:formContainers>
        </f:Form>
    </VBox>
    <OverflowToolbar width="100%" id="toolbar1">
        <content>
    <OverflowToolbarButton icon="sap-icon://save" id="_IDSave" type="Accept"  text="Save" press="onSaveQuestion"/>
    <OverflowToolbarButton icon="sap-icon://cancel" id="_IDCancel" type="Reject"       text="Cancel" press="onCancelQuestion"/>
        </content>
    </OverflowToolbar>
    </mvc:View>`

Behaviour definition - Projection

   projection;
   strict ( 1 );

   define behavior for ZC_P_QUESTIONS_CDS alias questions
   {
   use create;
   use update;
   use delete;
   }

Behaviour definition - CDS

   unmanaged implementation in class zbp_i_question_cds unique;
   strict ( 1 );

   define behavior for ZI_QUESTION_CDS alias questions
   //late numbering
   lock master
   authorization master ( instance )
   //etag master <field_name>
  {

  create;
  update;
  delete;

  field ( readonly : update ) question_id;

  mapping for ztquestionsc
    {
      question_id    = question_id;
      question_label = question_label;
      answer_type    = answer_type;
    }
  }

This should ideally call the behaviour implementation for CREATE but instead its passing empty context.

0

There are 0 answers