Answers.Body: <p>The post hook does not perform as expected for cases :-) I have done some mods to do exactly what you are doing: automatically assigning a role. I have done this in 2 ways:</p>
  
  <ol>
  <li>using the post hook on the Activity Open Case</li>
  <li>using the caseSummary hook and checking if there is no other activity than Open Case. In this case I still allow the user to override what I have done as it would then have more activities than just Open Case</li>
  </ol>
  
  <p>A refactor of CiviCRM Case is still on my wish list :-)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T16:28:15.003Z
Answers.Text: Good idea on the 'Open Case' one. The case probably has the clients associated with it by then? (I'm just looking at the case API code, it creates the case, then adds the contacts, then processes the XML configuration which is where the activity timeline lives)
Answers.UserDisplayName: DaveFF
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T16:34:42.367Z
Answers.Text: Yes, all the Case stuff is then completed. The client is there, the relationship with the case coordinator etc.
Answers.UserDisplayName: ErikH - CiviCooP
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T15:20:01.880Z
Answers.LastActivityDate: 2015-12-14T15:20:01.880Z
Answers.UserDisplayName: ErikH - CiviCooP
Answers.Body: <p>How I solved this was to bring CRM_Case_BAO_Case into custom PHP, and basically copy the firing of the hooks from create() to addCaseToContact(), changing the entity type to the new 'CaseContact'.</p>
  
  <p>Now the adding of a contact to a case fires a hook, that I can catch to create the relationship for a case role. It even works for cases with multiple clients, if you have this enabled.</p>
  
  <p>Would pre/post hooks for adding a contact to a case be a useful thing to have in core?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T12:39:55.443Z
Answers.LastActivityDate: 2015-12-14T12:39:55.443Z
Answers.UserDisplayName: DaveFF
Answers.Body: <p>You can use the postprocess hook and check for CRM_Case_Form_Case. I did have some trouble finding the case id though, but maybe I'm just not seeing it. A "bad" workaround would be to assume the case id is the latest one just created in the database, unless it's a busy site.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T16:35:33.467Z
Answers.Text: You can indeed. In my case the stuff could also come via the API using Webforms, so the postProcess was no joy for me :-)
Answers.UserDisplayName: ErikH - CiviCooP
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T14:51:14.687Z
Answers.LastActivityDate: 2015-12-14T14:51:14.687Z
Answers.UserDisplayName: Demerit
Body: <p>I'm setting up CiviCase with a new case type of 'Press Release'. We only have one press officer, but want other staff (those who more often deal directly with our members) to be able to open cases. So I wanted a case role of 'Press Officer', as the case manager, automatically given to a particular cid for all cases of that type.</p>
  
  <p>My first thought for this was a post hook, but when you create a case, what you get in the post hook has no client contact IDs set yet. (This isn't a case of <a href="https://issues.civicrm.org/jira/browse/CRM-15914" rel="nofollow">CRM-15914</a>, it's not there if I do an explicit Case.get on that case ID either.) The problem is, clients are added to the case (inserted into civicrm_case_contact) only after the post hook is fired for the case creation, and adding a client has no hooks of its own.</p>
  
  <p>How can I automatically assign a role to someone other than the creator when a case is created?</p>
  
Tags: <civicase><hooks>
Title: Determine client of new case from within hook
ThreadText: # Determine client of new case from within hook
  
  (Asked by: username_0 on 2015-12-14)
  
  I'm setting up CiviCase with a new case type of 'Press Release'. We only have one press officer, but want other staff (those who more often deal directly with our members) to be able to open cases. So I wanted a case role of 'Press Officer', as the case manager, automatically given to a particular cid for all cases of that type.
  
  My first thought for this was a post hook, but when you create a case, what you get in the post hook has no client contact IDs set yet. (This isn't a case of [CRM-15914](https://issues.civicrm.org/jira/browse/CRM-15914), it's not there if I do an explicit Case.get on that case ID either.) The problem is, clients are added to the case (inserted into civicrm\_case\_contact) only after the post hook is fired for the case creation, and adding a client has no hooks of its own.
  
  How can I automatically assign a role to someone other than the creator when a case is created?
  
  **Tags:** civicase, hooks
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2015-12-14. Score: 1
  
  The post hook does not perform as expected for cases :-) I have done some mods to do exactly what you are doing: automatically assigning a role. I have done this in 2 ways:
  
  1. using the post hook on the Activity Open Case
  2. using the caseSummary hook and checking if there is no other activity than Open Case. In this case I still allow the user to override what I have done as it would then have more activities than just Open Case
  
  A refactor of CiviCRM Case is still on my wish list :-)
  
  > **Comments:**
  > **username_0**: Good idea on the 'Open Case' one. The case probably has the clients associated with it by then? (I'm just looking at the case API code, it creates the case, then adds the contacts, then processes the XML configuration which is where the activity timeline lives)  
  > **username_1**: Yes, all the Case stuff is then completed. The client is there, the relationship with the case coordinator etc.  
  
  ---
  
  ### Answer by username_0 on 2015-12-14. Score: 0
  
  How I solved this was to bring CRM\_Case\_BAO\_Case into custom PHP, and basically copy the firing of the hooks from create() to addCaseToContact(), changing the entity type to the new 'CaseContact'.
  
  Now the adding of a contact to a case fires a hook, that I can catch to create the relationship for a case role. It even works for cases with multiple clients, if you have this enabled.
  
  Would pre/post hooks for adding a contact to a case be a useful thing to have in core?
  
  
  ---
  
  ### Answer by username_2 on 2015-12-14. Score: 0
  
  You can use the postprocess hook and check for CRM\_Case\_Form\_Case. I did have some trouble finding the case id though, but maybe I'm just not seeing it. A "bad" workaround would be to assume the case id is the latest one just created in the database, unless it's a busy site.
  
  > **Comments:**
  > **username_1**: You can indeed. In my case the stuff could also come via the API using Webforms, so the postProcess was no joy for me :-)  
  
  ---
  
  