4

Using the Explorer, I built up the smarty code I need for a template to pull in address information on a contact for use in an email:

{crmAPI var='result' entity='Address' action='get' contact_id=203}
{foreach from=$result.values item=address}
  {$address.some_field}
{/foreach}

and it produces the desired output in the Explorer when executed.

In an email though, the expected output never renders, and when I add the smarty expression {result|@debug_print_var}, I get null for the value of the variable in the message.

The fact that I'm getting null suggests to me that smarty is in fact running, but I can't for the life of me figure out why it can't seem to make an API call. Thoughts?

Thanks for the help!

N.B.: I'm running CiviCRM 5.17.4 on Drupal 7.67 and Ubuntu 16.04.

Update

With the following email code:

{capture assign=contact_id}{contact.contact_id}{/capture}

id: {contact.contact_id}

smarty id: {$contact_id}

{crmAPI var='result' entity='Contact' action='get' id=203}

{$result|@debug_print_var}

This is the email that renders when sending to contact # 203:

id: 203

smarty id: 203

null
2
  • The above code looks like it is pasted from the API explorer. Can you also paste the code that you've entered in the message template? is the contact id hardcoded in it too? Commented Sep 23, 2019 at 10:36
  • I used exactly the same code in the message to reduce the risk that I had messed something up. The contact id is hardcoded in this test (though it will eventually be dynamic).
    – pyfi
    Commented Sep 23, 2019 at 11:58

1 Answer 1

4

So, for posterity's sake, I wanted to share the answer I figured out.

When I looked at the Source view of the email in CiviCRM, I realized that the code had pasted as


<p>id:&nbsp;{contact.contact_id}</p>

<p>smarty id: {$contact_id}</p>

<p>{crmAPI var=&#39;result&#39; entity=&#39;Contact&#39; action=&#39;get&#39; id=203}</p>

<p>{$result|@debug_print_var}</p>

instead of

{capture assign=contact_id}{contact.contact_id}{/capture}

id: {contact.contact_id}

smarty id: {$contact_id}

{crmAPI var='result' entity='Contact' action='get' id=203}

{$result|@debug_print_var}

When I composed the code correctly in the Source view rather than the HTML view, everything worked as expected!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.