Hello Friends,

I’m writing this blog post to share my experience on a specific development task on How to send Data from UI5 to Header and Line Item List Table in SAP System using OData Service.


1.JPG

As per above Screen shot we need to read the data from .CSV file and Simple Form and Finally Sent it to SAP System


As per below code i can read the data from the .CSV file and convert into JSON format.


change=”handleUploadComplete” method in <u:FileUploader>

fileUploader is ID of <u:FileUploader>



/* =========================================================== */
        /* Excel File upload into the ECC System.                      */
        /* =========================================================== */
        handleUploadComplete: function(oEvent) {
            var ofileName = this.getView().byId("fileUploader").getValue();
            var ofileNameExt = ofileName.substring(ofileName.indexOf('.')+1);
            if(ofileNameExt==="csv"){
            var that = this;
            var file = oEvent.getParameter("files")[0];
            if (file && window.FileReader) {
                var reader = new FileReader();
                reader.onload = function(evn) {
                    var strCSV = evn.target.result; //string in CSV
                    that.csvJSON(strCSV);
                };
                reader.readAsText(file);
            }
            }
            else
            {
            sap.m.MessageBox.show("Please Select .CSV File formate", {
                            icon: sap.m.MessageBox.Icon.WARNING,
                            title: "Upload Quotation",
                            actions: [sap.m.MessageBox.Action.OK],
                            onClose: function(oAction) {
                                // Close the window.
                            }
                        });   
            }
        },
        csvJSON: function(csv) {
            var lines = csv.split("\n");
            //var result = [];
            this.result = [];
            var headers = lines[3].split(",");
            for (var i = 4; i < lines.length - 1; i++) {
                var obj = {};
                var currentline = lines[i].split(",");
                for (var j = 0; j < headers.length; j++) {
                    obj[headers[j]] = currentline[j];
                }
                this.result.push(obj);
            }
            // var oStringResult = JSON.stringify(result);
            //var oFinalResult = JSON.parse(oStringResult.replace(/\\r/g, ""));
            this.oStringResult = JSON.stringify(this.result);
            this.oFinalResult = JSON.parse(this.oStringResult.replace(/\r/g, ""));
        },
        /*
         * End Excel Uplaod Code  *
         */

Now when we click on “Submit” event as per above screen. I need to read the .CSV file Data and Simple Form Data

Using Deep-entity Set we will send it to SAP System.


onCreate: function(oEvent) {
            var oQuoteMode = this.getView().byId("Radio_Group").getSelectedIndex();
            if (oQuoteMode === 0) {
           
                // Line Items
                var oExcelLineItems = [];             
                for (var k = 0; k < finalModel.length; k++) {
                    oExcelLineItems.push({                       
                        "STYLENO": finalModel[k]["Style No."],
                        "QTY": finalModel[k]["Units Ordered"]
                       
                    });
                }
                // Header Items Info
                var ofileNamePath = this.getView().byId("fileUploader").getValue();
                var oOrderType = this.getView().byId("BSARK_id").getValue();
                var oPriceGroup = this.getView().byId("KONDA_id").getValue();             
             
                var oRequestedDeliveryDate = this.getView().byId("VDATU_id").getValue();
                var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({pattern : "yyyy-MM-dd\'T\'HH:mm:ss" }); 
                var dateStr = dateFormat.format(new Date(oRequestedDeliveryDate));  
             
               
                var uploadQuoteData = {
                    "FILENAME": ofileNamePath,
                    "BSARK": oOrderType,
                    "KONDA": oPriceGroup,                  
                    "VDATU": dateStr
                   
                };
                //Add child to request
                uploadQuoteData.QuoteDetailsInputSet = oExcelLineItems;
                //uploadQuoteData.QuoteDetailsInputSet = finalModel;
                // Set Data to ODataModel
                this.getView().getModel("QuoteModel").create('/<Your Entity Set>', uploadQuoteData, {
                    success: function(oData, oResponse) {
                        var sucessMessage = JSON.parse(oResponse.headers['sap-message'].replace(/\r/g, ""));
                        sap.m.MessageBox.show(sucessMessage.message, {
                            icon: sap.m.MessageBox.Icon.SUCCESS,
                            title: "Success",
                            actions: [sap.m.MessageBox.Action.OK],
                            onClose: function(oAction) {
                                // Clear Input Values after Submit                              
                             
                            }
                        });
                    },
                    error: function(oError) {
                        var errMessage = JSON.parse(oError.response.body.replace(/\r/g, ""));
                        //sap.m.MessageBox.show(errMessage.error.innererror.errordetails[0].message, {
                            sap.m.MessageBox.show(errMessage.error.message.value, {
                            icon: sap.m.MessageBox.Icon.ERROR,
                            title: "Error",
                            actions: [sap.m.MessageBox.Action.OK]
                        });
                    }
                });
            }

Hope this is helpful All !!!

Regards

Vijay Kalluri

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply