/scripts/ahub.form.attachments.js
0

SAPUI5: Create Object dynamically

Dec 08, 2016 at 10:42 PM

52

avatar image

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Iftah Peretz
Dec 17, 2016 at 11:38 PM
0

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, ...
Share
10 |10000 characters needed characters left characters exceeded
Skip to Content