Answers.Body: <p>I believe it would be '$value.id' (ex. <a href="https://github.com/civicrm/civicrm-core/blob/master/api/v3/examples/Contact/APIChainedArrayValuesFromSiblingFunction.php" rel="nofollow">https://github.com/civicrm/civicrm-core/blob/master/api/v3/examples/Contact/APIChainedArrayValuesFromSiblingFunction.php</a>). Basically, '$value' can be used to reference any values added by the parent api call.</p>
  
  <p>Also, to your question about sanitization, the values should be sanitized. However, I would recommend using CRM_Utils_Request::retrieve() to get the submitted values from the request rather than $_POST as it does some extra sanitization work.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-04T13:05:50.123Z
Answers.Text: Agree with the suggestion that you use CR_Utils_Request::retrieve() rather than accessing the $_POST directly
Answers.UserDisplayName: ErikH - CiviCooP
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-05T00:48:22.720Z
Answers.Text: So is $value context sensitive to the parent API call, or to the top-level API call? If I want to use the API to create 1 adult, chain 2 student records, and chain student/parent relationships within each student, do I keep using $value? My hope is to use a form with the jQuery Repeater plugin to do multiple student sign-ups in one form, and validate/submit the whole thing at once.
Answers.UserDisplayName: Nicholai
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-03T22:09:12.583Z
Answers.LastActivityDate: 2015-04-03T22:09:12.583Z
Answers.UserDisplayName: anemirovsky
Body: <p>I've read all the docs I could find, the code examples, the 4.6 explorer, but I'm not grasping one piece. Below is code to create a new adult contact (via profile submit, to get some extra validation for free), and multiple student contacts, each with a relationship to the adult. When chaining the relationship piece, what goes into contact_id_a and contact_id_b?</p>
  
  <p>Here is the error result, calling code is at the bottom of this post:</p>
  
  <pre><code>Array
  (
      [error] =&gt; Error in call to Profile_submit : Error in call to Relationship_create : contact_id_b is not a valid integer
      [error_code] =&gt; 2001
      [error_data] =&gt; Array
          (
              [error_field] =&gt; contact_id_b
              [type] =&gt; integer
              [error_code] =&gt; 2001
              [entity] =&gt; Profile
              [action] =&gt; submit
              [is_error] =&gt; 1
              [error_message] =&gt; Error in call to Profile_submit : Error in call to Relationship_create : contact_id_b is not a valid integer
          )
  
  )
  </code></pre>
  
  <p>Edit: updated the code to better reflect what I'm trying to do, please see the comments on the innermost calls to create relationships:</p>
  
  <pre><code>  //build array of student details
    $student_profiles = array();
    foreach ($_POST['student_details'] AS $student_record){
  
      $student_profiles[] = array(
            'sequential'  =&gt; 1,
            'profile_id'  =&gt; 14,
            'first_name'  =&gt; $student_record['student_first_name'],
            'last_name'   =&gt; $student_record['student_last_name'],
            'gender_id'   =&gt;  $student_record['gender_id'],
            'birth_date'  =&gt; $student_record['birth_date'],
            'allergies'   =&gt; $student_record['allergies'],
            'api.Relationship.create' =&gt; array (
                    'sequential' =&gt; 1,
                    'contact_id_a' =&gt; '$value.id',  //is this the valid contact ID of the student profile we just submitted?
                    'contact_id_b' =&gt; '$value.api.Profile.submit',  //how do i fix this to get the Profile.submit id from the parent's record?
                    'relationship_type_id' =&gt; 1,
                    'is_permission_a_b' =&gt; 0,
                    'is_permission_b_a' =&gt; 1,
                  ),
           );
    }
  
      //build parent details and attach student detail array
      $parent_profile = array(
        'sequential' =&gt; 1,
        'profile_id' =&gt; 14,
        'first_name' =&gt; $'first_name',
        'last_name' =&gt; $'last_name',
        'api.Profile.submit' =&gt; $student_profiles,
      );
  
      //submit the main profile with all chained items
      try {
          $parent_result = civicrm_api3('Profile', 'submit', $parent_profile);
      }
      catch (CiviCRM_API3_Exception $e) {
        // handle error here
        $errorMessage = $e-&gt;getMessage();
        $errorCode = $e-&gt;getErrorCode();
        $errorData = $e-&gt;getExtraParams();
        return array('error' =&gt; $errorMessage, 'error_code' =&gt; $errorCode, 'error_data' =&gt; $errorData);
      }
  </code></pre>
  
Tags: <api><chaining>
Title: How to use API chaining to create a contact and a relationship
ThreadText: # How to use API chaining to create a contact and a relationship
  
  (Asked by: username_0 on 2015-04-03)
  
  I've read all the docs I could find, the code examples, the 4.6 explorer, but I'm not grasping one piece. Below is code to create a new adult contact (via profile submit, to get some extra validation for free), and multiple student contacts, each with a relationship to the adult. When chaining the relationship piece, what goes into contact_id_a and contact_id_b?
  Here is the error result, calling code is at the bottom of this post:
  Array
  (
      [error] => Error in call to Profile_submit : Error in call to Relationship_create : contact_id_b is not a valid integer
      [error_code] => 2001
      [error_data] => Array
          (
              [error_field] => contact_id_b
              [type] => integer
              [error_code] => 2001
              [entity] => Profile
              [action] => submit
              [is_error] => 1
              [error_message] => Error in call to Profile_submit : Error in call to Relationship_create : contact_id_b is not a valid integer
          )
  
  )
  
  Edit: updated the code to better reflect what I'm trying to do, please see the comments on the innermost calls to create relationships:
    //build array of student details
    $student_profiles = array();
    foreach ($_POST['student_details'] AS $student_record){
  
      $student_profiles[] = array(
            'sequential'  => 1,
            'profile_id'  => 14,
            'first_name'  => $student_record['student_first_name'],
            'last_name'   => $student_record['student_last_name'],
            'gender_id'   =>  $student_record['gender_id'],
            'birth_date'  => $student_record['birth_date'],
            'allergies'   => $student_record['allergies'],
            'api.Relationship.create' => array (
                    'sequential' => 1,
                    'contact_id_a' => '$value.id',  //is this the valid contact ID of the student profile we just submitted?
                    'contact_id_b' => '$value.api.Profile.submit',  //how do i fix this to get the Profile.submit id from the parent's record?
                    'relationship_type_id' => 1,
                    'is_permission_a_b' => 0,
                    'is_permission_b_a' => 1,
                  ),
           );
    }
  
      //build parent details and attach student detail array
      $parent_profile = array(
        'sequential' => 1,
        'profile_id' => 14,
        'first_name' => $'first_name',
        'last_name' => $'last_name',
        'api.Profile.submit' => $student_profiles,
      );
  
      //submit the main profile with all chained items
      try {
          $parent_result = civicrm_api3('Profile', 'submit', $parent_profile);
      }
      catch (CiviCRM_API3_Exception $e) {
        // handle error here
        $errorMessage = $e->getMessage();
        $errorCode = $e->getErrorCode();
        $errorData = $e->getExtraParams();
        return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
      }
  
  **Tags:** api, chaining
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-04-03. Score: 3 | Accepted answer
  
  I believe it would be '$value.id' (ex. <https://github.com/civicrm/civicrm-core/blob/master/api/v3/examples/Contact/APIChainedArrayValuesFromSiblingFunction.php>). Basically, '$value' can be used to reference any values added by the parent api call.
  
  Also, to your question about sanitization, the values should be sanitized. However, I would recommend using CRM\_Utils\_Request::retrieve() to get the submitted values from the request rather than $\_POST as it does some extra sanitization work.
  
  > **Comments:**
  > **username_2**: Agree with the suggestion that you use CR_Utils_Request::retrieve() rather than accessing the $_POST directly  
  > **username_0**: So is $value context sensitive to the parent API call, or to the top-level API call? If I want to use the API to create 1 adult, chain 2 student records, and chain student/parent relationships within each student, do I keep using $value? My hope is to use a form with the jQuery Repeater plugin to do multiple student sign-ups in one form, and validate/submit the whole thing at once.  
  
  ---
  
  