function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Drees WollschlagerDrees Wollschlager 

SQOL query to filter information

Hello everyone, I want to create a SOQL query to solve an issue I have with a field that I want to filter information from. The field records names of people in the record of this custom object. The issue is, that when there are 3 records in which the field for the first record is filled with "X", the second with "X, Y" and the third with "X, Y, Z", the data is difficult to use for reports/dashboards as it gives you three different records rather than recognising 3 records for X, 2 for Y and 1 for Z. 

Essentially I want to create a SOQL Query that pulls all records from the database in which the records field contains X and then repeat that query for every single individual, so I have a singular number for every individual.

The first issue I ran into is that I cannot use the CONTAINS operator in SQOL. Can I instead use LIKE %X%? Does anyone have experience with a process like this? Am I on the right track with my idea?

Any feedback or ideas would be greatly appreciated. 
Abhishek BansalAbhishek Bansal
Hi Drees,

Yes, you can use LIKE operator in SOQL which behaves similar to the CONTAINS. It should be enclosed within the string literals like this '%X%'.
So if your field name is Name__c then query should be Select Name__c from YOUR_OBJECT__C where Name__c Like '%X%'.
Let me know if you need any further information on this.

Thanks,
Abhishek Bansal.
Drees WollschlagerDrees Wollschlager
Hi Abishek,

thank you so much for your response. I will need try to build the query for one individual and get it working, but the issue is going to be doing it for every individual in the organisation and then to make that data usable. I might have to create a field on every users profile and populate it with the data from the query. First, I will read more into SOQL and how to properly utilise it. Hopefully, later today I will be a little bit further on this.

Kind regards,
Drees
Drees WollschlagerDrees Wollschlager
When I put in 

Select name, id, names_attended__c From Campaign where names_attended__c = 'John Doe'

it shows all records in which John Doe is the only person in the field. Records in which John Doe AND other individuals appear, do not show up. When I try to change the logical operator to remedy this from an equal sign to like/contains/includes it does not work. I have tried the name with Like '%John Doe%' and all other combinations I could imagine. This article https://help.salesforce.com/articleView?id=000334304&type=1&mode=1 (https://help.salesforce.com/articleView?id=000334304&type=1&mode=1" style="color:blue; text-decoration:underline) and the one linked at the bottom of this article say when it is a picklist field, I should use include and can put multiple answers in such as (John Doe;Jana Doe) etc., which does not help me with my issue at all.
Abhishek BansalAbhishek Bansal
Hi Dress,

What is the data type for your field? And can you please add some records here with the values and what are your expected results so that I can give you the SOQL. 

Thanks,
Abhishek Bansal.
Drees WollschlagerDrees Wollschlager
Hi Abishek, 

it is a multi-select picklist. The names of the individuals are always written in the same way, as they are chosen from the available list and brought to the chosen list with the arrows. Any number or combinations of names can be chosen and put into the field which appear like this: John Doe; Maria Baum;Alex Smith

As in my above commentary, the command 
Select name, id, names_attended__c From Campaign where names_attended__c = 'John Doe' 

selects all records in which the field only has the name John Doe. If there was a record that would be "John Doe;Alex Smith" it would not show up with this command. I want to build an SOQL that instead of the logical operator equals (=) uses contains/like/includes or something similar, so that records like "John Doe;Alex Smith" or "Maria Baum;Alex Smith;John Doe" are all counted, as the name John Doe is featured in them. 

Ideally, I would be able to repeat this with every individual in the organisation, as I want to have the total number of records that feature any individual. I would like to create a new field on the user's profile that keeps track of this number or at least is populated (and thus updated) when the query is run manually to gain the information.

Kind regards and thank you for your help,
Drees