2

We have 2 relationship types : A and B. We have about 500 contacts with A-relationships already set, and for each we need to create a B-relationship with the same contact. For example :

We already have :

Bob -> A -> Alice

We need to have :

Bob -> A -> Alice

Bob -> B -> Alice

It could be done manually but I thought I could write a custom cron job which does this :

  • loop over contacts in a particular group (we already have this group)
  • for each contact with a B-relationship and without a A-relationship :

grab the A-relationship target contact (let's call him C)

create a B-relationship with C

  • end for each

How can I write a custom cron job for CiviCRM ? Is this a good idea ? Is there a better way to achieve this ?

Thanks

2 Answers 2

2

Are you sure it isn't more "logical" to not create/rename the relationship so instead of being "A" it's "A&B"?. Would be easier at least ;)

If you are on drupal, you can run a php script that has already dealt with initializing and authentication, and from within it, you can directly use civicrm_api3 to read the existing relationships and create new ones (it takes a bit longer, but safer than trying to access at the db layer directly)

The script itself is fairly simple, fetching all contacts in a group and creating a new relationship.

The easiest is probably to use the api explorer (on your own database or better a copy of it) to test and get it generate for your the right php code that you mostly have to copy/paste into your script file

This is assuming you have access to the shell on your machine, this is way easier to run these type of long running scripts

2
  • Unfortunatly (for us) we need the 2 relationships, they don't represent the same thing and they can "evolve" differently (at first we thought only one could do the job, but no...). I'm on Drupal, I'll try to write such a script. Can I find examples somewhere ? Oct 23, 2015 at 13:14
  • updated and mentioning api explorer
    – Xavier
    Oct 23, 2015 at 13:22
3

Here's a trick. Use the API to get relationships with one relationship type and chain an API call to create a relationship with the other relationship type. You have to explicitly say that the new relationship has "0" as the ID, or else it'll just change the relationships you find.

The following example creates a relationship of type 8 for each relationship of type 7.

CRM.api3('Relationship', 'get', {
  "relationship_type_id": 7,
  "api.Relationship.create":{
    "id":0,
    "relationship_type_id":8,
    "contact_id_a":"$value.contact_id_a",
    "contact_id_b":"$value.contact_id_b",
    "is_active":"$value.is_active"
  }
})

You could add parameters to the Relationship/Get call to just pick certain relationships. Theoretically, you could also chain all of this to another API call, perhaps to get the members of a group.

1
  • Awesome ! I tested this on sandbox, works great. I'm new to CiviCRM, I wasn't aware of the Javascript-Smarty-PHP-REST-Drush, great tool. Thanks ! Oct 24, 2015 at 9:02

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.