Using Rollbase API in script component?

Posted by gwf on 01-Oct-2014 09:55

Hello all,

I'm a business user building out an app on Rollbase. I want to write formulas using the Rollbase API in script components in the page editor but can't get it to work. A simple example would be to display the number of records in a view, which I am writing as:

<script>

var totalRecords = rbv.getCount(109623509);

document.write(totalRecords);

</script>

What am I doing wrong?


Thanks for your help!

All Replies

Posted by pvorobie on 01-Oct-2014 10:38

You can use Rollbase server-side API in EVAL[ ] block. This block will be evaluated on server side during page rendering process.

Posted by gwf on 01-Oct-2014 11:13

Thank you - what is the correct syntax? I tried

<script>

#EVAL[

var totalRecords = rbv.getCount(109623509);

document.write(totalRecords);

]

</script>

but that didn't work...

Posted by Gian Torralba on 01-Oct-2014 11:28

Hello,

Try using this syntax since you are missing the _api part. rbv_api.getCount();

Thank you,

Gian

Posted by gwf on 01-Oct-2014 11:35

Thank you, but that still doesn't work...

Posted by pvorobie on 01-Oct-2014 11:39

This is a little ticker: rbv_api lives on server side, while document.write - on client side. So try this instead:

<script>

document.write(#EVAL[rbv.getCount(109623509)]);

</script>

Content of EVAL block will be replaced by calculated value in HTML sent to the browser.

Posted by pvorobie on 01-Oct-2014 11:40

document.write(#EVAL[rbv_api.getCount(109623509)]);

Posted by gwf on 01-Oct-2014 11:50

Got it, thank you very much!

Posted by gwf on 24-Oct-2014 10:31

OK, stumped again...

I want to use a script component on a portal page to loop through records and display fields in a table. Everthing works fine if it's with a token within html:

{!#LOOP_BEGIN.all#112247192}

<p>It's a {!product_name}</br></p>

{!#LOOP_END.all}

But when I try to get the field value inside script, I can't get it to work:

{!#LOOP_BEGIN.all#112247192}

<script>

var text = {!product_name};

document.write(text);

</script>

{!#LOOP_END.all}

(Although the odd thing is it seems to work for numeric fields, but not text fields.)

I've tried everything I can think of - with #EVAL[] blocks, all the rbf and rbv api queries related to "getField"... what am I missing??

Posted by Godfrey Sorita on 24-Oct-2014 11:09

It is not working mainly because the parsed value of the token {!product_name} is treated as a variable in your JavaScript code. If you had a product name TEST, the code will be evaluated as:

<script>
var text = TEST;
document.write(text);
</script>

The code basically returns an error because TEST variable is not defined.

Enclosing the token with a single or double quotation mark should fix the problem.

Posted by gwf on 24-Oct-2014 12:03

Perfect, thank you!

This thread is closed