Answers.Body: <p>Here's how I would go about this:</p>
  
  <ol>
  <li>Configure a field on the contact. Select "set via PHP" or whatever the option's exact name is.</li>
  <li>Generate an extension using Civix. Remember portability - your extension can generate the field in (1), meaning that if you decided to share the extension or use it on multiple projects that.</li>
  <li>Use the hook as Ramesh said to fill the values in the field.</li>
  </ol>
  
  <p>It sounds like a neat extension. If you want to commission CiviFirst for this I'd be more than happy to help!</p>
  
  <p>JohnFF</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-21T13:49:54.970Z
Answers.Text: How is this CiviFirst commission works exactly? :)
Answers.UserDisplayName: Csaba
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-22T20:38:26.370Z
Answers.Text: Dear Csaba, please email me at john@civifirst.com for details!
Answers.UserDisplayName: JohnFF
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-21T12:19:32.433Z
Answers.LastActivityDate: 2015-08-21T12:19:32.433Z
Answers.UserDisplayName: JohnFF
Answers.Body: <p>you need to create an Extension and use hook_civicrm_custom
  <a href="http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_custom" rel="nofollow">http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_custom</a></p>
  
  <p>Please let me know if you need more help - </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-21T13:23:57.730Z
Answers.Text: Edited my post to show you what I have done so far.
Answers.UserDisplayName: Csaba
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-21T13:24:23.077Z
Answers.Text: give a quick read regarding extension http://wiki.civicrm.org/confluence/display/CRMDOC/Create+an+Extension. and you can create an new extension
Answers.UserDisplayName: Ramesh - ARTECH Consultancy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-21T13:27:56.017Z
Answers.Text: so you have started well  - what is the error message now
Answers.UserDisplayName: Ramesh - ARTECH Consultancy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-21T10:21:15.797Z
Answers.LastActivityDate: 2015-08-21T10:21:15.797Z
Answers.UserDisplayName: Ramesh - ARTECH Consultancy
Body: <p>Currently I am working on a civicrm-wordpress plugin.
  I have two custom fields displayed for a contact:
  - Date of consent
  - Age at recruit</p>
  
  <p>When the user enters the date of consent I would like to calculate the Age at recruit (using the contact's date of birth and the date of consent field). How can I target a custom field and update it with a value when the user clicks on Save?</p>
  
  <p>What I have done so far:
   - Created and enabled a plugin with the following content:</p>
  
  <pre><code>add_action('civicrm_preProcess', 'set_ageatrecruitment', 92,2);
  function set_ageatrecruitment($formName, &amp;$form){
  
          global $recruitmentDate;
          global $contactData;
          global $ageAtRecruit;
          $contact_id=$_GET['cid'];
          $contactDOB = $contactData[values][$contact_id][birth_date];
  
              if($recruitmentDate !='' &amp;&amp; $contactDOB !='' ) {
                  $ageAtRecruitment = getAgeAtRecruitment($contactDOB,$recruitmentDate);
  
              if ($ageAtRecruit != $ageAtRecruitment){
                  updateCustomField($contact_id, "OC-Integrator", "Age at Recruitment", $ageAtRecruitment);}
              }
  
  }
  
  //updates the field, returns number of fields updated
  function updateCustomField($uid,$group_name,$field_name,$value, $overwriteblank=FALSE){
  
      //Don't overwrite existing data if our data value is empty/blank
      //We consider anything that is just whitespace to be "blank"
      if ( ( !isset($value) || strlen(trim($value))==0 ) &amp;&amp; !$overwriteblank) return 0;
  
      $results=civicrm_api("CustomValue","create", array (version =&gt; '3','sequential' =&gt;'1', 'entity_id' =&gt;$uid, "custom_" .$group_name . ":" . $field_name =&gt; $value));
  
      return 1;
  
  }
  </code></pre>
  
  <p>There is another hook called before this which will give value to the global variables.</p>
  
Tags: <php>
Title: Inserting a calculated value
ThreadText: # Inserting a calculated value
  
  (Asked by: username_0 on 2015-08-21)
  
  Currently I am working on a civicrm-wordpress plugin.
  I have two custom fields displayed for a contact:
  - Date of consent
  - Age at recruit
  When the user enters the date of consent I would like to calculate the Age at recruit (using the contact's date of birth and the date of consent field). How can I target a custom field and update it with a value when the user clicks on Save?
  What I have done so far:
   - Created and enabled a plugin with the following content:
  add_action('civicrm_preProcess', 'set_ageatrecruitment', 92,2);
  function set_ageatrecruitment($formName, &$form){
  
          global $recruitmentDate;
          global $contactData;
          global $ageAtRecruit;
          $contact_id=$_GET['cid'];
          $contactDOB = $contactData[values][$contact_id][birth_date];
  
              if($recruitmentDate !='' && $contactDOB !='' ) {
                  $ageAtRecruitment = getAgeAtRecruitment($contactDOB,$recruitmentDate);
  
              if ($ageAtRecruit != $ageAtRecruitment){
                  updateCustomField($contact_id, "OC-Integrator", "Age at Recruitment", $ageAtRecruitment);}
              }
  
  }
  
  //updates the field, returns number of fields updated
  function updateCustomField($uid,$group_name,$field_name,$value, $overwriteblank=FALSE){
  
      //Don't overwrite existing data if our data value is empty/blank
      //We consider anything that is just whitespace to be "blank"
      if ( ( !isset($value) || strlen(trim($value))==0 ) && !$overwriteblank) return 0;
  
      $results=civicrm_api("CustomValue","create", array (version => '3','sequential' =>'1', 'entity_id' =>$uid, "custom_" .$group_name . ":" . $field_name => $value));
  
      return 1;
  
  }
  
  There is another hook called before this which will give value to the global variables.
  
  **Tags:** php
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-08-21. Score: 1
  
  Here's how I would go about this:
  
  1. Configure a field on the contact. Select "set via PHP" or whatever the option's exact name is.
  2. Generate an extension using Civix. Remember portability - your extension can generate the field in (1), meaning that if you decided to share the extension or use it on multiple projects that.
  3. Use the hook as Ramesh said to fill the values in the field.
  
  It sounds like a neat extension. If you want to commission CiviFirst for this I'd be more than happy to help!
  
  username_1
  
  > **Comments:**
  > **username_0**: How is this CiviFirst commission works exactly? :)  
  > **username_1**: Dear username_0, please email me at john@civifirst.com for details!  
  
  ---
  
  ### Answer by username_2 on 2015-08-21. Score: 0
  
  you need to create an Extension and use hook\_civicrm\_custom
  <http://wiki.civicrm.org/confluence/display/CRMDOC43/hook_civicrm_custom>
  
  Please let me know if you need more help -
  
  > **Comments:**
  > **username_0**: Edited my post to show you what I have done so far.  
  > **username_2**: give a quick read regarding extension http://wiki.civicrm.org/confluence/display/CRMDOC/Create+an+Extension. and you can create an new extension  
  > **username_2**: so you have started well  - what is the error message now  
  
  ---
  
  