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:

    [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);

1 Answer 1


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.

  • Agree with the suggestion that you use CR_Utils_Request::retrieve() rather than accessing the $_POST directly Commented Apr 4, 2015 at 13:05
  • 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.
    – Nicholai
    Commented Apr 5, 2015 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.