3

I have some code in a custom module (in the civicrm_pre hook) that sets a custom field value, using the function CRM_Core_BAO_CustomValueTable::setValues

Here's a print_r of the parameters it is passed in an example case:

(
    [entityID] => 1202
    [custom_133] => foo
)

The field is not being set. Before upgrading to 4.6, it was working. There is definitely still a custom field with id 133 on the entity. Here's the documentation - is it outdated now, or should it still work?

http://wiki.civicrm.org/confluence/display/CRMDOC/Setting+and+getting+custom+field+values+from+within+hooks

Edit: The output of CRM_Core_BAO_CustomValueTable::setValues is:

(
    [is_error] => 0
    [result] => 1
)

So it thinks it has worked, but it hasn't. The new value is not written to the database table.

2
  • 1
    That's the input, what about output? Is it returning an error code, etc?
    – Coleman
    May 7, 2015 at 15:19
  • Edited to add return value
    – naomi
    May 8, 2015 at 14:40

1 Answer 1

3

I don't believe that the document you referenced is current best practice - it should probably be rewritten! Using the BAO rather than the API to set values greatly increases the chance that an upgrade will break someone's custom extension.

You can troubleshoot the BAO code. There's certainly something that could be done to make it work (assuming the issue is the code, and not that the hook is no longer firing). However, I'd suggest rewriting the BAO call as an API call instead. Something like this (assuming the custom field is attached to a Contact entity, otherwise change appropriately):

$params = array(
  'version' => 3,
  'custom_133' => 'foo',
  'id' => 1202,
);
$result = civicrm_api('Contact', 'create', $params);
3
  • Thanks. I've tried this: $params = array( 'version' => '3', 'custom_133' => 'foo', 'entity_id' => $objectId, ); $result = civicrm_api('CustomValue', 'create', $params); and it returns an array suggesting that it was successful, but still does not update the field
    – naomi
    May 8, 2015 at 14:33
  • I've also tried using "custom_" and then the group name and field name as it suggests (very confusingly) in point 4 of this: wiki.civicrm.org/confluence/display/CRMDOC/…
    – naomi
    May 8, 2015 at 14:39
  • @naomi - the code I used above is meant to be run against the API for the entity that the custom value belongs to, not the CustomValue API. There's undoubtedly a way to do this using the CustomValue API as well, but this example was pulled from working code I have which uses this approach. May 8, 2015 at 16:38

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.