Answers.Body: <p>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.</p>
  
  <p>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):</p>
  
  <pre><code>$params = array(
    'version' =&gt; 3,
    'custom_133' =&gt; 'foo',
    'id' =&gt; 1202,
  );
  $result = civicrm_api('Contact', 'create', $params);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-08T14:33:23.767Z
Answers.Text: 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
Answers.UserDisplayName: naomi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-08T14:39:01.980Z
Answers.Text: I've also tried using "custom_" and then the group name and field name as it suggests (very confusingly) in point 4 of this: http://wiki.civicrm.org/confluence/display/CRMDOC/Using+Custom+Data+with+the+API
Answers.UserDisplayName: naomi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-08T16:38:21.520Z
Answers.Text: @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.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-07T15:41:20.383Z
Answers.LastActivityDate: 2015-05-07T15:41:20.383Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>I have some code in a  custom module (in the civicrm_pre hook) that sets a custom field value, using the function <code>CRM_Core_BAO_CustomValueTable::setValues</code></p>
  
  <p>Here's a print_r of the parameters it is passed in an example case:</p>
  
  <pre><code>(
      [entityID] =&gt; 1202
      [custom_133] =&gt; foo
  )
  </code></pre>
  
  <p>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?</p>
  
  <p><a href="http://wiki.civicrm.org/confluence/display/CRMDOC/Setting+and+getting+custom+field+values+from+within+hooks" rel="nofollow">http://wiki.civicrm.org/confluence/display/CRMDOC/Setting+and+getting+custom+field+values+from+within+hooks</a></p>
  
  <p>Edit: The output of <code>CRM_Core_BAO_CustomValueTable::setValues</code> is:</p>
  
  <pre><code>(
      [is_error] =&gt; 0
      [result] =&gt; 1
  )
  </code></pre>
  
  <p>So it thinks it has worked, but it hasn't. The new value is not written to the database table.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-07T15:19:27.687Z
Comments.Text: That's the input, what about output? Is it returning an error code, etc?
Comments.UserDisplayName: Coleman
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-08T14:40:09.290Z
Comments.Text: Edited to add return value
Comments.UserDisplayName: naomi
Tags: <hooks><4.6.2><custom-fields>
Title: Custom code that sets custom field value stopped working in 4.6
ThreadText: # Custom code that sets custom field value stopped working in 4.6
  
  (Asked by: username_0 on 2015-05-07)
  
  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.
  
  **Tags:** hooks, 4.6.2, custom-fields
  
  > **Comments:**
  > **username_1**: That's the input, what about output? Is it returning an error code, etc?  
  > **username_0**: Edited to add return value  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-05-07. Score: 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);
  
  > **Comments:**
  > **username_0**: 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  
  > **username_0**: I've also tried using "custom_" and then the group name and field name as it suggests (very confusingly) in point 4 of this: http://wiki.civicrm.org/confluence/display/CRMDOC/Using+Custom+Data+with+the+API  
  > **username_2**: @username_0 - 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.  
  
  ---
  
  