Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow "not like" statements to be used with other filters #32

Merged
merged 1 commit into from
Mar 13, 2024

Conversation

AndrewJarrett
Copy link
Contributor

Hi - first, thank you for sharing your blog and this code, it has been really helpful for our team in reducing test execution time and improving code coverage.

We noticed an issue where we were converting a large query that used multiple "not like" statements in it that the query was not valid. The issue seems to be that the correct syntax for a "not like" statement is to have it be wrapped in parentheses so multiple where clauses will work correctly.

For example, the following code would generate a working query:

Query accountQuery = Query.notLike(Account.Name, '%someName%');
List<Account> accounts = (List<Account>) accountRepo.get(accountQuery);
SELECT Id, Name FROM Account WHERE NOT Name LIKE '%someName%'

However, it seems that if we add any other criteria to the query then it will result in a non-valid SOQL query. This seems to be the case if we add any other filter criteria to the where clause, not just if we use multiple "not like" filters.

Query accountQuery = Query.andQuery(new List<Query> {
    Query.notLike(Account.Name, '%someName%'),
    Query.notLike(Account.Name, '%otherName%')
});
List<Account> accounts = (List<Account>) accountRepo.get(accountQuery);
SELECT Id, Name FROM Account WHERE NOT Name LIKE '%someName%' AND NOT Name LIKE '%otherName%'

Instead, the solution seems to be that if we wrap all "not like" queries in parentheses and format the string to include the field name and the value, then we could generate a working query like the one below:

SELECT Id, Name FROM Account WHERE (NOT Name LIKE '%someName%') AND (NOT Name LIKE '%otherName%')

I have written a basic unit test and updated the other two unit tests to expect the added parentheses. The unit tests passed when I ran the updated code in a personal dev sandbox.

Let me know if you have any comments or suggested changes to this!

Copy link
Owner

@jamessimone jamessimone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - thank you for submitting!

@jamessimone jamessimone merged commit b8dc989 into jamessimone:main Mar 13, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants