Answers.Body: <p>I ended up going for a very hacky solution.</p>
  
  <p>The buildForm hook runs twice when you are adding/editing an activity with custom fields. Once for the activity and once for the custom fields. So, during the activity run I am taking the contact ID from the form array and putting it into $_SESSION. Then on the custom fields run I retrieve it and use it to get the values from the API with which to populate them.</p>
  
  <p>I had tried getting the contact ID from the URL as Sleewok suggested, but it is not in there!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-22T16:34:49.217Z
Answers.Text: Storing in $_SESSION is the way to go. There is no other way. I've attempted to use static variables, and even add custom listeners in a class, but the calls are still separated so the static variable is cleared between the main activity form and the custom fields form. So no way to share except by storing in the session (the database could be used too but that's heavy overhead for such a little thing).
Answers.UserDisplayName: herb
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-15T15:08:30.920Z
Answers.LastActivityDate: 2016-12-15T15:08:30.920Z
Answers.UserDisplayName: naomi
Body: <p>I would like to prepopulate a set of custom fields (on an activity) with data about the contact it pertains to. (The data I want to pull in is in a set of custom fields on the contact).</p>
  
  <p>My idea was to use <code>hook_civicrm_buildForm</code> and look up the data by contact id, retrieve it from the API and place it into the form. But the <code>$form</code> array for the custom fields form does not contain the contact id.</p>
  
  <p>Then I thought of using tokens to set default values in the custom fields (either for them all, or even just to set one to the contact id in order to solve the above issue) but I tried {contact.contact_id} and it came out just like that as a literal. Can tokens be used in custom fields on activities? If not, can you suggest another way of getting my hands on the contact id during the form building process?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-12-02T20:55:03.150Z
Comments.Text: There is probably an easier way to get the ID, but how about just pulling it from the URL since it is listed there?
Comments.UserDisplayName: Sleewok
Tags: <custom-fields><hooks>
Title: How to prepopulate custom fields
ThreadText: # How to prepopulate custom fields
  
  (Asked by: username_0 on 2016-12-01)
  
  I would like to prepopulate a set of custom fields (on an activity) with data about the contact it pertains to. (The data I want to pull in is in a set of custom fields on the contact).
  
  My idea was to use `hook_civicrm_buildForm` and look up the data by contact id, retrieve it from the API and place it into the form. But the `$form` array for the custom fields form does not contain the contact id.
  
  Then I thought of using tokens to set default values in the custom fields (either for them all, or even just to set one to the contact id in order to solve the above issue) but I tried {contact.contact\_id} and it came out just like that as a literal. Can tokens be used in custom fields on activities? If not, can you suggest another way of getting my hands on the contact id during the form building process?
  
  **Tags:** custom-fields, hooks
  
  > **Comments:**
  > **username_1**: There is probably an easier way to get the ID, but how about just pulling it from the URL since it is listed there?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-12-15. Score: 2 | Accepted answer
  
  I ended up going for a very hacky solution.
  
  The buildForm hook runs twice when you are adding/editing an activity with custom fields. Once for the activity and once for the custom fields. So, during the activity run I am taking the contact ID from the form array and putting it into $\_SESSION. Then on the custom fields run I retrieve it and use it to get the values from the API with which to populate them.
  
  I had tried getting the contact ID from the URL as username_1 suggested, but it is not in there!
  
  > **Comments:**
  > **username_2**: Storing in $_SESSION is the way to go. There is no other way. I've attempted to use static variables, and even add custom listeners in a class, but the calls are still separated so the static variable is cleared between the main activity form and the custom fields form. So no way to share except by storing in the session (the database could be used too but that's heavy overhead for such a little thing).  
  
  ---
  
  