1

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

  • 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. – Vito Mantese Aug 10 '15 at 23:00
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
    ));
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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