Can I have a row be selected whenever a field is edited?

  • 1
  • Question
  • Updated 3 years ago
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?
Photo of David Forder

David Forder

  • 2,144 Points 2k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Skuidward Tentacles (Raymond)

Skuidward Tentacles (Raymond), Champion

  • 17,224 Points 10k badge 2x thumb
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.
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
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
Photo of David Forder

David Forder

  • 2,144 Points 2k badge 2x thumb
Thanks for the tips.. new to snippets but I will have a go at it )

Appreciate the help.
Photo of David Forder

David Forder

  • 2,144 Points 2k badge 2x thumb
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();
Photo of David Giger

David Giger

  • 1,758 Points 1k badge 2x thumb
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 = [];
$.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
Photo of David Forder

David Forder

  • 2,144 Points 2k badge 2x thumb
Hi David,

Thanks for the code. I really appreciate the help. Mine wasnt even close!

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.

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


var params = arguments[0],       
$ = 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){
        IdsOfRowsWithQty.push(row.Id);
    }
});

$.each(OrderProduct.getRows(), function(i, row){
    OrderProduct.updateRow(row, {'Id': IdsOfRowsWithQty});
    OrderProduct.updateRow(row, {'Quantity__c': Products.getRowById(row.Id).Qty__c});
});

OrderProduct.save();
Photo of Bill McCullough

Bill McCullough, Champion

  • 12,436 Points 10k badge 2x thumb
David,

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

var params = arguments[0],       
$ = 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
(Edited)
Photo of David Forder

David Forder

  • 2,144 Points 2k badge 2x thumb
Thanks so much for the help )

I will give it a try