Action "Update a field on row(s)" with a table's sum?

  • 3
  • Question
  • Updated 4 years ago
  • Answered
I have a popup that contains a table. Is there any way I can make an Action "Update a field on row(s)" that will, when I save my table's model, populate a related/parent record's field with the value that is the sum/total of a column in my table? Looks like Actions are designed to put a single record's value into the "Value" field of the action, but I'm guessing I don't have access to the calculated sum on the page?
Photo of Peter Bender

Peter Bender, Champion

  • 6,246 Points 5k badge 2x thumb

Posted 4 years ago

  • 3
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
I don't know how with Skuid/javascript, but Rollup Helper is an AppExchange app that will rollup anything via Apex Trigger. I use it all the time. Works with formula fields and can filter which child records to include in the rollup.
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Peter I'm going to concur with Pat.  I know we don't expose table summaries as a merge field (that could be put in a action step).  I also have been told by the dev team that we don't expose table summary items in the API yet.  

Roll up summaries, or this other AppExchange app...  Your best bet. 
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb
We had a similar rollup summary requirement but for packaging reasons weren't able to put a new rollup summary field on the parent object. I've managed to put together a snippet which sums the table values for our cost field, and populates the total into a field on the parent object. The snippet is run by an action button in a wizard.

Snippet below in case anyone else isn't able to do a rollup summary (although I do agree that a rollup summary is the best approach where possible):

var $ = skuid.$;

var parentEnquiry = skuid.model.getModel('pEnquiry');
var parentRow = parentEnquiry.getFirstRow();
var childRows = skuid.model.getModel('ChildModel');
var baseCost = 0;

$.each(childRows.data, function (i, row){    //iterate over the rows
   baseCost += row.Cost__c;
   console.log(baseCost);
});
console.log('************');
console.log(baseCost);

parentEnquiry.updateRow(parentRow,'custom_field__c',baseCost);

DISCLAIMER: It still requires some conditions to test for values in each row before adding to the baseCost total. If anyone can improve it I'd greatly appreciate it.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Greg,

You're probably well beyond this, since this thread is two months old, but you can use an if statement to make sure Cost__c is not empty before you add it, if you'd like.

var $ = skuid.$;
var parentEnquiry = skuid.model.getModel('pEnquiry');
var parentRow = parentEnquiry.getFirstRow();
var childRows = skuid.model.getModel('ChildModel');
var baseCost = 0;

$.each(childRows.data, function (i, row){    //iterate over the rows
   IF (row.Cost__c !== null) {       //check that Cost is not empty
      baseCost += row.Cost__c;
   }
   console.log(baseCost);
});
console.log('************');
console.log(baseCost);

parentEnquiry.updateRow(parentRow,'custom_field__c',baseCost);
Photo of Greg Jarrett

Greg Jarrett

  • 3,496 Points 3k badge 2x thumb
Thanks Jim.

I tried a few different methods on this and wound up using an || to input a 0 value if Cost__c was null or undefined:

baseCost += row.Cost__c || 0;

You've probably got significantly more JS knowledge than me, so I'm wondering whether the above method is appropriate or not. It's been working fine so far, but if there's a better way I'd love to know. 

I was getting a few errors using both null and undefined, and found that simply putting in the 0 value solved most of those errors.

Is the above method appropriate?
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Greg,

I'm sure the reverse is true, actually... I'm very new to javascript. Glad you found a solution!