Answers.Body: <p>Value is present on AJAX form. Solution:</p>
  
  <pre><code>            function webform_membership_civicrm_buildForm($formName, &amp;$form) {
  
                  if (($formName == 'CRM_Activity_Form_Activity') || ($formName == 'CRM_Custom_Form_CustomDataByType')) {
  
                      if (in_array($form-&gt;getAction(), array(0,1,2))) {
  
                          //$defaults['custom_8_-1'] = 710;
                          //$form-&gt;_groupTree[4]['fields'][8]['element_value'] = 111;
  
                          foreach ($form-&gt;_elements as $index =&gt; &amp;$element) {
                              if ($element-&gt;_attributes['data-crm-custom'] == 'Points_for_this:Points') {
                                  $element-&gt;_attributes['value'] = 115; // Set custom value here
                              }
                          }
                      }
                  }
              }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-18T12:34:53.817Z
Answers.LastActivityDate: 2017-04-18T13:02:43.643Z
Answers.LastEditDate: 2017-04-18T13:02:43.643Z
Answers.UserDisplayName: Adam
Body: <p>What is the correct way to set a default value on rendering a form for custom values. The code below works for native fields, but not for custom fields (presumably because they are looked up through API calls.</p>
  
  <p>This is the code I have so far:</p>
  
  <pre><code>            function webform_membership_civicrm_buildForm($formName, &amp;$form) {
                  if ($formName == 'CRM_Activity_Form_Activity') {
                      if (in_array($form-&gt;getAction(), array(CRM_Core_Action::ADD, CRM_Core_Action::UPDATE))) {
                          $defaults['custom_8_-1'] = 710;
                          $form-&gt;setDefaults($defaults);
  
                      }
                  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-04-18T07:45:01.447Z
Comments.Text: I assume you have checked in the _elementIndex array of the form object that you have the correct name of the form field?
Comments.UserDisplayName: ErikH - CiviCooP
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-04-18T12:01:58.743Z
Comments.Text: Yes unfortunately, no _1 or -1 or similar value in the array.
Comments.UserDisplayName: Adam
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-04-18T12:32:27.500Z
Comments.Text: The ajax call is being made to: /index.php?q=civicrm/custom&type=Activity&subType=58&entityID=2&qf=xxxx&cgcount=1&snippet=json which does contain the HTML value.
Comments.UserDisplayName: Adam
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-04-18T14:35:16.707Z
Comments.Text: Sorry - not sure I understand exactly what you're trying to do but I often set values in a form based on conditionals (and webform validation) - if Membership is of type x -> then set some other webform element to =y - not sure I've tried this specifically for custom data values - but can't see how that would be any different?
Comments.UserDisplayName: KarinG - Semper IT
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-04-18T21:15:37.057Z
Comments.Text: I am trying to set default values, specifically within CiviCRM. i.e. Set a custom default value if none has been set within CiviCRM for that entity. For instance, staff create activities (internally) for clients as part of the default workflow, which customers can also do via the web form. The code below sets a custom default value on create or edit of an existing activity within CiviCRM, where a value is not set. I have successfully been able to alter the web forms (front end) without issue.
Comments.UserDisplayName: Adam
Tags: <webform-civicrm><hooks>
Title: Problem with setting the default for a custom value in the buildForm hook
ThreadText: # Problem with setting the default for a custom value in the buildForm hook
  
  (Asked by: username_0 on 2017-04-18)
  
  What is the correct way to set a default value on rendering a form for custom values. The code below works for native fields, but not for custom fields (presumably because they are looked up through API calls.
  
  This is the code I have so far:
  
  ```
              function webform_membership_civicrm_buildForm($formName, &$form) {
                  if ($formName == 'CRM_Activity_Form_Activity') {
                      if (in_array($form->getAction(), array(CRM_Core_Action::ADD, CRM_Core_Action::UPDATE))) {
                          $defaults['custom_8_-1'] = 710;
                          $form->setDefaults($defaults);
  
                      }
                  }
  
  ```
  
  **Tags:** webform-civicrm, hooks
  
  > **Comments:**
  > **username_1**: I assume you have checked in the _elementIndex array of the form object that you have the correct name of the form field?  
  > **username_0**: Yes unfortunately, no _1 or -1 or similar value in the array.  
  > **username_0**: The ajax call is being made to: /index.php?q=civicrm/custom&type=Activity&subType=58&entityID=2&qf=xxxx&cgcount=1&snippet=json which does contain the HTML value.  
  > **username_2**: Sorry - not sure I understand exactly what you're trying to do but I often set values in a form based on conditionals (and webform validation) - if Membership is of type x -> then set some other webform element to =y - not sure I've tried this specifically for custom data values - but can't see how that would be any different?  
  > **username_0**: I am trying to set default values, specifically within CiviCRM. i.e. Set a custom default value if none has been set within CiviCRM for that entity. For instance, staff create activities (internally) for clients as part of the default workflow, which customers can also do via the web form. The code below sets a custom default value on create or edit of an existing activity within CiviCRM, where a value is not set. I have successfully been able to alter the web forms (front end) without issue.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-04-18. Score: 2
  
  Value is present on AJAX form. Solution:
  
  ```
              function webform_membership_civicrm_buildForm($formName, &$form) {
  
                  if (($formName == 'CRM_Activity_Form_Activity') || ($formName == 'CRM_Custom_Form_CustomDataByType')) {
  
                      if (in_array($form->getAction(), array(0,1,2))) {
  
                          //$defaults['custom_8_-1'] = 710;
                          //$form->_groupTree[4]['fields'][8]['element_value'] = 111;
  
                          foreach ($form->_elements as $index => &$element) {
                              if ($element->_attributes['data-crm-custom'] == 'Points_for_this:Points') {
                                  $element->_attributes['value'] = 115; // Set custom value here
                              }
                          }
                      }
                  }
              }
  
  ```
  
  
  ---
  
  