on 2025 Sep 03 2:35 PM
Hi everyone,
I'm creating an application with SAP Build Apps, but I got stuck with some Java Script enhancement.
I got an object, which is transformed into a JSON Object and mapped to the output parameter:
Afterwards I'm mapping this to a app variable:
Everytime I'm calling the function, all the other information are loaded, but only the urgency is not set:
The Javascript Log shows the right information (medium) but the frontend only shows undefined:
Any idea?
BR
Request clarification before answering.
Hello there, this seems to be a js syntax mistake rather than an issue with the custom js block in SAPBA.
You return your value as `return { myObj }`. Which if you would log that to the console result in the following structure:
```
{ "myObj": { "urgency": "", ... } }
```
Instead of your expected output where you defined a structure like:
```
{ "urgency": "", ... }
```
So the proposed solution is to replace your `return { myObj }` with the `return myObj` line simply.
However, let me give a few more tips here on the custom js in SAPBA, for things like this, you probably would have been better off with using a 'set app variable' function with a formula like this:
```
DECODE_JSON(appVars.aiInput[1].content.urgency)
```
By doing it with the filters instead of js you spare yourself some of the tricky aspects of the custom js.
Lastly, if you really want to use the custom js, then note the following:
1. The output is most robust if returned as an array of objects (list of objects), e.g. `return [0, myObj]`. Why? Because in SAPBA there is the possibility to define multiple outputs from the cutom JS, e.g. one for success, one for error, one for default output etc. You can see prime examples of this with essentially any data related logic item in SAPBA.
2. Master the output schema definition. They way you defined the output is just right, to make the best use of the custom js, one has to be good at defininng the variable schemas in SAPBA, especially when it comes to list of things.
3. Best practice, rename the input properties to something you will remember and understand in the future. Every property that you define on the left side will become a property of the `inputs` object. You are accessing it via the `inputs.input1` path, which is fine, but a slightly more readable and maintainable option would be to do the following: `const { input1 } = inputs;` This destructuring helps in making sure you have more readable and maintainable code, as you would reference this new variable instead of the inputs object always. Just imagine if you would have here a 40-90 line js block and would need to find a typo...
Hope this helps and good luck with SAPBA.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 15 | |
| 9 | |
| 6 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.