Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5: Create Object dynamically

0 Kudos

Hi Experts!

I´m looking for an option to create dynamically a requestBody object for an odata post.

Example:

Ich have a couple of variables where I'm looking for the field input:

var RepaymentAmount = sap.ui.getCore().byId("RAinput").getValue();
var TermEnd = sap.ui.getCore().byId("Tinput").getValue();

Based on these values I have an requestBody object:

var requestBody = {};
    requestBody.Repayment = RepaymentAmount;
    requestBody.TermE = TermEnd;
...
oDataModel.create("/LoanCreate", requestBody, ...

But in some product cases the TermEnd is empty, because it will be calculated within the odata service. But if TermEnd value is empty in the object my odata.create runs into an error. To avoid that, my idea is to create the object dynamically. I´ll check whether a value is true or false. So in this example I will not include or delete the "requestBody.TermE" in the object. Is that somehow possible?

Best Regards,

Matthias

1 REPLY 1

iftah_peretz
Active Contributor
0 Kudos

Hey,

Why not to just add a check to see what's in it and if it's empty replace it with a known value that works in the "create()" method?

In your example:

const emptyConst = 'EMPTY'; //Could be '' as well, as long as you decode it the same all the way

var requestBody = {};
    requestBody.Repayment = RepaymentAmount;
    TermEnd === null ? requestBody.TermE = emptyConst : requestBody.TermE = TermEnd;
...
oDataModel.create("/LoanCreate", requestBody, ...