Answers.Body: <pre><code>/**
   * Implements hook_civicrm_buildForm().
   *
   * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
   *
   */
  function extensionname_civicrm_buildForm($formName, $form) {
    if ('CRM_Custom_Form_CustomDataByType' == $formName
      // type == cuctom group belongs to which type i.e Contact, Event, Activity...
      &amp;&amp; in_array($form-&gt;getVar('_type'), ['Contact'])
    ) {
      foreach ($form-&gt;getVar('_groupTree') as $customGroup) {
        // custom group name in which the field you want to set default to.
        if ($customGroup['name'] == 'custom_group_name') {
          foreach ($customGroup['fields'] as $field) {
            // custom field name you want to set default to.
            if ($field['name'] == 'custom_field_name_type_date') {
              $defaults = [
                $field['element_name'] =&gt; date('Y', strtotime('-3 years', strtotime(date('Ymd'))))
              ];
              $form-&gt;setDefaults($defaults);
              break;
            }
          }
          break;
        }
      }
    }
  }
  
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-19T18:00:13.877Z
Answers.Text: Thanks, both ```foreach``` are running, but no default value is set...
Answers.UserDisplayName: Pál Rudan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-19T18:16:42.323Z
Answers.Text: what type of custom field is it?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-20T10:26:56.023Z
Answers.Text: I created a custom group for Activities, in this group is a single field, which is a Date field, the format is "Year only" (%Y), no time is show, it's mandatory, searchable with range.
Answers.UserDisplayName: Pál Rudan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-21T10:53:33.283Z
Answers.Text: I have updated the answer
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-16T15:26:14.553Z
Answers.LastActivityDate: 2022-02-21T10:53:19.637Z
Answers.LastEditDate: 2022-02-21T10:53:19.637Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>My CiviCRM 5.56.1 runs on Drupal 7.</p>
  <p>I try to set the default value for a custom field which is used for an Action.</p>
  <p>I created the field in Admin -&gt; Customized Data &amp; Screen -&gt; Custom Fields. The field <code>custom_76_-1</code> is shown in the form, and it works, I can change the current value. This is a date field showing a year-value only, and I'd like to set it's default value from current year to 3 years before (eg. in this year from 2022 to 2019). On the UI I can set a fix date, not a relative date.</p>
  <p>With Javascript / jQuery I can change the shown value of the year, but not the default value in the form. I tried to set the default value in the form with the code from this <a href="https://civicrm.stackexchange.com/a/33474/5877">https://civicrm.stackexchange.com/a/33474/5877</a> answer, but it doesn't works for me -- I mean the default value didn't changed. As I understood, custom fields are created from an AJAX call's response, and the <code>buildForm()</code> function runs earlier as the AJAX call, which creates the custom field, so the default value can't be set (is this true or do / understand I something wrong?).</p>
  <p>Please advice me how can I set the field's default value.</p>
  
Tags: <custom-fields><dates>
Title: Default value for custom date field
ThreadText: # Default value for custom date field
  
  (Asked by: username_0 on 2022-02-16)
  
  My CiviCRM 5.56.1 runs on Drupal 7.
  
  I try to set the default value for a custom field which is used for an Action.
  
  I created the field in Admin -> Customized Data & Screen -> Custom Fields. The field `custom_76_-1` is shown in the form, and it works, I can change the current value. This is a date field showing a year-value only, and I'd like to set it's default value from current year to 3 years before (eg. in this year from 2022 to 2019). On the UI I can set a fix date, not a relative date.
  
  With Javascript / jQuery I can change the shown value of the year, but not the default value in the form. I tried to set the default value in the form with the code from this <https://civicrm.stackexchange.com/a/33474/5877> answer, but it doesn't works for me -- I mean the default value didn't changed. As I understood, custom fields are created from an AJAX call's response, and the `buildForm()` function runs earlier as the AJAX call, which creates the custom field, so the default value can't be set (is this true or do / understand I something wrong?).
  
  Please advice me how can I set the field's default value.
  
  **Tags:** custom-fields, dates
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-02-16. Score: 3
  
  ```
  /**
   * Implements hook_civicrm_buildForm().
   *
   * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
   *
   */
  function extensionname_civicrm_buildForm($formName, $form) {
    if ('CRM_Custom_Form_CustomDataByType' == $formName
      // type == cuctom group belongs to which type i.e Contact, Event, Activity...
      && in_array($form->getVar('_type'), ['Contact'])
    ) {
      foreach ($form->getVar('_groupTree') as $customGroup) {
        // custom group name in which the field you want to set default to.
        if ($customGroup['name'] == 'custom_group_name') {
          foreach ($customGroup['fields'] as $field) {
            // custom field name you want to set default to.
            if ($field['name'] == 'custom_field_name_type_date') {
              $defaults = [
                $field['element_name'] => date('Y', strtotime('-3 years', strtotime(date('Ymd'))))
              ];
              $form->setDefaults($defaults);
              break;
            }
          }
          break;
        }
      }
    }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, both ```foreach``` are running, but no default value is set...  
  > **username_1**: what type of custom field is it?  
  > **username_0**: I created a custom group for Activities, in this group is a single field, which is a Date field, the format is "Year only" (%Y), no time is show, it's mandatory, searchable with range.  
  > **username_1**: I have updated the answer  
  
  ---
  
  