Answers.Body: <p>SO I was able to figure this out, turns out I wasn't able to reference the custom data directly from the activity object but had to use the api to grab the custom data related to that activity object.</p>
  
  <pre><code>function checkActivityMakeRelationship_civicrm_post($op, $objectName, $objectId, &amp;$objectRef) {
  /* This function checks if a specific activity was created and if so makes a related relationship
  */
  
  if(($op == 'create' || $op == 'edit') &amp;&amp; $objectName == 'Activity') {
      $params = array(
          'sequential' =&gt; 1,
          'id' =&gt; $objectId,
          'return' =&gt; "custom_50,custom_51",//replace with your specific custom
      );
      $activity = civicrm_api3('Activity', 'get', $params);
      $contact_a_id = (int)$activity[values][0]['custom_50_id'];//note the appended _id
      $contact_b_id = (int)$activity[values][0]['custom_51_id'];// appended _id
      $result = civicrm_api3('Relationship', 'create', array(
      'sequential' =&gt; 1,
      'contact_id_a' =&gt; $contact_a_id,
      'contact_id_b' =&gt; $contact_b_id,
      'relationship_type_id' =&gt; 11,//your specific relationship id
      ));
  }
  </code></pre>
  
  <p>}</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-11T13:59:38.203Z
Answers.LastActivityDate: 2015-08-11T14:05:50.623Z
Answers.LastEditDate: 2015-08-11T14:05:50.623Z
Answers.UserDisplayName: Vito Mantese
Body: <p>I'm trying to use a php hook to automatically create a relationship between two contacts. Let me set the stage for you.</p>
  
  <p>Meeting Extender: a custom data set that extends the meeting activity(activity entity). It basically records a few custom fields, two of which are contact references(think pitcher and pitchee). These two fields would be two integers referring to contact A and contact B. Basically, I want to write a hook that creates a relationship, called Pitched to (A to B) and been Pitched(B to A).</p>
  
  <p>The pipeline</p>
  
  <p>A meeting is created, in the custom field set Google is saved as contact A and Apple is saved as contact B. The hook should realize a new activity has been created, check if the Pitched relationship exists between Google --> Apple and if not create it.</p>
  
  <p>This was my attempt so far(in an extension)</p>
  
  <pre><code>function checkActivityMakeRelationship_civicrm_post($op, $objectName, $objectId, &amp;$objectRef) {
  /* This file checks if an activity was created and if so
   */
  
      if(($op == 'create' || $op == 'edit') &amp;&amp; $objectName == 'Activity') {
          $result = civicrm_api3('Relationship', 'create', array(
          'sequential' =&gt; 1,
          'contact_id_a' =&gt; **integer-referring-to-contact-a**,
          'contact_id_b' =&gt; **integer-referring-to-contact-b**,
          'relationship_type_id' =&gt; 11,//this is the relationship type for pitched
      ));
      }
  }
  </code></pre>
  
  <p>Ignoring the 'relationship-already-exists' check for now, how to I find the correct integers for the contact A and contact B i just entered in to my custom data set. I imagine it is a part of $op but I'm not sure how to access it. Any help is appreciated, thanks.
  V</p>
  
  <p>PS: I'm running wordpress 4.2.4 and civicrm 4.6.6</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-08-10T23:00:02.867Z
Comments.Text: I'm assuming its something such as $objectRef['custom_x'] or $object->custom_x where x is the proper integer but neither of these is working.
Comments.UserDisplayName: Vito Mantese
Tags: <api><activities><hooks><relationships>
Title: Update custom field from other custom data
ThreadText: # Update custom field from other custom data
  
  (Asked by: username_0 on 2015-08-10)
  
  I'm trying to use a php hook to automatically create a relationship between two contacts. Let me set the stage for you.
  
  Meeting Extender: a custom data set that extends the meeting activity(activity entity). It basically records a few custom fields, two of which are contact references(think pitcher and pitchee). These two fields would be two integers referring to contact A and contact B. Basically, I want to write a hook that creates a relationship, called Pitched to (A to B) and been Pitched(B to A).
  
  The pipeline
  
  A meeting is created, in the custom field set Google is saved as contact A and Apple is saved as contact B. The hook should realize a new activity has been created, check if the Pitched relationship exists between Google --> Apple and if not create it.
  
  This was my attempt so far(in an extension)
  
  ```
  function checkActivityMakeRelationship_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  /* This file checks if an activity was created and if so
   */
  
      if(($op == 'create' || $op == 'edit') && $objectName == 'Activity') {
          $result = civicrm_api3('Relationship', 'create', array(
          'sequential' => 1,
          'contact_id_a' => **integer-referring-to-contact-a**,
          'contact_id_b' => **integer-referring-to-contact-b**,
          'relationship_type_id' => 11,//this is the relationship type for pitched
      ));
      }
  }
  
  ```
  
  Ignoring the 'relationship-already-exists' check for now, how to I find the correct integers for the contact A and contact B i just entered in to my custom data set. I imagine it is a part of $op but I'm not sure how to access it. Any help is appreciated, thanks.
  V
  
  PS: I'm running wordpress 4.2.4 and civicrm 4.6.6
  
  **Tags:** api, activities, hooks, relationships
  
  > **Comments:**
  > **username_0**: I'm assuming its something such as $objectRef['custom_x'] or $object->custom_x where x is the proper integer but neither of these is working.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-08-11. Score: 1
  
  SO I was able to figure this out, turns out I wasn't able to reference the custom data directly from the activity object but had to use the api to grab the custom data related to that activity object.
  
  ```
  function checkActivityMakeRelationship_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  /* This function checks if a specific activity was created and if so makes a related relationship
  */
  
  if(($op == 'create' || $op == 'edit') && $objectName == 'Activity') {
      $params = array(
          'sequential' => 1,
          'id' => $objectId,
          'return' => "custom_50,custom_51",//replace with your specific custom
      );
      $activity = civicrm_api3('Activity', 'get', $params);
      $contact_a_id = (int)$activity[values][0]['custom_50_id'];//note the appended _id
      $contact_b_id = (int)$activity[values][0]['custom_51_id'];// appended _id
      $result = civicrm_api3('Relationship', 'create', array(
      'sequential' => 1,
      'contact_id_a' => $contact_a_id,
      'contact_id_b' => $contact_b_id,
      'relationship_type_id' => 11,//your specific relationship id
      ));
  }
  
  ```
  
  }
  
  
  ---
  
  