Answers.Body: <p>I don't see a way of doing it without writing custom code</p>
  
  <p>One path would be to create a custom search that has the right sql to contain only the most recent one. This is closest to what you want, but might have a performance impact (eg if you have loads of records/contacts) when civi builds the smart group</p>
  
  <p>Another one is to denormalise and create the same custom fields you have on the activity but add them (as read only) on the contact instead of the activity</p>
  
  <p>and then you write a hook so everytime a new activity is created with these custom fields, it would automatically duplicate them to the contact... or skip the duplicate custom fields on contact and directly add them to the right group if it's your only use case.</p>
  
  <p>So the activities contain all the history, but the latest version is always stored in the contact (or belong to the right group)</p>
  
  <p>This might get a little bit more tricky if you have to handle edge cases (eg what happens if you delete an activity? if you merge two contacts that have activities?)</p>
  
  <p>This might or might not be an issue in your case</p>
  
  <p>Another way to skin that cat would be to improve civirule to allow new triggers on custom fields.</p>
  
  <p>Good luck, writing an extension isn't that complicated if you know php/sql
  X+</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-11T08:43:13.690Z
Answers.LastActivityDate: 2019-10-11T08:43:13.690Z
Answers.UserDisplayName: Xavier
Body: <p>How could I search for X activity type custom field and only return the last activity created if the record has more than 1 X activity type?</p>
  
  <p>I envision creating a smart group out of this data.</p>
  
  <p>My use case is tracking user preferences and if they do a 180 degree on their preference, I would only want to consider their last preference change. I could use a static custom field but that will not track longitudinally. The worst case is I could store data in both and send data to both custom fields...</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-09T22:57:30.953Z
Comments.Text: Are you looking for a UI-only solution or is SQL ok?
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-09T22:59:15.737Z
Comments.Text: The goal is to keep a mailing group accurate automatically. So probably UI.
Comments.UserDisplayName: Andy Burns
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-09T23:00:58.303Z
Comments.Text: Civirules doesn't expose custom fields of activities so doesn't look like that is an option
Comments.UserDisplayName: Andy Burns
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-09T23:07:45.347Z
Comments.Text: If keeping the old activities is about tracking/logging, then you could turn on detailed logging and instead of creating new activities always update the old one. I'm not sure though how to enforce only having one activity of that type per person (for regular activities outside of civicase) without some code.
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-09T23:12:14.663Z
Comments.Text: Or conversely, create regular custom fields used for individuals and detailed logging would show changes to those fields? A detailed logging report might be able to show these changes?
Comments.UserDisplayName: Andy Burns
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-09T23:16:05.290Z
Comments.Text: Yes good thinking. It does show those.
Comments.UserDisplayName: Demerit
Tags: <activity>
Title: Search only the last activity created
ThreadText: # Search only the last activity created
  
  (Asked by: username_0 on 2019-10-09)
  
  How could I search for X activity type custom field and only return the last activity created if the record has more than 1 X activity type?
  
  I envision creating a smart group out of this data.
  
  My use case is tracking user preferences and if they do a 180 degree on their preference, I would only want to consider their last preference change. I could use a static custom field but that will not track longitudinally. The worst case is I could store data in both and send data to both custom fields...
  
  **Tags:** activity
  
  > **Comments:**
  > **username_1**: Are you looking for a UI-only solution or is SQL ok?  
  > **username_0**: The goal is to keep a mailing group accurate automatically. So probably UI.  
  > **username_0**: Civirules doesn't expose custom fields of activities so doesn't look like that is an option  
  > **username_1**: If keeping the old activities is about tracking/logging, then you could turn on detailed logging and instead of creating new activities always update the old one. I'm not sure though how to enforce only having one activity of that type per person (for regular activities outside of civicase) without some code.  
  > **username_0**: Or conversely, create regular custom fields used for individuals and detailed logging would show changes to those fields? A detailed logging report might be able to show these changes?  
  > **username_1**: Yes good thinking. It does show those.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-10-11. Score: 1 | Accepted answer
  
  I don't see a way of doing it without writing custom code
  
  One path would be to create a custom search that has the right sql to contain only the most recent one. This is closest to what you want, but might have a performance impact (eg if you have loads of records/contacts) when civi builds the smart group
  
  Another one is to denormalise and create the same custom fields you have on the activity but add them (as read only) on the contact instead of the activity
  
  and then you write a hook so everytime a new activity is created with these custom fields, it would automatically duplicate them to the contact... or skip the duplicate custom fields on contact and directly add them to the right group if it's your only use case.
  
  So the activities contain all the history, but the latest version is always stored in the contact (or belong to the right group)
  
  This might get a little bit more tricky if you have to handle edge cases (eg what happens if you delete an activity? if you merge two contacts that have activities?)
  
  This might or might not be an issue in your case
  
  Another way to skin that cat would be to improve civirule to allow new triggers on custom fields.
  
  Good luck, writing an extension isn't that complicated if you know php/sql
  X+
  
  
  ---
  
  