EVAL block with variable inside?

Posted by gwf on 05-Dec-2014 11:36

I want to be able to concatenate an EVAL block so that I can use variables within the block, something like:

var1 = "'value'";

var ev1 = "#EVAL[rbv_api.getCount(123456789, 'picklist_field', var1)]";

alert(ev1);

(In order to cycle through the values in a picklist and get the count for each.)

But that doesn't return anything, although this does when a variable holds the entire string:

var ev1 = "#EVAL[rbv_api.getCount(123456789, 'picklist_field', 'value')]";

alert(ev1);

* returns the correct number from the EVAL operation

And then when I try different ways of concatenating the EVAL block, I get different results but none that work:

var ev1 = "rbv_api.getCount(123456789, 'picklist_field', 'value')";

alert(#EVAL[ev1]);

* returns nothing

 

var ev1 = "#EVAL[rbv_api.getCount(123456789, 'picklist_field', "; // breaking the string into two pieces

var ev2 = "'value')]";

var ev3 = ev1 + ev2;

alert(ev3);

* returns nothing

 

var ev1 = "#";

var ev2 = "EVAL[rbv_api.getCount(123456789, 'picklist_field', 'value')]"; //

var ev3 = ev1 + ev2;

alert(ev3);

* returns string “#EVAL[rbv_api.getCount(123456789, 'picklist_field', 'value')]”

How can I run an EVAL block with a variable inside?

Thank you,

Greg

All Replies

Posted by Gian Torralba on 05-Dec-2014 11:45

Hello,

Can you try creating a formula field and putting the value inside that field so that you can use it as a token in the variable parameter?

var ev1 = "#EVAL[rbv_api.getCount(123456789, 'picklist_field', '{!formula_field#value}')]";

Thank you,
Gian

Posted by gwf on 05-Dec-2014 11:53

Thank you, Gian.

I'm not sure that will work for what I'm trying to do, which is use an array of values in a picklist to produce a second array of the counts of each value. Then I will use the two arrays to build a pie chart showing the breakdown of records according to the picklist.

The array of values needs to be dynamically generated since the picklist may change when new records are uploaded (see community.progress.com/.../14742.aspx for my question on how to generate that array). Now I am trying to use an EVAL block in a for loop that will cycle through the values held in the first array to populate the second array of the corresponding counts.

What's the best way to do this?

Posted by Gian Torralba on 05-Dec-2014 12:18

Hello,

Where are you doing this exactly? If you are doing this inside a formula or a trigger, there is no need to create an EVAL block since you can use the rbv_api.getCount() directly and pass the variables inside the formula body. If you are doing it via client-side, you can use rbf_getCount2() inside the script component and add the variable in the parameter list.

EVAL blocks cannot read variables outside the block so I believe the method you are using right now will not work.

Thank you,
Gian

Posted by pvorobie on 05-Dec-2014 12:50

EVAL block is calculated on server side and replaced by result before sending HTML/JavaScript to the browser. For that reason EVAL block cannot use browser-side variables. It can, however, use any template tokens available in helper UI.

Posted by gwf on 05-Dec-2014 14:54

Both those explanations help, thank you. A resulting question I have is whether this is best done server- or client-side. I thought I would put the code in the same script component on the portal page that generates the chart, but can I generate the two arrays server-side and then access them with client-side code? If I am using rbf_getCount2() won't that quickly use up many API calls (I am building a dashboard with multiple charts)? What's the best practice here?

Posted by Gian Torralba on 05-Dec-2014 16:41

Hello,

It depends on the loop count. Will the loop scale over time? If so, I suggest that you create a new object to hold the values for the graph and just do a selectQuery() method to avoid the method loop. This will remove the multiple call that you will make if you are using the getCount() method. Take note that charts do slow down if there are to many data in it.

You also need to create additional server-side coding (triggers, formula) just to update the new graph object.

I hope this helps.

Thank you,
Gian

Posted by gwf on 09-Dec-2014 14:13

Thank you, Gian. I am a little stumped as to how to do this - how do I create an array of picklist values server-side? I can't get any formula to work with selectQuery(). Can you please show some sample code of using selectQuery() to populate an array?

And to clarify - are you recommending creating a new object with formula fields that will hold the values and record counts (in the original object) for the picklist?

Any further direction will be much appreciated!

Greg

Posted by Gian Torralba on 10-Dec-2014 10:10

Hello,

Please follow the steps provided below:

- Object A is the original object that holds the records.
- Object B is the new object that will hold the counter for the charts.

1. Create the object B that will be used to hold the integer count of records.
2. Create an integer field in object B this will hold the updated count value of records.
3. Create a string field in object B this will hold the picklist code.
4. Create a trigger in object A (after create, after update, after delete). This will update the record in object B if
    - The record with a picklist value already exists
    - If there is no record with the same picklist code found in object B, create a record with the picklist code from object A.
5. Every time object A's record is update, created or deleted, the trigger will fire and update records listed in object B.
6. Create a script component in the page you need to display the chart.

<script>
$(document).ready(function(){
  rbf_selectQuery("SELECT name, count FROM object_b", 1000, function(data){
    if(data.length > 0){
      for(var i=0; i<data.length; i++){
        /*this will loop each record in object_b and you can integrate it with your chart script*/
      }
    }
  });
});
</script>

Hope this helps a bit.

Thank you,
Gian

This thread is closed