pass extra parameter to callback function in rbf API call?

Posted by gwf on 24-Mar-2016 10:42

I have a series of fields, following this naming convention:

field1InUse [checkbox]

field1Label [text]

field1Value [picklist]

field2InUse [checkbox]

field2Label [text]

field2Value [picklist]

... and so on

On a portal page, I want to loop through these fields in a given record to first check if field1InUse is checked (i.e., has a value of 1), then get field1Label and field1Value to build the HTML with. The loop will then do the same for field2InUse, etc. Simplified example code I have tried looks like this:

<html>

<div id="d1"></div>
</html>
<script>
function buildHtml(data) { var d1 = document.getElementById("d1");
var p1 = document.createElement("p");
p1.textContent = data[0][0] + ": " + data[0][1];
d1.appendChild(p1); } function checkInUse(passedArray) { if (passedArray[0] == 1) { rbf_selectQuery("SELECT field" + passedArray[1] + "Label,field" + passedArray[1] + "Value#value FROM objectA WHERE id = 123456", 1, buildHtml); } }
for (i=1; i<3; i++) { var fieldNumber = i; rbf_selectValue("SELECT field" + fieldNumber + "InUse FROM objectA WHERE id = 123456", function(checkbox) { var arr1 = [checkbox, fieldNumber]; return arr1; }); } </script>

For one, at

    rbf_selectValue("SELECT field" + fieldNumber + "InUse FROM objectA WHERE id = 123456", function(checkbox) {
                    var arr1 = [checkbox, fieldNumber];
                    return arr1;
                    });

I get an error message that there is no close parenthesis ) after the arguments, although I am not sure why since there is one. And second, even if that works I am not sure this is the way to in essence pass two parameters to the callback function checkInUse which will test if field1InUse is checked and then, if so, create the second API call to return field1Label and field1Value. So how can I do this?

Any help will be much appreciated!

Thank you,

Greg

Posted by Shiva Duriseati on 29-Mar-2016 01:31

Hi Greg,

"callback" function is something which is called after the query part executes completely and you cannot pass a value which keeps changing(in this case "fieldnumber"). But for the values which remains constant can be used by defining function in the query itself as follows.(Taking your own example)

rbf_selectValue("SELECT field" + fieldNumber + "InUse FROM objectA WHERE id = 123456", function(checkbox) {
var arr1 = [checkbox, fieldNumber];
return arr1;
});

"checkbox" is an array of resultset of selectquery and fieldnumber will be "4"( since by the time callback is called query part executed completely without calling callback function.) So there will be no continuous call to callback function.

Lets take other example where a parameter remains unchanged and the same param want to be passed to callback function along with array.

var idValue="{!id}";
rbf_selectValue("SELECT name FROM Object_A where id="+idValue, function(arr){
//idValue can be used here
});


Please let me know if you any other queries.


Regards,
Shiva

All Replies

Posted by Shiva Duriseati on 25-Mar-2016 08:54

Hi Greg,

Yes having an ability to pass a parameter in callback function is good. But using an existing mechanism we can still achieve.

Instead of using two API calls we can use a single selectquery I have slightly modified your code and tested its working fine.

<script type="text/javascript">

 function checkInUseAndBuildHTML(arr){

   if(arr[0][0]==1){

   var d1 = document.getElementById("d1");

   var p1 = document.createElement("p");

   p1.textContent = arr[0][1] + ": " + arr[0][2];

   d1.appendChild(p1);

   }

 }

 for(var i=1;i<=3;i++){

  rbf_selectQuery("SELECT field"+i+"InUse,field"+i+"Label,field"+i+"Value#value FROM Object_One WHERE id=15170", 1,checkInUseAndBuildHTML)

 }

 </script>

Regards,

Shiva

Posted by gwf on 28-Mar-2016 09:00

Thank you very much, Shiva, that works perfectly.

For future reference, is it indeed possible to pass additional parameters in the rbf callback functions?

Thank you,

Greg

Posted by Shiva Duriseati on 29-Mar-2016 01:31

Hi Greg,

"callback" function is something which is called after the query part executes completely and you cannot pass a value which keeps changing(in this case "fieldnumber"). But for the values which remains constant can be used by defining function in the query itself as follows.(Taking your own example)

rbf_selectValue("SELECT field" + fieldNumber + "InUse FROM objectA WHERE id = 123456", function(checkbox) {
var arr1 = [checkbox, fieldNumber];
return arr1;
});

"checkbox" is an array of resultset of selectquery and fieldnumber will be "4"( since by the time callback is called query part executed completely without calling callback function.) So there will be no continuous call to callback function.

Lets take other example where a parameter remains unchanged and the same param want to be passed to callback function along with array.

var idValue="{!id}";
rbf_selectValue("SELECT name FROM Object_A where id="+idValue, function(arr){
//idValue can be used here
});


Please let me know if you any other queries.


Regards,
Shiva

Posted by gwf on 29-Mar-2016 11:37

Shiva,

That is very helpful, thanks so much!

Greg

This thread is closed