Search code examples
serviceodatasapui5hana

Calling XSJS file in SAPUI5 for writng data on HANA


I'm trying to create a service to write data from a SAPUI5 frontend to HANA tables. As far as I checked, I believe this is not possible via OData Services. So I've found another way, sing an XSJS file with a SQL INSERT statement.

Now, my problem is using this in UI5. As with OData, I would use something like oModel.create but now I think this doesn't work like that.

Those anyone has a clue?

Thanks!

Eva

UPDATE: After using the first answer, I tried to create an entry in a HANA Table, but I'm getting a 500 error. Here's the code of the xsjs file:

var data = '', conn = $.db.getConnection(), pstmt;  

        if($.request.body){
            data = $.request.parameters.get("firstName");   
        }

        var conn = $.db.getConnection();
        var pstmt = conn.prepareStatement( 'INSERT INTO "Z003HB1N"."T_TEST" (FIRSTNAME) VALUES(?)' );

        pstmt.setString(1,data);

        pstmt.execute();
        pstmt.close();

        conn.commit();
        conn.close();

        doResponse(200,'');

        $.response.contentType = 'text/plain';
        $.response.setBody('Upload ok');
        $.response.status = 200;

Any clue what might be wrong?


Solution

  • You can simply use .ajax calls to call your new oData service on HANA and use parameters to hand over the desired values to your .xsjs service.

    Example:

    var query = "firstName=Sherlock&lastName=Holmes"
    jQuery.ajax({
        url : "url/to/your/Service.xsjs?" + query,
        success : function(response) {
            // will be called once the xsjs file sends a response
            console.log(response);
        },
        error : function(e) {
            // will be called in case of any errors:
            console.log(e);
        }
    });
    

    On HANA you can access the provided parameters in your service like this:

    var firstName = $.request.parameters.get("firstName");
    var lastName = $.request.parameters.get("lastName");