Answers.Body: <p>The error comes from <code>{capture assign='note_id'}{contribution.contribution_id}{/capture}</code>. When grouping, if you have more than one contribution the output will be ie. <code>1, 2, 3, 4 ...</code>, try printing <code>{$note_id}</code> when grouping and you'll see what I mean, passing <code>1, 2, 3, 4</code> as contribution_id to the API call the response is <code>"is_error": 1</code></p>
  
  <p><strong>Edit:</strong> so having a look at <code>CRM/Contribute/Form/Task/PDFLetterCommon.php</code>, I believe that's the correct class, there´s a <code>$contributions</code> variable which holds the contributions, so if you loop through it, the above could look like this:</p>
  
  <pre><code>{foreach from=$contributions item=contribution}
  {crmAPI var='result' entity='Note' action='get' entity_table="civicrm_contribution" entity_id=$contribution.id}
   {assign var='note_count' value=$result.count}
    Contribution note: {if !$note_count}None
    {elseif $note_count}
      {foreach from=$result.values item=note}
        {$note.note}
      {/foreach}
    {/if} 
   | Non deductible amount: {if $contribution.non_deductible_amount gt 0}${$contribution.non_deductible_amount}{else}N/A{/if}
  &lt;br/&gt;
  {/foreach}
  </code></pre>
  
  <p>Now this <strong>works when grouping</strong> but it won't when you are not grouping, so my guess is that you'll have to create two ThankYou templates, one for grouping and one for <em><strong>no grouping</strong></em>.</p>
  
  <p>Also important to mention this is <strong>smarty</strong> they are not custom tokens. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-27T20:41:33.633Z
Answers.Text: My apologies, my original post I referenced both smarty and custom tokens. (I also tried writing a custom token for the note variable but it also failed to show a variable for multiple contributions). I since edited it and added code so I pulled out the mention of custom tokens for an easier read. I printed {$note_id} and it printed out the contribution IDs.
Answers.UserDisplayName: RayWright
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-27T22:48:01.760Z
Answers.Text: Updated the answer, hope that helps.
Answers.UserDisplayName: Andrei
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-27T23:52:44.163Z
Answers.Text: you are my hero! Totally works didn't realize I could put the foreach around the API.....mind blown!
Answers.UserDisplayName: RayWright
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-27T23:56:47.550Z
Answers.Text: Glad it helped!
Answers.UserDisplayName: Andrei
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-27T18:35:46.603Z
Answers.LastActivityDate: 2016-01-27T22:41:57.957Z
Answers.LastEditDate: 2016-01-27T22:41:57.957Z
Answers.UserDisplayName: Andrei
Body: <p>Being able to group Thank You Letters by contact is a great feature for both everyday receipts (we have a company that will donate via multiple smaller donations at once) and end of the year receipts.</p>
  
  <p>But I'm running into a problem, if I use an if statement around a contribution token, the token no longer loops for each contribution printed. For example: </p>
  
  <pre><code>{crmAPI var='contresult' entity='Contribution' action='get' sequential=0 id=$note_id}
   {foreach from=$contresult.values item=contribution}
    {if $contribution.non_deductible_amount gt 0}${$contribution.non_deductible_amount}{else}N/A{/if}
   {/foreach}
  </code></pre>
  
  <p>The function doesn't display anything when I 'group by' (If I run a thank you letter for a single contribution it does work.)</p>
  
  <p>In this example: </p>
  
  <pre><code>{capture assign='note_id'}{contribution.contribution_id}{/capture}
  {crmAPI var='result' entity='Note' action='get' sequential=0 entity_table="civicrm_contribution" entity_id=$note_id}
   {assign var='note_count' value=$result.count}
    {if !$note_count}None{elseif $note_count}
     {foreach from=$result.values item=note}{$note.note}{/foreach}
    {/if} 
  </code></pre>
  
  <p>It only returns "None" once, when the contributions all have notes.</p>
  
  <p>How can I make the if statement run for each of the contributions selected for the thank you letter?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-01-26T23:33:52.763Z
