Answers.Body: <p>You can either do it pre() or PostProcess() hook</p>
  <p>In postProcess hook you can call below API to update case id 34</p>
  <pre><code>\Civi\Api4\CiviCase::update(TRUE)
    -&gt;addValue('subject', 'Your custom value')
    -&gt;addWhere('id', '=', 34)
    -&gt;execute();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-10T13:37:59.100Z
Answers.Text: Why do I need to explicitly save the entity? Using a hook I would expect to intercept at some point in the call flow, change some values and let the regular process continue. Why can't I just change the submitted form values?
Answers.UserDisplayName: Jan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-10T14:19:29.817Z
Answers.Text: The postprocess hook is called after the entity is saved. So either you need to use buildForm hook to set to default or via JS on form load to save it automatically. The user will be allowed to change the subject field from the form via inspect element or on the form. So it may not end up with the same subject you want to be
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-10T15:18:28.637Z
Answers.Text: I see. The subject field's value depends on values the user enters in other fields. So concatenating these values and setting the subject after submit and before saving the entity appears the best approach to me. What about the pre hook?
Answers.UserDisplayName: Jan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-10T16:19:05.797Z
Answers.Text: pre hook is a better option if the values you will be retrieving is not part of the case
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-12T10:53:46.107Z
Answers.Text: I have solved it by updating submittedValues in buildForm now. I didn't know this hook is called on POST as well. Only downside is that I have to reload contact data and reformat date fields. And buildForm ist not used on API calls.
Answers.UserDisplayName: Jan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-10T10:37:40.813Z
Answers.LastActivityDate: 2024-04-10T16:19:23.777Z
Answers.LastEditDate: 2024-04-10T16:19:23.777Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I would like to set the value of the case subject field automatically on saving. The value is made up of the values of the client name and a custom field. Is the <code>postProcess</code> or the <code>post</code> hook the better choice for this? I have tried to do it in <code>postProcess</code> but didn't find the right method to set the field value.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-04-10T12:39:33.240Z
Comments.Text: Note that case subject is a required field so you need to either set a default value on form load, or remove the required attribute before saving the form. See https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/
Comments.UserDisplayName: jitendra - fuzion
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-04-10T13:45:05.383Z
Comments.Text: Good point. I used setElementError(NULL) in validateForm and hide the field via CSS in the UI. Otherwise the JavaScript validation will be active.
Comments.UserDisplayName: Jan
Tags: <hooks>
Title: How to generate field values on save
ThreadText: # How to generate field values on save
  
  (Asked by: username_0 on 2024-04-10)
  
  I would like to set the value of the case subject field automatically on saving. The value is made up of the values of the client name and a custom field. Is the `postProcess` or the `post` hook the better choice for this? I have tried to do it in `postProcess` but didn't find the right method to set the field value.
  
  **Tags:** hooks
  
  > **Comments:**
  > **username_1**: Note that case subject is a required field so you need to either set a default value on form load, or remove the required attribute before saving the form. See https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_buildForm/  
  > **username_0**: Good point. I used setElementError(NULL) in validateForm and hide the field via CSS in the UI. Otherwise the JavaScript validation will be active.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2024-04-10. Score: 1
  
  You can either do it pre() or PostProcess() hook
  In postProcess hook you can call below API to update case id 34
  \Civi\Api4\CiviCase::update(TRUE)
    ->addValue('subject', 'Your custom value')
    ->addWhere('id', '=', 34)
    ->execute();
  
  > **Comments:**
  > **username_0**: Why do I need to explicitly save the entity? Using a hook I would expect to intercept at some point in the call flow, change some values and let the regular process continue. Why can't I just change the submitted form values?  
  > **username_2**: The postprocess hook is called after the entity is saved. So either you need to use buildForm hook to set to default or via JS on form load to save it automatically. The user will be allowed to change the subject field from the form via inspect element or on the form. So it may not end up with the same subject you want to be  
  > **username_0**: I see. The subject field's value depends on values the user enters in other fields. So concatenating these values and setting the subject after submit and before saving the entity appears the best approach to me. What about the pre hook?  
  > **username_2**: pre hook is a better option if the values you will be retrieving is not part of the case  
  > **username_0**: I have solved it by updating submittedValues in buildForm now. I didn't know this hook is called on POST as well. Only downside is that I have to reload contact data and reformat date fields. And buildForm ist not used on API calls.  
  
  ---
  
  