Skip to main content

I have a table of products and a quantity field where users will run down the table and enter quantities that a client may want to order.

I then have a mass action that adds these to the order but rather than have the user enter quantity AND select the relevant row tick boxes each time, can I have them organised to be auto selected for the mass action or alternately can I have the mass action only run on rows where something was entered into the quantity field?


Does it have to be a mass action? Depending on what your actions are, you may be able to move them into a button in a page title or navigation item. Then you wouldn’t have tick boxes to worry about.


David,

Your use case seems to be a good fit for a JavaScript snippet.  Replace your Mass Action with a ‘Save’ button that runs a snippet.  The snippet will load the model for the table, loop through each row and select rows where the quantity is not empty, add these rows to a second model and finally save the second model.

Thanks,

Bill


Thanks for the tips… new to snippets but I will have a go at it )

Appreciate the help.


I came up with this to loop through the table rows, collecting the rows with a quantity that isnt zero and then trying to add them as rows on my OrderProduct model but I am guessing it is a long way from working.


Anyone able to give me any pointers


var params = arguments[0],

$ = skuid.$;


var Products = skuid.model.getModel(‘AllProducts’);

var OrderProduct = skuid.model.getModel(‘OrderProduct’);

var Qty = “Qty__c”;

var IdsOfRowsWithQty = ;


$.each(Products.data,function(i,row){

if (Products.getFieldValue(row,qty) <> 0){

IdsOfRowsWithQty.push(row.Id);

}


$.each(OrderProduct.getRows(),function(){


OrderProduct.updateRow(this,‘Id’,‘IdsOfRowsWithQty’);

OrderProduct.updateRow(this,‘Quantity__c’,‘Qty’);


});


OrderProduct.save();


Hi David,


In order to update a row you need to call the method correctly. The updateRow(row, updates) takes two arguments: row → the row you want to update and updates → an object of fields which have to be named exactly as the fields in the model (think of it as if it is the old row, just with new values). If you’re not familiar with the structure of the row, use console.log(row) and you’ll see the details of the row-object.


To exactly answer your snippet-question:


if (Products.getFieldValue(row,qty) <> 0){ //not working
if (row.Qty__c > 0){ //rather use this

OrderProduct.updateRow(row, {'Id': IdsOfRowsWithQty}); //Correct updaRow call

More on updateRow() and other useful model methods here.


The second each loop is never closed, which will throw a syntax error (also move your second each loop out of the first each loop).


I came up with this. I couldn’t test it since I don’t have the models nor the data nor some other setup to test it.


var params = arguments 0],        $ = skuid.$,
Products = skuid.model.getModel('AllProducts'),
OrderProduct = skuid.model.getModel('OrderProduct'),
Qty = "Qty__c",
IdsOfRowsWithQty = w];
$.each(Products.getRows(), function(i,row){
if (row.Qty__c > 0){
IdsOfRowsWithQty.push(row.Id);
}
});
$.each(OrderProduct.getRows(), function(i, row){
OrderProduct.updateRow(row, {'Id': IdsOfRowsWithQty});
OrderProduct.updateRow(row, {'Quantity__c': Product.getRowById(row.Id).Qty__c});
});
OrderProduct.save();

You’ll maybe need to modify it to fit your exact needs.


Cheers


Hi David,<br><br>Thanks for the code. I really appreciate the help. Mine wasnt even close!<br><br>It didnt quite work but I think I worked out why. The Order Product rows being updated in each case I think need to be created first. <br><br>I tried bringing in the Order Id ok as a variable from the first row of the order object and inserting a createRow but haven't found the correct spot<br><br><br>var params = arguments[0], &nbsp; &nbsp;&nbsp; &nbsp; <br>$ = skuid.$,<br>&nbsp; &nbsp; <br>Products = skuid.model.getModel('AllProducts'),<br>OrderProduct = skuid.model.getModel('OrderProduct'),<br><br>Qty = "Qty__c",<br>IdsOfRowsWithQty = [];<br><br>$.each(Products.getRows(), function(i,row){<br>&nbsp; &nbsp; if (row.Qty__c &gt; 0){<br>&nbsp; &nbsp; &nbsp; &nbsp; IdsOfRowsWithQty.push(row.Id);<br>&nbsp; &nbsp; }<br>});<br><br>$.each(OrderProduct.getRows(), function(i, row){<br>&nbsp; &nbsp; OrderProduct.updateRow(row, {'Id': IdsOfRowsWithQty});<br>&nbsp; &nbsp; OrderProduct.updateRow(row, {'Quantity__c': Products.getRowById(row.Id).Qty__c});<br>});<br><br>OrderProduct.save();



David,

Try this variation…just be sure to set the ‘additional conditions’ to whatever fields you want in your order products.

var params = argumentsu0],       
$ = skuid.$,

Products = skuid.model.getModel(‘AllProducts’),
OrderProduct = skuid.model.getModel(‘OrderProduct’),
Qty = “Qty__c”,
IdsOfRowsWithQty = ;
$.each(Products.getRows(), function(i,row){
    if (row.Qty__c > 0){
      OrderProduct.createRow({
          additionalConditions: >{
              field: ‘Quantity__c’, value : row.Qty__c
          }, {
              field: ‘Id’, value: row.Id
          }]
      });
    }
});
OrderProduct.save();

Thanks,

Bill


Thanks so much for the help )

I will give it a try 


Reply