Answers.Body: <p>If I understand it correctly you want to add contact (New individual) to a group of  logged in user, then i would write something like this</p>
  
  <pre><code>function search_civicrm_pre($op, $objectName, $id, &amp;$params) {
    if ($objectName == 'Individual' &amp;&amp; $op == 'create) {
      $groups = civicrm_api3('Contact', 'getsingle', [
        'return' =&gt; "group",
        'id' =&gt; 'user_contact_id',
      ])['groups'];
      foreach (explode(',', $groups) as $groupId) {
        $params['group'][$groupId] = 1;
      }
    }
  }
  </code></pre>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T10:41:05.253Z
Answers.Text: Hello Pradip Sir, I successfully fetch the group id of currently logged in contact(Only one group is there). When I am adding a new Individual it must attach a group of currently logged in contact. How can I do that? I am not getting a solution.
Answers.UserDisplayName: Dipak Giri
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T10:43:03.860Z
Answers.Text: Did you try above snippet?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T10:49:31.953Z
Answers.Text: Yes. Give an error "field group unset or not existing"
Answers.UserDisplayName: Dipak Giri
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T10:56:43.323Z
Answers.Text: That means the logged in user is not assigned to any group
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T10:59:02.313Z
Answers.Text: Use TRY CATCH method to handle fatal error when Logged in user is not assigned to any group
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T11:11:50.653Z
Answers.Text: No Sir. It is already in there (I checked by GroupContact API) and also showing a group id.
Answers.UserDisplayName: Dipak Giri
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T11:12:52.307Z
Answers.Text: What does contact api with above params return?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T11:13:49.293Z
Answers.Text: {
  
  
      "invalid_field": "group",
      "is_error": 1,
      "error_message": "field group unset or not existing"
  }
Answers.UserDisplayName: Dipak Giri
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T11:14:06.967Z
Answers.Text: I used a groupcontact API and it will return a group ID. But I want to add it to the new Individual contact.
Answers.UserDisplayName: Dipak Giri
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T11:16:01.240Z
Answers.Text: I have updated the code, Try the updated snippet
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T11:21:23.047Z
Answers.Text: Thanks Pradeep Sir. It is working very well. But how this code is working. I didn't understand.
Answers.UserDisplayName: Dipak Giri
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T11:35:16.087Z
Answers.Text: pre() hook is used to alter the params which is used for creating a contact. You can pass additional params likes groups, tags etc since the BAO handles it correctly. In short pre() hook is invoked just before a contact is created and post after a contact.
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T10:32:30.360Z
Answers.LastActivityDate: 2019-01-08T11:15:32.530Z
Answers.LastEditDate: 2019-01-08T11:15:32.530Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I am trying to assign a group from currently logged in user(One group in assigned only) to a newly created individual. </p>
  
  <pre><code>function search_civicrm_post($op, $objectName, $objectId, &amp;$objectRef){ {
  
  $getContactId = (int) CRM_Core_Session::singleton()-&gt;getLoggedInContactID();
  
  try {
      $result = civicrm_api3('GroupContact', 'getsingle', [
          'return' =&gt; ["group_id"],
          'contact_id' =&gt; $getContactId,
      ]);
  } 
  catch (CRM_Exception $e) {
      echo "error";
  }
  
  $getGroupId = $result['group_id'];
  $recentContactId= '';
  $query ="INSERT INTO `civicrm_group_contact` (`group_id`, `contact_id`, `status`) VALUES ('.$getGroupId', '$recentContactId','Added')";
  </code></pre>
  
  <p>But problem is that how will I get the id of recently added individuals;</p>
  
Tags: <extension><groups><postcode>
Title: Assign a group from logged in user contact group to new individual
ThreadText: # Assign a group from logged in user contact group to new individual
  
  (Asked by: username_0 on 2019-01-08)
  
  I am trying to assign a group from currently logged in user(One group in assigned only) to a newly created individual. 
  function search_civicrm_post($op, $objectName, $objectId, &$objectRef){ {
  
  $getContactId = (int) CRM_Core_Session::singleton()->getLoggedInContactID();
  
  try {
      $result = civicrm_api3('GroupContact', 'getsingle', [
          'return' => ["group_id"],
          'contact_id' => $getContactId,
      ]);
  } 
  catch (CRM_Exception $e) {
      echo "error";
  }
  
  $getGroupId = $result['group_id'];
  $recentContactId= '';
  $query ="INSERT INTO `civicrm_group_contact` (`group_id`, `contact_id`, `status`) VALUES ('.$getGroupId', '$recentContactId','Added')";
  
  But problem is that how will I get the id of recently added individuals;
  
  **Tags:** extension, groups, postcode
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-08. Score: 2 | Accepted answer
  
  If I understand it correctly you want to add contact (New individual) to a group of  logged in user, then i would write something like this
  function search_civicrm_pre($op, $objectName, $id, &$params) {
    if ($objectName == 'Individual' && $op == 'create) {
      $groups = civicrm_api3('Contact', 'getsingle', [
        'return' => "group",
        'id' => 'user_contact_id',
      ])['groups'];
      foreach (explode(',', $groups) as $groupId) {
        $params['group'][$groupId] = 1;
      }
    }
  }
  
  HTH
  Pradeep
  
  > **Comments:**
  > **username_0**: Hello Pradip Sir, I successfully fetch the group id of currently logged in contact(Only one group is there). When I am adding a new Individual it must attach a group of currently logged in contact. How can I do that? I am not getting a solution.  
  > **username_1**: Did you try above snippet?  
  > **username_0**: Yes. Give an error "field group unset or not existing"  
  > **username_1**: That means the logged in user is not assigned to any group  
  > **username_1**: Use TRY CATCH method to handle fatal error when Logged in user is not assigned to any group  
  > **username_0**: No Sir. It is already in there (I checked by GroupContact API) and also showing a group id.  
  > **username_1**: What does contact api with above params return?  
  > **username_0**: {       "invalid_field": "group",     "is_error": 1,     "error_message": "field group unset or not existing" }  
  > **username_0**: I used a groupcontact API and it will return a group ID. But I want to add it to the new Individual contact.  
  > **username_1**: I have updated the code, Try the updated snippet  
  > **username_0**: Thanks Pradeep Sir. It is working very well. But how this code is working. I didn't understand.  
  > **username_1**: pre() hook is used to alter the params which is used for creating a contact. You can pass additional params likes groups, tags etc since the BAO handles it correctly. In short pre() hook is invoked just before a contact is created and post after a contact.  
  
  ---
  
  