Page 1 of 1

SUM list service results into page component

Posted: Tue Jul 21, 2015 2:58 am
by Jack Bua

I have found a lot of topics on things like this, but it has just confused me trying to get each one to work. I have spent hours on this with no result.

I have filtered result mapped to a collapsible set, and I need all the resulting "line_price" from collection "jobs" to SUM in the label "price_ttl".

Image

How can I do this? Where do I put the code, in success event or on success mapping to the page component?

Thank you in advance.


SUM list service results into page component

Posted: Tue Jul 21, 2015 11:53 pm
by Jack Bua

Please mark this as Answered. I would delete this post, but just in case the other posts like this does not help someone, this one might.

To get a sum of all values in a particular collection:
1) Make a service (query, list or read) on that collection
2) invoke that service on page load or show (or however you want)
3) OPTIONALLY add any filters by mapping them into the where field on the Before Send event
4) Put this code as javascript on the Success event of the service (not on a mapping):

//myVal should be whatever you want to name the variable
//column_name should be the name of your collection's column you want to sum
//destination_label should be the name of the label you want to display your results
var myVal=0;
for (var i=0; it know how to do steps 1-4 then I strongly suggest you do at least a dozen of the tutorials to get a hang of the basics.


SUM list service results into page component

Posted: Mon Jan 25, 2016 10:16 pm
by BTF

Hi Jack,

I tried to follow your post, but I am not having much luck . My version of the javascript is below and I have it running on the Success Event as a Run JavaScript action with not added code to the mapping action:

var totalpoints = 'PointTotal'; //the column returned with the value to sum
var sum = 0;

for (var i = 0; i < data.length; i++){
var points = parseInt(data[totalpoints]);
console.log("points for " + i + " Item is " + points);

Code: Select all

 if(points) 
 sum += points; 

}

console.log("sum = " + sum);

Apperyio("ProfileTotalPointsEarnedField").txt(sum); //the field to display total

Thanks for any advice you can provide.

Brian


SUM list service results into page component

Posted: Mon Jan 25, 2016 10:49 pm
by Jack Bua

codeasdasdasd/code


SUM list service results into page component

Posted: Mon Jan 25, 2016 11:03 pm
by Jack Bua

Instead of:

codesum += points; /code

try

codesum +=+ points; /code

This is what I have on one of my services:

codevar tm=0;
for (var i=0; i<data&#46;length; i++) {
if(data&#46;minutes){
tm+=+data&#46;minutes;
}
}
var th = Number(tm)/60;
th = th&#46;toFixed(2);
Apperyio&#46;storage&#46;selected_job_lineitem&#46;update("$['actual_hours']", th);/code


SUM list service results into page component

Posted: Tue Jan 26, 2016 9:31 pm
by BTF

Thanks for your help Jack. I tried changing += to +=+ without much luck, but it turned out I was storing the result using a label text property rather than a field value property. Once I corrected that it worked fine.


SUM list service results into page component

Posted: Tue Jan 26, 2016 9:33 pm
by Jack Bua

Good to hear.