Answers.Body: <p>I wouldn't use the setvalue method, it was introduced as a stop gap measure when the api v3 was young and it's deprecated now.</p>
  
  <p>Is the relationship already active when you call the create call? setvalue isn't going to do anything more anyway.</p>
  
  <p>I don't know the specific of that api, but in general, the api calls tend to be "close to the metal" and if there is a chain of events (eg if relationship is created and lots of configuration magic, then add a membership too), they are often not included by default.</p>
  
  <p>On your specific case, if you are lucky it's just a matter of finding the right parameter to trigger the "advanced mode", or create the membership manually.</p>
  
  <p>You should check out how it's done in the code for the UI, the code handling the membership creation might be already into the BAO layer and you can easily modify (or add an extra param) to the api to benefit from it.</p>
  
  <p>If not, it might need some re-factoring....</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-24T20:08:34.740Z
Answers.Text: Thanks for the ideas Xavier, that chain of events is (I think) hinted at in the code for setvalue and which I was trying to trigger by using it to set is_active. I explicitly set 'is_active' to 0 when I create the relationship but it doesn't appear to make a difference in creating the inherited membership. I will look into the code for the UI as per your suggestion, cheers!
Answers.UserDisplayName: schatz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-25T06:07:49.333Z
Answers.Text: I've tried just about every approach I can think of and even tried creating the membership myself. I dumped the membership each time the post_hook got passed a membership object and I see at least two memberships claiming they were created. Looking in log_civicrm_membership table, it looks like they get inserted but then immediately deleted. What would cause such a behaviour?
Answers.UserDisplayName: schatz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-26T17:15:02.070Z
Answers.Text: did you try switching from relationship "setvalue" to "create"? That should work better.
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-24T08:44:49.773Z
Answers.LastActivityDate: 2015-09-24T08:44:49.773Z
Answers.UserDisplayName: Xavier
Answers.Body: <p>We've been in a same situation after mass users import and ended up re-saving all Contacts via API call, using "Create" Contact and passing an existing IDs, as it has been recently introduced as "Update" call replacement. So no real changes were made, method works exactly as if you hit "Save" button in browser for each contact, but obviously much faster.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-16T20:14:09.407Z
Answers.LastActivityDate: 2015-10-16T20:14:09.407Z
Answers.UserDisplayName: yurg
Body: <p>CiviCRM 4.6.8</p>
  
  <p>I'm using <code>Relationship.create</code> in a civicrm post hook to create an inherited membership. My membership type is set to use this relationship. However after that API call is successful, the relationship exists but no membership follows.</p>
  
  <p>I was reading the source for api/v3/Relationship.php and noticed <a href="https://github.com/civicrm/civicrm-core/blob/022394b0dc4a866f831d79ef4f9f70497f3b656d/api/v3/Relationship.php#L151" rel="nofollow">this note for the setvalue method</a>. It seems like I need to use it to invoke <code>disableEnableRelationship</code>.</p>
  
  <p>However, I tried setting is_active to 1 with <code>Relationship.setvalue</code> in my hook function right after creating the Relationship and still no membership. Is this expected behaviour?</p>
  
  <p>Here's roughly what I have:</p>
  
  <pre><code>$id = $relationship['id'];
  $params = array(
         'id' =&gt; $id,
         'field' =&gt; "is_active",
         'value' =&gt; 1,
  );
  $result = civicrm_api3('Relationship', 'setvalue', $params);
  </code></pre>
  
  <p>Curiously, I notice if I edit the specific relationship in the admin UI and save, the proper inherited membership gets created.</p>
  
Tags: <civimember><api><relationships>
Title: Relationship created by API does not add Membership until edited/updated
ThreadText: # Relationship created by API does not add Membership until edited/updated
  
  (Asked by: username_0 on 2015-09-24)
  
  CiviCRM 4.6.8
  I'm using Relationship.create in a civicrm post hook to create an inherited membership. My membership type is set to use this relationship. However after that API call is successful, the relationship exists but no membership follows.
  I was reading the source for api/v3/Relationship.php and noticed this note for the setvalue method. It seems like I need to use it to invoke disableEnableRelationship.
  However, I tried setting is_active to 1 with Relationship.setvalue in my hook function right after creating the Relationship and still no membership. Is this expected behaviour?
  Here's roughly what I have:
  $id = $relationship['id'];
  $params = array(
         'id' => $id,
         'field' => "is_active",
         'value' => 1,
  );
  $result = civicrm_api3('Relationship', 'setvalue', $params);
  
  Curiously, I notice if I edit the specific relationship in the admin UI and save, the proper inherited membership gets created.
  
  **Tags:** civimember, api, relationships
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-09-24. Score: 2
  
  I wouldn't use the setvalue method, it was introduced as a stop gap measure when the api v3 was young and it's deprecated now.
  
  Is the relationship already active when you call the create call? setvalue isn't going to do anything more anyway.
  
  I don't know the specific of that api, but in general, the api calls tend to be "close to the metal" and if there is a chain of events (eg if relationship is created and lots of configuration magic, then add a membership too), they are often not included by default.
  
  On your specific case, if you are lucky it's just a matter of finding the right parameter to trigger the "advanced mode", or create the membership manually.
  
  You should check out how it's done in the code for the UI, the code handling the membership creation might be already into the BAO layer and you can easily modify (or add an extra param) to the api to benefit from it.
  
  If not, it might need some re-factoring....
  
  > **Comments:**
  > **username_0**: Thanks for the ideas username_1, that chain of events is (I think) hinted at in the code for setvalue and which I was trying to trigger by using it to set is_active. I explicitly set 'is_active' to 0 when I create the relationship but it doesn't appear to make a difference in creating the inherited membership. I will look into the code for the UI as per your suggestion, cheers!  
  > **username_0**: I've tried just about every approach I can think of and even tried creating the membership myself. I dumped the membership each time the post_hook got passed a membership object and I see at least two memberships claiming they were created. Looking in log_civicrm_membership table, it looks like they get inserted but then immediately deleted. What would cause such a behaviour?  
  > **username_2**: did you try switching from relationship "setvalue" to "create"? That should work better.  
  
  ---
  
  ### Answer by username_3 on 2015-10-16. Score: 1
  
  We've been in a same situation after mass users import and ended up re-saving all Contacts via API call, using "Create" Contact and passing an existing IDs, as it has been recently introduced as "Update" call replacement. So no real changes were made, method works exactly as if you hit "Save" button in browser for each contact, but obviously much faster.
  
  
  ---
  
  