CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Trouble getting custom field value in _post hook, $op = 'create'
Pages: [1]

Author Topic: Trouble getting custom field value in _post hook, $op = 'create'  (Read 1129 times)

Andy Laken

  • I’m new here
  • *
  • Posts: 27
  • Karma: 3
Trouble getting custom field value in _post hook, $op = 'create'
February 22, 2011, 11:53:32 am
Use case:
  • User creates a new contribution, and designates an organization using a contact reference custom field in the Contribution profile
  • Upon save, the contribution's contact_id is switched from the user who made it the contact_id of the organization designated in the contact_reference field
I'm using the _post hook for this, as _pre didn't seem to cut it (couldn't get the contribution entity id in the _pre hook for some reason).

I used the code here as a model for getting the custom field values: http://wiki.civicrm.org/confluence/display/CRMDOC33/CiviCRM+hook+specification#CiviCRMhookspecification-Noteonsettingandgettingcustomfieldvaluesinhooks

In the _post hook I am trying to retrieve the org's contact id from the custom field, and update the contribution to switch its owner to the org's contact id.

Here's the code in the hook: http://laken.pastebin.com/kfd0xr9F

This is all working during the 'edit' operation, but during the 'create' operation (where it's needed) I'm getting this error back from the CRM_Core_BAO_CustomValueTable::getValues call:

     No values found for the specified entity ID and custom field(s).

The $get_params array seems to be properly structured, e.g.:
  • entityID (Integer) 125
  • custom_84 (Integer) 1

Why does this fail on 'create', but work on 'edit'?

This is in 3.2.5, and we are in the middle of upgrading to 3.3.x
« Last Edit: February 22, 2011, 12:00:22 pm by Andy Laken »
Andy Laken
Engineer | CivicActions, LLC
e: andy.laken@civicactions.com | skype: mcantsin | http://twitter.com/alaken

Andy Laken

  • I’m new here
  • *
  • Posts: 27
  • Karma: 3
Re: Trouble getting custom field value in _post hook, $op = 'create'
February 22, 2011, 02:10:24 pm
According to Matt2000, whom I trust:

Quote
Custom fields haven't been stored yet when the 'create' op run on the _pre and_post hooks. There's another hook that is invoked when action is taken on custom fields. You'll need to use that one.

Accordingly I added a note of warning to the relevant doc section on the wiki:

Quote
Note that custom field values may not always be available when you might expect. For instance, you can't retrieve custom field values in the 'create' operation in the _pre and _post hooks, because the custom field values haven't been stored yet. However, you can retrieve values in the 'edit' operation.

http://wiki.civicrm.org/confluence/display/CRMDOC33/CiviCRM+hook+specification#CiviCRMhookspecification-Noteonsettingandgettingcustomfieldvaluesinhooks
Andy Laken
Engineer | CivicActions, LLC
e: andy.laken@civicactions.com | skype: mcantsin | http://twitter.com/alaken

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Trouble getting custom field value in _post hook, $op = 'create'

This forum was archived on 2017-11-26.