Comments.Text: Can you edit your question and post the code, I guess really the HTML, you're using?   That way folks can see the logic, loops, foreach, etc.
Comments.UserDisplayName: Paul-Tahoe
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-01-27T16:36:46.423Z
Comments.Text: @Paul-Tahoe Done!
Comments.UserDisplayName: RayWright
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-01-27T18:25:34.570Z
Comments.Text: Have you looked at this https://github.com/civicrm/civicrm-user-guide/blob/master/docs/contributions/manual-receipts-and-thank-yous.md ?  Near the bottom of the page is some html/smarty's code.  It isn't as complex as yours though it does fill out a table with all the different donations for the period.  Also, it appears that the thank you letter already has an array that most likely includes the non_deductible_amount so you might be able to do like the example and stick you if then in their inner loop.
Comments.UserDisplayName: Paul-Tahoe
Tags: <tokens><receipts>
Title: How can I use custom tokens when grouping Thank You letters
ThreadText: # How can I use custom tokens when grouping Thank You letters
  
  (Asked by: username_0 on 2016-01-26)
  
  Being able to group Thank You Letters by contact is a great feature for both everyday receipts (we have a company that will donate via multiple smaller donations at once) and end of the year receipts.
  
  But I'm running into a problem, if I use an if statement around a contribution token, the token no longer loops for each contribution printed. For example:
  
  ```
  {crmAPI var='contresult' entity='Contribution' action='get' sequential=0 id=$note_id}
   {foreach from=$contresult.values item=contribution}
    {if $contribution.non_deductible_amount gt 0}${$contribution.non_deductible_amount}{else}N/A{/if}
   {/foreach}
  
  ```
  
  The function doesn't display anything when I 'group by' (If I run a thank you letter for a single contribution it does work.)
  
  In this example:
  
  ```
  {capture assign='note_id'}{contribution.contribution_id}{/capture}
  {crmAPI var='result' entity='Note' action='get' sequential=0 entity_table="civicrm_contribution" entity_id=$note_id}
   {assign var='note_count' value=$result.count}
    {if !$note_count}None{elseif $note_count}
     {foreach from=$result.values item=note}{$note.note}{/foreach}
    {/if} 
  
  ```
  
  It only returns "None" once, when the contributions all have notes.
  
  How can I make the if statement run for each of the contributions selected for the thank you letter?
  
  **Tags:** tokens, receipts
  
  > **Comments:**
  > **username_1**: Can you edit your question and post the code, I guess really the HTML, you're using?   That way folks can see the logic, loops, foreach, etc.  
  > **username_0**: @username_1 Done!  
  > **username_1**: Have you looked at this https://github.com/civicrm/civicrm-user-guide/blob/master/docs/contributions/manual-receipts-and-thank-yous.md ?  Near the bottom of the page is some html/smarty's code.  It isn't as complex as yours though it does fill out a table with all the different donations for the period.  Also, it appears that the thank you letter already has an array that most likely includes the non_deductible_amount so you might be able to do like the example and stick you if then in their inner loop.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-01-27. Score: 2 | Accepted answer
  
  The error comes from `{capture assign='note_id'}{contribution.contribution_id}{/capture}`. When grouping, if you have more than one contribution the output will be ie. `1, 2, 3, 4 ...`, try printing `{$note_id}` when grouping and you'll see what I mean, passing `1, 2, 3, 4` as contribution\_id to the API call the response is `"is_error": 1`
  
  **Edit:** so having a look at `CRM/Contribute/Form/Task/PDFLetterCommon.php`, I believe that's the correct class, there´s a `$contributions` variable which holds the contributions, so if you loop through it, the above could look like this:
  
  ```
  {foreach from=$contributions item=contribution}
  {crmAPI var='result' entity='Note' action='get' entity_table="civicrm_contribution" entity_id=$contribution.id}
   {assign var='note_count' value=$result.count}
    Contribution note: {if !$note_count}None
    {elseif $note_count}
      {foreach from=$result.values item=note}
        {$note.note}
      {/foreach}
    {/if} 
   | Non deductible amount: {if $contribution.non_deductible_amount gt 0}${$contribution.non_deductible_amount}{else}N/A{/if}
  <br/>
  {/foreach}
  
  ```
  
  Now this **works when grouping** but it won't when you are not grouping, so my guess is that you'll have to create two ThankYou templates, one for grouping and one for ***no grouping***.
  
  Also important to mention this is **smarty** they are not custom tokens.
  
  > **Comments:**
  > **username_0**: My apologies, my original post I referenced both smarty and custom tokens. (I also tried writing a custom token for the note variable but it also failed to show a variable for multiple contributions). I since edited it and added code so I pulled out the mention of custom tokens for an easier read. I printed {$note_id} and it printed out the contribution IDs.  
  > **username_2**: Updated the answer, hope that helps.  
  > **username_0**: you are my hero! Totally works didn't realize I could put the foreach around the API.....mind blown!  
  > **username_2**: Glad it helped!  
  
  ---
  
  