Answers.Body: <p>Thanks for sharing this, I agree that it looks like a bug. Instructions on filing a bug report here: <a href="https://civicrm.org/bug-reporting" rel="nofollow">https://civicrm.org/bug-reporting</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-27T06:37:14.510Z
Answers.LastActivityDate: 2015-05-28T02:30:34.580Z
Answers.LastEditDate: 2015-05-28T02:30:34.580Z
Answers.UserDisplayName: ErikH - CiviCooP
Answers.Body: <p>I believe this is normal behavior for the api, although the error message is a bit obtuse. By default it will not create cases or activities (or anything else for that matter) if the logged-in user does not have permission to do so.</p>
  
  <p>Add <code>'check_permissions' =&gt; 0</code> to your api params if you wish it to proceed without permission checks.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-12T19:24:42.887Z
Answers.Text: I tried adding 'check_permissions' => 0 to no effect.  (Isn't it 0 by default anyway?  Or is that for certain other sets of the API?)
Answers.UserDisplayName: Norris
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-11T16:54:05.033Z
Answers.LastActivityDate: 2015-06-11T16:54:05.033Z
Answers.UserDisplayName: Coleman
Body: <p>I think this is a bug.</p>
  
  <p>From the API Explorer, I can successfully run this to create a new case, and it is auto-populated with the default Sequence:</p>
  
  <pre><code>$result = civicrm_api3('Case', 'create', array(
    'sequential' =&gt; 1,   
    'contact_id' =&gt; 4243,
    'case_type_id' =&gt; "member_application",
    'creator_id' =&gt; 1033,
    'subject' =&gt; "Application",
    'medium_id' =&gt; 6,
  ));
  </code></pre>
  
  <p>But running that same code as a hook fails with the error: </p>
  
  <pre><code>CiviCRM_API3_Exception: "source_contact_id is not a valid integer"
  </code></pre>
  
  <p>It actually does create the case and the initial Open Case activity, but seems to fail while trying to create the first activity in my Sequence, which is not created.</p>
  
  <p>I can fix it by editing api/v3/utils.php and adding this at line 1956:    </p>
  
  <pre><code>if ($fieldValue =='user_contact_id')
    $fieldValue = 1033;
  </code></pre>
  
  <p>I think what's happening is that somewhere along the line, it sets the value of source_contact_id to 'user_contact_id'.  The _civicrm_api3_validate_integer function is supposed to replace this with the currently logged in user.  This works correctly when running from the API Explorer, but not when running from a hook.</p>
  
  <p>The proper solution would be to pass the API "creator_id" through to "source_contact_id" for the extra activities, but I don't know how to fix that.  My hack to hard code the contact works for me for now.</p>
  
Tags: <api><civicase>
Title: Why doesn't creating a case from API work?
ThreadText: # Why doesn't creating a case from API work?
  
  (Asked by: username_0 on 2015-05-27)
  
  I think this is a bug.
  
  From the API Explorer, I can successfully run this to create a new case, and it is auto-populated with the default Sequence:
  
  ```
  $result = civicrm_api3('Case', 'create', array(
    'sequential' => 1,   
    'contact_id' => 4243,
    'case_type_id' => "member_application",
    'creator_id' => 1033,
    'subject' => "Application",
    'medium_id' => 6,
  ));
  
  ```
  
  But running that same code as a hook fails with the error:
  
  ```
  CiviCRM_API3_Exception: "source_contact_id is not a valid integer"
  
  ```
  
  It actually does create the case and the initial Open Case activity, but seems to fail while trying to create the first activity in my Sequence, which is not created.
  
  I can fix it by editing api/v3/utils.php and adding this at line 1956:
  
  ```
  if ($fieldValue =='user_contact_id')
    $fieldValue = 1033;
  
  ```
  
  I think what's happening is that somewhere along the line, it sets the value of source\_contact\_id to 'user\_contact\_id'. The \_civicrm\_api3\_validate\_integer function is supposed to replace this with the currently logged in user. This works correctly when running from the API Explorer, but not when running from a hook.
  
  The proper solution would be to pass the API "creator\_id" through to "source\_contact\_id" for the extra activities, but I don't know how to fix that. My hack to hard code the contact works for me for now.
  
  **Tags:** api, civicase
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-05-27. Score: 2 | Accepted answer
  
  Thanks for sharing this, I agree that it looks like a bug. Instructions on filing a bug report here: <https://civicrm.org/bug-reporting>
  
  
  ---
  
  ### Answer by username_2 on 2015-06-11. Score: 0
  
  I believe this is normal behavior for the api, although the error message is a bit obtuse. By default it will not create cases or activities (or anything else for that matter) if the logged-in user does not have permission to do so.
  
  Add `'check_permissions' => 0` to your api params if you wish it to proceed without permission checks.
  
  > **Comments:**
  > **username_0**: I tried adding 'check_permissions' => 0 to no effect.  (Isn't it 0 by default anyway?  Or is that for certain other sets of the API?)  
  
  ---
  
  