1

Basically what I need to do is search for contacts created by the current logged in user. I have tried the change log tab but that does not work for me because then it would be contacts created by user x which is not what I want. I don't know if this is possible. Please help.

I need to create a smart group with this search criteria. My end goal is to use this smart group to give access to all users to view/edit the contacts they themselves created,

  • Are you using Drupal? If you just want to display this stuff it might be possible with Drupal Views – mickadoo Sep 6 '17 at 9:42
  • Yes I am using Drupal but I am bounded to use the civicrm User interface – nmeegama Sep 6 '17 at 11:12
1

I don't think this is currently possible with the existing functionality. But you can attain this by custom fields with little customization using extension. What needs to be done is -

  • Create a contact custom set from Administer -> Customise Data & Screens -> Custom Fields.
  • Add a single field. Eg. Created By. Make it searchable (Select Is this Field Searchable? to Yes ).
  • Create a simple extension using civix.
  • Write post hook and save a value for this field in DB
  • Go to Advance Search and search it by using this field.
  • Thank you for the reply. Your method solves half of my problem. The thing is I want to search by current logged in user. Simply the Advanced search / Smart group should show contacts created by the current logged in user. So if user x logs in The smart group should show contacts xa, xb, xc But If user y logs in it should show ya, yb, yc as contacts in the smart group – nmeegama Sep 7 '17 at 11:03
  • yes, I understand your requirement. This is what the posted answer can do with the extension. You need to write a little code inside the post hook which saves the logged in user id into this custom field after a contact is created. And then you can use this field in the advance search. – jitendra - fuzion Sep 7 '17 at 11:09
  • But still on the advanced search we would have to type in the logged in users ID right ? We can do the same in the Changelog section using the Added by field and type the created users email to search I want something like created id = <?php $_GET['cid'] ?> Something similar to contextual filters in Drupal Hope I am not confusing you – nmeegama Sep 7 '17 at 12:08
1

I solved this the following was I first installed the below extensions on civicrm

  1. Entity Settings Helper Extension
  2. Related Permissions Module

enter image description here

Then I created a relationship

  1. GOTO /civicrm/admin/reltype?reset=1
  2. Click on Add Relationship Type and add the relationship. Make sure you check the Always Permission A to B check box enter image description here

The next step is to create a Drupal module that will add the above created relationship to the new contact and the creator of the contact. We need to just use on hook to get this done. The code has the relationship ID hard coded but it works :)

/** * Implements hook_civicrm_post */ function crc_civicrm_creator_relate_civicrm_post($op, $objectName, $objectId, &$objectRef) { if (!civicrm_initialize()) { return; }

if ($objectName == "Individual" && $op == 'create') { global $user; $userObj = user_load($user->uid); // Get the email of the current logged in user $emailObj = civicrm_api3('Email', 'get', [ 'sequential' => 1, 'email' => $userObj->mail, ]); //Create a relationship between the current user and the created // individual contact $result = civicrm_api3('Relationship ', 'create', [ 'sequential' => 1, 'contact_id_a' => $emailObj['values'][0]['contact_id'], 'contact_id_b' => $objectId, 'relationship_type_id' => 11, ]); } }

THATS IT. Now enable the module and create your contacts and you will also be able to search view and edit them.

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.