-
Notifications
You must be signed in to change notification settings - Fork 29
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
Issues with OR in Where clause #494
Comments
I would need to see a log generated by the rollup running without successfully updating things. I will say that you have a workaround at present, which is creating a formula field to contain the logic in this where clause and then using that formula field in the where clause instead. |
@jamessimone yeah formula fields seems to be only work around, tried adding extra brackets around the clause doesn't seem to help. Saw you added some fixes around OR clause, I am trying to replicate this using test class. Will update you once I have something |
@IsTest
static void shouldWorkMultipleOrInWhereClause() {
Account acc = [SELECT Id, Name FROM Account];
insert new List<Opportunity>{
new Opportunity(StageName = 'one', CloseDate = System.today(), AccountId = acc.Id, Name = 'one', Amount = 2),
new Opportunity(StageName = 'two', CloseDate = System.today(), AccountId = acc.Id, Name = 'two', Amount = 2),
new Opportunity(StageName = 'three', CloseDate = System.today(), AccountId = acc.Id, Name = 'three', Amount = 2),
new Opportunity(StageName = 'four', CloseDate = System.today().addDays(1), AccountId = acc.Id, Name = 'four', Amount = 2)
};
Test.startTest();
Rollup__mdt mdt = new Rollup__mdt(
CalcItem__c = 'Opportunity',
LookupObject__c = 'Account',
RollupFieldOnCalcItem__c = 'StageName',
LookupFieldOnCalcItem__c = 'AccountId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'SicDesc',
RollupOperation__c = 'LAST',
CalcItemWhereClause__c = '(StageName = \'one\' AND CloseDate = Today) OR (StageName = \'four\' AND CloseDate = Today)'
);
Rollup.performFullRecalculation(
mdt
);
Test.stopTest();
List<Opportunity> opps = Database.query('SELECT Id, StageName FROM Opportunity WHERE ' + mdt.CalcItemWhereClause__c);
System.assertEquals('one', opps[opps.size() - 1].StageName);
acc = [SELECT SicDesc FROM Account];
System.assertEquals('one', acc.SicDesc);
} I believe this should be able to replicate the issue |
@Avinava thanks for providing this repro - I must have missed the notification about your last comment. I'll have a look and let you know. |
@Avinava I was able to reproduce this issue and have fixed it. It will be go out in the next version of Apex Rollup to be released, With your current version, this will already work, but at the moment it might trigger a full recalc every time due to a related issue with query parsing. That's the fix that will be going out in the next version. |
…ex from running unexpectedly for nested OR clauses
@jamessimone thanks for the quick fix ! |
@jamessimone regarding the Date literal, I am just curious does capitalisation really matter with soql and dynamic query ? |
@Avinava SOQL is indeed case insensitive but apex rollup's internal classes are only partially SOQL-compliant with regards to sensitivity. I haven't expanded out the date literal support to be case insensitive. |
* Fixes #494 by updating where clause query parsing to prevent batch apex from running unexpectedly for nested OR clauses * Making some notes on WEEK_IN_YEAR date literal to come back to later * Optimizations, added a test on full recalc path to ensure parent reset processor only runs when it's supposed to * Fixes #500 by properly handling the stateful maintenance of previously reset parents when children span across multiple batches
@jamessimone Below is a test condition that should work @IsTest
static void shouldWorkWithINandOR() {
Account acc = [SELECT Id, Name FROM Account];
insert new List<Opportunity>{
new Opportunity(StageName = 'one', CloseDate = System.today(), AccountId = acc.Id, Name = 'one', Amount = 2, LeadSource = 'Web'),
new Opportunity(StageName = 'two', CloseDate = System.today(), AccountId = acc.Id, Name = 'two', Amount = 2, LeadSource = 'Phone'),
new Opportunity(StageName = 'three', CloseDate = System.today(), AccountId = acc.Id, Name = 'three', Amount = 2, LeadSource = 'Portal'),
new Opportunity(StageName = 'four', CloseDate = System.today().addDays(1), AccountId = acc.Id, Name = 'four', Amount = 2, LeadSource = 'Walk In')
};
Test.startTest();
Rollup__mdt mdt = new Rollup__mdt(
CalcItem__c = 'Opportunity',
LookupObject__c = 'Account',
RollupFieldOnCalcItem__c = 'StageName',
LookupFieldOnCalcItem__c = 'AccountId',
LookupFieldOnLookupObject__c = 'Id',
RollupFieldOnLookupObject__c = 'SicDesc',
RollupOperation__c = 'LAST',
CalcItemWhereClause__c = '(StageName = \'one\' AND LeadSource IN (\'Web\', \'Phone\') ) OR (LeadSource IN (\'Web\', \'Phone\') AND StageName != \'one\')'
);
Rollup.performFullRecalculation(mdt);
Test.stopTest();
List<Opportunity> opps = Database.query('SELECT Id, StageName FROM Opportunity WHERE ' + mdt.CalcItemWhereClause__c);
System.assertEquals('two', opps[opps.size() - 1].StageName);
acc = [SELECT SicDesc FROM Account];
System.assertEquals('two', acc.SicDesc);
// Validate that job ran as queueable - that the where clause was formatted correctly, in other words
System.assertEquals('Completed', [SELECT Status FROM AsyncApexJob WHERE JobType = 'Queueable' LIMIT 1]?.Status);
}
|
I will have a look! Thanks for bringing to my attention |
@Avinava I've identified where this issue is occurring and I'll be working on a fix for it. However, it's possible that in the meantime you can get this working yourself:
works, for instance. I am assuming these are simply example values, though? In any case, part of the current issue is that the LeadSource part of the clause is duplicated, and refactoring that out helps massively. I am going to continue to try to fix the actual underlying issue - just pointing out that depending on what your actual values are, you may have a path forward even before a fix is introduced. Thanks! |
We have been using apex-rollup for setting up rolls and it has been working great overall. However, we have encountered an issue where the filter criteria and rollup values are incorrect when OR statements are added in the WHERE clauses.
Example:
I am currently debugging this issue to identify the root cause, but I wanted to bring it to your attention as a heads up in case you were already aware of it
version : v1.5.84
The text was updated successfully, but these errors were encountered: