Answers.Body: <p>Are you sure it isn't more "logical" to not create/rename the relationship so instead of being "A" it's "A&amp;B"?. Would be easier at least ;)</p>
  
  <p>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)</p>
  
  <p>The script itself is fairly simple, fetching all contacts in a group and creating a new relationship.</p>
  
  <p>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</p>
  
  <p>This is assuming you have access to the shell on your machine, this is way easier to run these type of long running scripts</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-23T13:14:26.113Z
Answers.Text: 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 ?
Answers.UserDisplayName: Florian Motteau
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-23T13:22:36.540Z
Answers.Text: updated and mentioning api explorer
Answers.UserDisplayName: Xavier
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-23T13:08:19.700Z
Answers.LastActivityDate: 2015-10-23T13:22:11.573Z
Answers.LastEditDate: 2015-10-23T13:22:11.573Z
Answers.UserDisplayName: Xavier
Answers.Body: <p>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.</p>
  
  <p>The following example creates a relationship of type 8 for each relationship of type 7.</p>
  
  <pre><code>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"
    }
  })
  </code></pre>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-24T09:02:35.773Z
Answers.Text: 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 !
Answers.UserDisplayName: Florian Motteau
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-23T18:16:54.637Z
Answers.LastActivityDate: 2015-10-23T18:16:54.637Z
Answers.UserDisplayName: Andie Hunt
Body: <p>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 :</p>
  
  <p><strong>We already have :</strong></p>
  
  <p>Bob -> A -> Alice</p>
  
  <p><strong>We need to have :</strong></p>
  
  <p>Bob -> A -> Alice</p>
  
  <p>Bob -> B -> Alice</p>
  
  <p>It could be done manually but I thought I could write a custom cron job which does this :</p>
  
  <ul>
  <li>loop over contacts in a particular group (we already have this group)</li>
  <li>for each contact with a B-relationship and without a A-relationship :</li>
  </ul>
  
  <p>grab the A-relationship target contact (let's call him C)</p>
  
  <p>create a B-relationship with C</p>
  
  <ul>
  <li>end for each</li>
  </ul>
  
  <p>How can I write a custom cron job for CiviCRM ? Is this a good idea ? Is there a better way to achieve this ?</p>
  
  <p>Thanks</p>
  
Tags: <cron-job><relationships>
Title: Batch create relationships
ThreadText: # Batch create relationships
  
  (Asked by: username_0 on 2015-10-23)
  
  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
  
  **Tags:** cron-job, relationships
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-10-23. Score: 2 | Accepted answer
  
  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
  
  > **Comments:**
  > **username_0**: 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 ?  
  > **username_1**: updated and mentioning api explorer  
  
  ---
  
  ### Answer by username_2 on 2015-10-23. Score: 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.
  
  > **Comments:**
  > **username_0**: 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 !  
  
  ---
  
  