How to reflect saved search relationship on updating a contact?

Here is one scenario

  • i have a saved search based on a tag and have results for that on infusion
  • i have pulled all these saved search based contacts and stored at my end. i keep a reference of saved search id for the contact.
  • then i go into one such contact on infusion and remove the tag. so for future results, this contact won’t show up for this saved search result.
  • but there is no way i can get this reflected at my end unless I make fresh api call for this saved search.
  • the problem with fresh api call is there can be 30K records for a saved search and it would take some time to update at my end. Somehow i need to figure out the missing contacts and remove that saved search reference at my end.

Is there any better and faster way to reflect this at my end.

Hi @Chitta_Pattnaik, I would recommend using REST Hooks to listen for when tags are removed from a contact. The event key that you’ll need to listen for is contactGroup.removed. The REST Hook payload will contain the contact id and tag ids that were removed.

Thanks Nicholas. I am already using this contactGroup.removed and it updates correctly at my end.

The problem is it’s not the only criteria that the saved search has, there may be multiple tags and some other fields set like partial match for first name or last name. Lets say there are couple of tags selected for the saved search. Both were assigned to a contact but one is removed from the contact now. The contact will still be listed under the saved search as there is another tag which matches the criteria. So on removing a tag on infusion, I can’t detach the contact from saved search at my end.

There may be some other criteria changes which will add or remove contacts from the saved search result. Apart from this, a contact details has been modified so it may or may not be listed under the saved search result. If there can be a way to pass saved search id and a contact id to the API and it will return true or false depending on whether the contact will be listed under the saved search or not, then that will do the job for me.

As of now I can only think of making fresh api call to get all contacts for the selected saved search id. I hope I have put this in a clean manner.

I think I would maintain a db of the saved search and run it in a queue every hour or so. Using the contact.updated or contactgroup.applied may allow you to remove any contacts that no longer qualify for the search criteria and running the saved search periodically would allow you to audit the results.

Otherwise, you might as well just maintain the information with rest hooks based on your criteria.

Given the number of attributes involved, I don’t think you can get more realtime than that.

Yes, I do have a db table of the saved searches and I use the saved search infusion id to pull related contacts from API.

The API returns 1K records at a time and there is no specific ordering of the records. If my saved search has 30K records and my new contact id is on the last page, then I will be unnecessarily waiting for 29K records to be processed. Processing from the beginning has advantage as it may pull new records but can be heavy at times.

And it’s not always the contact data those get updated and will determine whether it will be listed under the saved search or not but also the saved search criteria itself can change resulting some contacts included and some excluded from the result.

There is a query order by method that you can sort the results by the last updated field

Sorting option is not available for getting records from a saved search. This is what the API has in order to pull records from a saved search

public function getSavedSearchResults($savedSearchId, $userId, $pageNumber, $returnFields)

There is no method such as you are describing to do that operation under the Legacy XML-RPC API, and we are not providing enhancements to the functionality of that API at this point. Additionally, in-app saved searches are not on our current roadmap to replicate in the REST API.

1 Like

In regards to the sorting option you mentioned. In the Saved Search you have set up, click on the Column you want to Sort, so that you have the results in Descending Order. Now save that Search again, so that it preserves the sorting order.

When you run the Saved Search via the API again, it will appear in that order. Then within your code you can track the latest entries, which will appear in the first page of the results. No need to go through all the results each time.

Hope that helps.

1 Like

Apologies for this late reply.

Thank you for this approach but unfortunately this won’t help in my case. If few of the contacts get updated, then it will follow the search criteria upto some extent. But sometimes contacts are being imported and sometimes they are also mass updated. So in such cases it may or may not follow the search criteria.

There are cases when the search condition is updated and in that case the contact records will mostly be different. Like few days back another condition was added to the criteria which brought down the records count from 27K to 1.7K.

I have set up jobs to pull these records based on different pages and there are multiple workers which process these jobs. I have a flag value for the search id and contact id relationship which is reset just before calling the api. The flag value for new records or existing records in the latest saved search gets updated. Once all the jobs are completed, I check for records which do not have the updated flag value and simply remove that relationship. Now I am left with only those contacts which are there in the latest saved search result.

Search criteria is applied to whatever contacts are present in the app. How they got there has absolutely no bearing on how the saved search/report functions.