Body: <p>How can one access, e.g., the newly created contact ID and relationship ID?</p>
  <p>Code example:</p>
  <pre><code>$result = civicrm_api3('Contact', 'create', array(
      'sequential' =&gt; 1,
      'contact_type' =&gt; &quot;Individual&quot;,
      'first_name' =&gt; $famFirstName,
      'last_name' =&gt; $famLastName,
      'api.Email.create' =&gt; array('email' =&gt; $famEmail, 'location_type_id' =&gt; &quot;Main&quot;),
      'api.Phone.create' =&gt; array('phone' =&gt; $famPhone, 'location_type_id' =&gt; &quot;Main&quot;, 'phone_type_id' =&gt; &quot;Phone&quot;),
      'api.Relationship.create' =&gt; array(
          'contact_id_a' =&gt; $userContactId,
          'contact_id_b' =&gt; &quot;\$value.id&quot;,
          'is_permission_a_b' =&gt; 1,                                                                  
          'is_permission_b_a' =&gt; 2,
          'relationship_type_id' =&gt; 11,
          'is_active' =&gt; 0,
          'start_date' =&gt; date(&quot;Y-m-d&quot;),
      )
  ));
  $newContactId = $result['values'][0]['contact.id'];
  $newRelationshipId = $result['values'][0]['relationship.id'];
  </code></pre>
  <p>The contact, email, phone and relationship get created, but I'd like to use the newly created values (e.g., the contact ID and relationship ID) right away in the code, and just can't figure out how (tried a couple different combinations in addition to the one shown at the end of my sample code.</p>
  <p>Thank you so much.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-08T00:10:50.273Z
Comments.Text: OK, so while playing with this, I realized I could use, e.g., $newContactId = $result['id'] ... but that only works if I run the create separately for each entity. So, still not sure how to do this with chaining : (
Comments.UserDisplayName: MtnPavlas
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-08T00:16:16.520Z
Comments.Text: My understanding is chaining is just a shorthand syntax, and it still does separate calls behind the scenes, so I would just do this without chaining if you need those ids.
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-08T14:09:08.820Z
Comments.Text: Thanks, Demerit - so no real performance enhancer, but rather just a cleaner code. I'm OK with the simple (non-chaining) solution, but if there's a way to get those ID's with chaining, would still like to know.
Comments.UserDisplayName: MtnPavlas
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-11T09:12:14.610Z
Comments.Text: FWIW I'd strongly recommend switching to APIv4 where possible.
Comments.UserDisplayName: Aidan
Tags: <api>
Title: How to access entity newly created via API
ThreadText: # How to access entity newly created via API
  
  (Asked by: username_0 on 2022-07-08)
  
  How can one access, e.g., the newly created contact ID and relationship ID?
  Code example:
  $result = civicrm_api3('Contact', 'create', array(
      'sequential' => 1,
      'contact_type' => "Individual",
      'first_name' => $famFirstName,
      'last_name' => $famLastName,
      'api.Email.create' => array('email' => $famEmail, 'location_type_id' => "Main"),
      'api.Phone.create' => array('phone' => $famPhone, 'location_type_id' => "Main", 'phone_type_id' => "Phone"),
      'api.Relationship.create' => array(
          'contact_id_a' => $userContactId,
          'contact_id_b' => "\$value.id",
          'is_permission_a_b' => 1,                                                                  
          'is_permission_b_a' => 2,
          'relationship_type_id' => 11,
          'is_active' => 0,
          'start_date' => date("Y-m-d"),
      )
  ));
  $newContactId = $result['values'][0]['contact.id'];
  $newRelationshipId = $result['values'][0]['relationship.id'];
  
  The contact, email, phone and relationship get created, but I'd like to use the newly created values (e.g., the contact ID and relationship ID) right away in the code, and just can't figure out how (tried a couple different combinations in addition to the one shown at the end of my sample code.
  Thank you so much.
  
  **Tags:** api
  
  > **Comments:**
  > **username_0**: OK, so while playing with this, I realized I could use, e.g., $newContactId = $result['id'] ... but that only works if I run the create separately for each entity. So, still not sure how to do this with chaining : (  
  > **username_1**: My understanding is chaining is just a shorthand syntax, and it still does separate calls behind the scenes, so I would just do this without chaining if you need those ids.  
  > **username_0**: Thanks, username_1 - so no real performance enhancer, but rather just a cleaner code. I'm OK with the simple (non-chaining) solution, but if there's a way to get those ID's with chaining, would still like to know.  
  > **username_2**: FWIW I'd strongly recommend switching to APIv4 where possible.  
  
  ---
  
  ## 0 answers
  
  