Hawk Posted August 9, 2011 Share Posted August 9, 2011 I have have an external data file that I am using to display a list of courses. Each record will display the courses that match a field in my data. That part works fine. One of the columns is a number that represents hours. I need to add all of the hours for a given record to get total hours and display that number. Thanks, Larry Link to comment Share on other sites More sharing options...
step Posted August 9, 2011 Share Posted August 9, 2011 How about something like this to push the number of records from each record in the external data file into an array and then return the sum amount of that array? var hours = []; var totalHours = 0; var records = externalDataFile.recordCount; // Start an array for your hours for (var i=1; i <= records; i++) { hours.push(externalDataFile.GetFieldValue(i,'Hours')); } //Sum your hours for (var n=0; n< hours.length; n++){ totalHours += Int(hours[n]); } return totalHours; Link to comment Share on other sites More sharing options...
Hawk Posted August 10, 2011 Author Share Posted August 10, 2011 Thanks for the help. The only change was. totalHours += Int(hours[n]); To totalHours += StringToNumber(hours[n]); I need to return a floation-point Number. Link to comment Share on other sites More sharing options...
Hawk Posted August 10, 2011 Author Share Posted August 10, 2011 I'm having a problem with the math on this. It seems that if I have more than one record match in the external data file it doubles the returned value. If only one match is found it is correct. //Check for a valid External File if(FusionPro.Composition.isPreview == true || FusionPro.inValidation == true) { Rule("OnJobStart"); } //Trim leading zeros from licenseid to find matches in External File function trimNumber(s) { while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); } return s; } var s = Field("licenseid"); var hours = []; var totalHours = 0; var numRecsExtDF = XDF.recordCount; //count records in database for (var i=1; i <= numRecsExtDF; i++){ //count records that match licenseid, push to aray and return total hours. if (XDF.GetFieldValue(i, 1) == trimNumber(s) && XDF.GetFieldValue(i, 5) == "Technical") { { hours.push(XDF.GetFieldValue(i, 4)); } for (var n=0; n< hours.length; n++){ totalHours += StringToNumber(hours[n]); } } } var result = totalHours.toFixed(2); return result; Can anyone tell me what I'm missing? Link to comment Share on other sites More sharing options...
Dan Korn Posted August 10, 2011 Share Posted August 10, 2011 Why are you putting the values into an array and then adding them? Just add the numbers as you find them in the file: if(FusionPro.inValidation) Rule("OnJobStart"); var s = parseInt(Field("licenseid"), 10); var totalHours = 0; for (var i = 1; i <= XDF.recordCount; i++) { //count records that match licenseid and return total hours. if (XDF.GetFieldValue(i, 1) == s && XDF.GetFieldValue(i, 5) == "Technical") totalHours += StringToNumber(XDF.GetFieldValue(i, 4)); } return totalHours.toFixed(2); I simplified a few other things too. Link to comment Share on other sites More sharing options...
Hawk Posted August 10, 2011 Author Share Posted August 10, 2011 That did it thanks for the help. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.