Answers.Body: <p>Moe's answer is technically workable - but oof ouch my performance!</p>
  
  <p>Instead, something like this:</p>
  
  
  
  <pre><code>function getElementNameFromFieldName($fieldName, $elementIndex) {
    // This function could be further optimized so it's only called once, but meh.
    $elementNames = array_keys($elementIndex);
    foreach ($fieldNames as $fieldName) {
      if (strpos($elementName, $fieldName) === 0) {
        return $elementName;
      }
    }
  }
  
  if ( $formName == 'CRM_Contact_Form_CustomData' || $form == CRM_Core_Action::ADD) {
    $custom198ElementName = getElementNameFromFieldName('custom_198', $elementIndex);
    $custom197ElementName = getElementNameFromFieldName('custom_197', $elementIndex);
    $custom160ElementName = getElementNameFromFieldName('custom_160', $elementIndex);
  
    $form-&gt;updateElementAttr($custom198ElementName, ['readonly']);
    $form-&gt;updateElementAttr($custom197ElementName, ['readonly']);
    $form-&gt;updateElementAttr($custom160ElementName, ['readonly']);
  }
  
  if ($form == CRM_Core_Action::ADD) {
    $customid = rand (1,10000);
    $currentDate = date('d/m/Y');
  
    $defaults[$custom160ElementName] = $customid;
    $defaults[$custom197ElementName] = $currentDate;
    $defaults[$custom198ElementName] = $currentDate;
    $form-&gt;setDefaults($defaults);
  }
  </code></pre>
  
  <p>Additionally -  your code (and mine) as written will set defaults on EVERY form's <code>ADD</code> action.  You probably want to limit it to those that have these custom fields.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-28T11:10:45.180Z
Answers.Text: Do i use the buildform hook or a different hook?
Answers.UserDisplayName: Moe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-28T12:07:20.410Z
Answers.Text: buildForm, yes.  The helper function goes outside the hook of course.
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-28T13:38:05.617Z
Answers.Text: Thanks for your help but have got back these two error codes:
  
  Notice: Object of class CRM_Activity_Form_ActivityLinks could not be converted to int in dfi_civicrm_buildForm() 
  Error: Using $this when not in object context in getElementNameFromFieldName()
Answers.UserDisplayName: Moe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-28T14:00:51.387Z
Answers.Text: @Moe Sorry - I didn't test this code, it's intended more as a guideline than a copy-paste :)  However, I'll modify the code to handle the error.  The notice isn't being generated from this code (or even this hook)
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-28T14:18:30.773Z
Answers.Text: Thanks for all your help, really appreciated!
Answers.UserDisplayName: Moe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-24T15:43:15.917Z
Answers.LastActivityDate: 2019-10-28T14:01:46.237Z
Answers.LastEditDate: 2019-10-28T14:01:46.237Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>With the help of JaapJansma-CiviCooP</p>
  
  <p>I was able to make this work using the below code:</p>
  
  <pre><code>function dfi_civicrm_buildForm($formName, &amp;$form) {
      if ( $formName == 'CRM_Contact_Form_CustomData' ) {
          for ($i = 1; $i &lt; 10000; $i++) {
              $form-&gt;updateElementAttr('custom_198_'. $i,array('readonly'));
              $form-&gt;updateElementAttr('custom_197_'. $i,array('readonly'));
              $form-&gt;updateElementAttr('custom_160_'. $i,array('readonly'));
          }
       if ($form == CRM_Core_Action::ADD) {
          $customid = rand (1,10000);
          $currentDate = date('d/m/Y');
          for ($i = 1; $i &lt; 10000; $i++) {
              $defaults['custom_160_-' . $i] = $customid;
              $defaults['custom_197_-' . $i] = $currentDate;
              $defaults['custom_198_-' . $i] = $currentDate;
              $form-&gt;updateElementAttr('custom_198_-'. $i,array('readonly'));
              $form-&gt;updateElementAttr('custom_197_-'. $i,array('readonly'));
              $form-&gt;updateElementAttr('custom_160_-'. $i,array('readonly'));
          }
          $form-&gt;setDefaults($defaults);
         } 
      }
  }
  </code></pre>
  
  <p>Thanks</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-23T16:14:29.533Z
Answers.LastActivityDate: 2019-10-23T16:14:29.533Z
Answers.UserDisplayName: Moe
Body: <p>I am trying to set custom fields in a Custom Group. Here is my code:</p>
  
  <pre><code>function dfi_civicrm_buildForm($formName, &amp;$form) {
    if ($formName == 'CustomData') {
      if ($form-&gt;getAction() == CRM_Core_Action::ADD) {
        echo 'Hello World';
        $defaults['custom_160_-5'] = mt_rand();
        $defaults['custom_70_-5'] = '23/10/2019';
        $form-&gt;setDefaults($defaults);
      }
    }
  }
  </code></pre>
  
  <p>I have added it as an extension. It is not working even the Hello World isn't showing.</p>
  
  <p>Thanks</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-23T11:37:39.673Z
