loop through rows in a model

  • 3
  • Question
  • Updated 3 months ago
  • Answered
Using something similar to the code below how can I write a javascript snippet to loop through all the data in a Model displayed in a table.

Basically I have 3 columns and one of the columns contains checkboxes.
The user can check any of the checkboxes for each row.
Once they click on the button, it calls the snippet, which should then loop through and get the Id's of the rows that have been checked.

(NB when looping through to check each row, to see what has been checked if the data has not been saved yet, can we still get the Id's of the checkboxes that have been checked?)

var params = arguments[0],
    $ = skuid.$;


var OutstandingItemModel = skuid.model.getModel('OutstandingItem');
var allOutstandingItemData = [];
allOutstandingItemData = OutstandingItemModel.data;
var i = 0;
for(i=0;i<allOutstandingItemData.length;i++){
var itemRow = itemData[i];
}
Photo of Eddie

Eddie

  • 720 Points 500 badge 2x thumb

Posted 4 years ago

  • 3
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Rows that have not been saved yet still have "temporary Ids", so yes you can get those rows' Ids as well.

If your checkbox field name is "My_Checkbox__c", then you could do something like this:

var params = arguments[0],
    $ = skuid.$;
var CHECKBOX_FIELD = "My_Checkbox__c";
var idsOfRowsThatAreChecked = [];
var OutstandingItemModel = skuid.model.getModel('OutstandingItem');
$.each(OutstandingItemModel.data,function(i,row){
   if (OutstandingItemModel.getFieldValue(row,CHECKBOX_FIELD)===true){
        idsOfRowsThatAreChecked.push(row.Id);
   }
});
// Display the ids of the rows that are checked
console.log(idsOfRowsThatAreChecked);
Photo of Eddie

Eddie

  • 720 Points 500 badge 2x thumb
Thanks that worked a charm.

Any idea how to pass the array of Id's to an apex webservice call as the below doesn't work:

var sResponse = new String(sforce.apex.execute("AppSummary","packReceivedButton",{OutstandingItems:idsOfRowsThatAreChecked}));

I get the below error, thanks

{faultcode:'soapenv:Client', faultstring:'Element {http://soap.sforce.com/schemas/package/AppSummary}OutstandingItems invalid at this location', }
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Check your Web Service call definition to make sure that you're passing in parameters exactly as your Web Service call is expecting them. Especially case sensitivity. If your parameter name is outstandingItems in your Web Service (camel-cased), then it will need to be outstandingItems in your web service call in JavaScript. 
Photo of Eddie

Eddie

  • 720 Points 500 badge 2x thumb
its ok, I worked it out, I changed the apex input param to accept Id[] instead of String.

Thanks
Photo of Sriram

Sriram

  • 70 Points
How do I need to resolve this issue same as above, when I am using above code, only pushed one record , not able to send to list of ids param , can u share a piece of code.
Photo of Eddie

Eddie

  • 720 Points 500 badge 2x thumb
one more question would you know how to close the popup from the javascript snippet?
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 43,986 Points 20k badge 2x thumb
Valid question that should be searchable. Please reference the new topic here: Close a Popup from a Javascript Snippet
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
skuid.$('.ui-dialog-content').last().dialog('close');