Jump to content

sum Total of a field


david.young

Recommended Posts

Thanks to the example of how to work with an external data file I have been able to get the data into the template but one field,'Gift Receipt Amount', needs to show a total at the end. Attached is how the out put needs to look

Thanks in advance

David

 

for (recordWalker=1; recordWalker <= numRecsExtDF; recordWalker++)

{

//Compare the value of the CID field in the current record

//we are looking at in the external data file to the value

//of the CustomerID field in the main data file.

if (externalDF.GetFieldValue(recordWalker, 'Constituent ID') == Field("Constituent ID"))

{

//Check to see if this is a TAX item. If it is, let's format it differently

//if (externalDF.GetFieldValue(recordWalker, 'Product-Purchased') == 'Tax')

//{

// returnStr += '<br><b>Total Tax - ' + externalDF.GetFieldValue(recordWalker, 'Price') + '</b>';

 

//}

//else //OK - so this is a normal product - not a Tax item

{

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Date');

returnStr += '<t>';

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Type');

returnStr += '<t>';

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Amount');

returnStr += '<t>';

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Receipt Number');

returnStr += '<t>';

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Receipt Amount');

returnStr += '<br>';

}

}

}

 

 

return returnStr;

tax statements 1.pdf

Link to comment
Share on other sites

Just add a variable ("total") to hold the sum of each amount and add to it as you're stepping through the external data. My edits in red:

[color="red"]var total = 0;[/color]

for (recordWalker=1; recordWalker <= numRecsExtDF; recordWalker++)
{
//Compare the value of the CID field in the current record
//we are looking at in the external data file to the value
//of the CustomerID field in the main data file.
   if (externalDF.GetFieldValue(recordWalker, 'Constituent ID') == Field("Constituent ID")) {
       returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Date');
       returnStr += '<t>';
       returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Type');
       returnStr += '<t>';
       returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Amount');
       returnStr += '<t>';
       returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Receipt Number');
       returnStr += '<t>';
       returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Receipt Amount');
       returnStr += '<br>';
       [color="red"]total += StringToNumber(externalDF.GetFieldValue(recordWalker, 'Gift Receipt Amount').replace(/[^\d\.]/g,''));[/color]
   }
}

[color="Red"]returnStr += '<t><t><t><t>$' + FormatNumber("#.00",total);[/color]

Link to comment
Share on other sites

Thanks Step,

I copied and pasted your code like this, and when I click validate I get an error "Function does not return a value"

 

//Now, loop through all records in the external data file and

//find the records that belong to the customer. This is done

//by comparing the value of "CustomerID" from the main input

//data file to the value of the "CID" field in the external data

//file.

//

//Note that in this example, there can be multiple records in the

//external data file for each customer which is why we are going to

//look at every record in the external data file.

var total = 0;

 

for (recordWalker=1; recordWalker <= numRecsExtDF; recordWalker++)

{

//Compare the value of the CID field in the current record

//we are looking at in the external data file to the value

//of the CustomerID field in the main data file.

if (externalDF.GetFieldValue(recordWalker, 'Constituent ID') == Field("Constituent ID"))

{

//Check to see if this is a TAX item. If it is, let's format it differently

//if (externalDF.GetFieldValue(recordWalker, 'Product-Purchased') == 'Tax')

//{

// returnStr += '<br><b>Total Tax - ' + externalDF.GetFieldValue(recordWalker, 'Price') + '</b>';

 

//}

//else //OK - so this is a normal product - not a Tax item

{

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Date');

returnStr += '<t>';

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Type');

returnStr += '<t>';

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Amount');

returnStr += '<t>';

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Receipt Number');

returnStr += '<t>';

returnStr += externalDF.GetFieldValue(recordWalker, 'Gift Receipt Amount');

returnStr += '<br>';

total += StringToNumber(externalDF.GetFieldValue(recordWalker, 'Gift Receipt Amount').replace(/[^\d\.]/g,''));

 

}

}

}

 

 

returnStr += '<t><t><t><t>$' + FormatNumber("#.00",total);

Link to comment
Share on other sites

Please understand that I've not seen all of the rules in your template or your data. That being said, the code that I added to your existing code wasn't meant to be copied-and-pasted and assumed to work perfectly for your job without being tweaked. It was merely an example of how I would create a variable to hold a sum of a field. It's difficult to say why it's not working for you because I haven't seen an example of the data file you're using or your template. If you collect your template and data to upload to the forum, I (or any of the other knowledgable users) can take a look for you.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...