Comments.Text: Try -1 instead of -5
Comments.UserDisplayName: Jaap Jansma - CiviCooP
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-23T11:57:23.513Z
Comments.Text: @JaapJansma-CiviCooP - Thank you for trying to help me but unfortunately no, if i put -1 or even remove the _-5 or _-1 it still isn't working.
Comments.UserDisplayName: Moe
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-23T13:14:09.183Z
Comments.Text: The formName probably isn't CustomData, it's usually something like CRM_XX_Form_XX. You can use a step-debugger or log it to the ConfigAndLog folder using CRM_Core_Error::debug_log_message($formName).
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-23T14:29:19.287Z
Comments.Text: @JaapJansma-CiviCooP - You were right, the formname was incorrect the issue now is after populating the form doesn't save the values.
Comments.UserDisplayName: Moe
Tags: <custom-fields>
Title: Set Default Value for Custom Field
ThreadText: # Set Default Value for Custom Field
  
  (Asked by: username_0 on 2019-10-23)
  
  I am trying to set custom fields in a Custom Group. Here is my code:
  
  ```
  function dfi_civicrm_buildForm($formName, &$form) {
    if ($formName == 'CustomData') {
      if ($form->getAction() == CRM_Core_Action::ADD) {
        echo 'Hello World';
        $defaults['custom_160_-5'] = mt_rand();
        $defaults['custom_70_-5'] = '23/10/2019';
        $form->setDefaults($defaults);
      }
    }
  }
  
  ```
  
  I have added it as an extension. It is not working even the Hello World isn't showing.
  
  Thanks
  
  **Tags:** custom-fields
  
  > **Comments:**
  > **username_1**: Try -1 instead of -5  
  > **username_0**: @JaapJansma-username_1 - Thank you for trying to help me but unfortunately no, if i put -1 or even remove the _-5 or _-1 it still isn't working.  
  > **username_2**: The formName probably isn't CustomData, it's usually something like CRM_XX_Form_XX. You can use a step-debugger or log it to the ConfigAndLog folder using CRM_Core_Error::debug_log_message($formName).  
  > **username_0**: @JaapJansma-username_1 - You were right, the formname was incorrect the issue now is after populating the form doesn't save the values.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2019-10-24. Score: 2 | Accepted answer
  
  Moe's answer is technically workable - but oof ouch my performance!
  
  Instead, something like this:
  
  ```
  function getElementNameFromFieldName($fieldName, $elementIndex) {
    // This function could be further optimized so it's only called once, but meh.
    $elementNames = array_keys($elementIndex);
    foreach ($fieldNames as $fieldName) {
      if (strpos($elementName, $fieldName) === 0) {
        return $elementName;
      }
    }
  }
  
  if ( $formName == 'CRM_Contact_Form_CustomData' || $form == CRM_Core_Action::ADD) {
    $custom198ElementName = getElementNameFromFieldName('custom_198', $elementIndex);
    $custom197ElementName = getElementNameFromFieldName('custom_197', $elementIndex);
    $custom160ElementName = getElementNameFromFieldName('custom_160', $elementIndex);
  
    $form->updateElementAttr($custom198ElementName, ['readonly']);
    $form->updateElementAttr($custom197ElementName, ['readonly']);
    $form->updateElementAttr($custom160ElementName, ['readonly']);
  }
  
  if ($form == CRM_Core_Action::ADD) {
    $customid = rand (1,10000);
    $currentDate = date('d/m/Y');
  
    $defaults[$custom160ElementName] = $customid;
    $defaults[$custom197ElementName] = $currentDate;
    $defaults[$custom198ElementName] = $currentDate;
    $form->setDefaults($defaults);
  }
  
  ```
  
  Additionally - your code (and mine) as written will set defaults on EVERY form's `ADD` action. You probably want to limit it to those that have these custom fields.
  
  > **Comments:**
  > **username_0**: Do i use the buildform hook or a different hook?  
  > **username_3**: buildForm, yes.  The helper function goes outside the hook of course.  
  > **username_0**: Thanks for your help but have got back these two error codes:  Notice: Object of class CRM_Activity_Form_ActivityLinks could not be converted to int in dfi_civicrm_buildForm()  Error: Using $this when not in object context in getElementNameFromFieldName()  
  > **username_3**: @username_0 Sorry - I didn't test this code, it's intended more as a guideline than a copy-paste :)  However, I'll modify the code to handle the error.  The notice isn't being generated from this code (or even this hook)  
  > **username_0**: Thanks for all your help, really appreciated!  
  
  ---
  
  ### Answer by username_0 on 2019-10-23. Score: 1
  
  With the help of JaapJansma-username_1
  
  I was able to make this work using the below code:
  
  ```
  function dfi_civicrm_buildForm($formName, &$form) {
      if ( $formName == 'CRM_Contact_Form_CustomData' ) {
          for ($i = 1; $i < 10000; $i++) {
              $form->updateElementAttr('custom_198_'. $i,array('readonly'));
              $form->updateElementAttr('custom_197_'. $i,array('readonly'));
              $form->updateElementAttr('custom_160_'. $i,array('readonly'));
          }
       if ($form == CRM_Core_Action::ADD) {
          $customid = rand (1,10000);
          $currentDate = date('d/m/Y');
          for ($i = 1; $i < 10000; $i++) {
              $defaults['custom_160_-' . $i] = $customid;
              $defaults['custom_197_-' . $i] = $currentDate;
              $defaults['custom_198_-' . $i] = $currentDate;
              $form->updateElementAttr('custom_198_-'. $i,array('readonly'));
              $form->updateElementAttr('custom_197_-'. $i,array('readonly'));
              $form->updateElementAttr('custom_160_-'. $i,array('readonly'));
          }
          $form->setDefaults($defaults);
         } 
      }
  }
  
  ```
  
  Thanks
  
  
  ---
  
  