You need to sign in to do that
Don't have an account?
Michael J. Lupino
If Condition is null being ignored
Good evening. I'm seeing an odd behavior with the code. It appears my if conditions are being ignored for if blocks that involve VariableA, VariableB an VariableC if the system reports back as null. It seams to treat NULL as a string. The code is designed to scale the where clause based on if a value is not null. If the value is null, i want it to be excluded from the conditions.add commands which therefore would prevent it from being written into StrQuery. If the value is not null, I want it to be included in the conditions.add commands.
Here's an example:
If VariableA,B &C contains data the conditions list would report back 3 + 1 for recordid being automatically added
If VariableA,B contans data, the conditions list would report back 2 + 1 for recordid being automatically added
If VariableA contains data (B,C are null) then the list would report back 1 + 1 for recordid being automatically added.
Ideally I'm looking for the code to work for any combination of A,B,C (eg. B,C, C,A etc.). This makes up 3 conditions to the third power or nine combinations.
My code is below:
String strQuery = 'select id from objectname__c ';
String VariableA;
String VariableB;
String VariableC;
List<string> conditions = new List<string>();
String Esc = '\'';
conditions.add('RecordID =' + Esc +RecordID+ Esc +' ');
system.debug('the filtered value of RecordID is:' + RecordID);
//adds VariableA when VariableA not null
if(VariableA != null) {
conditions.add('FieldA__c ='+ '' + Esc + VariableA + Esc);
system.debug('Added Value from Field A: ' + conditions);
}
//adds VariableB when VariableB not null
if (VariableB != null) {
conditions.add('FieldB__c =' + '' + Esc +VariableB + Esc);
system.debug('Added Value from Field B: ' + conditions);
}
//adds VariableC when VariableC is not nuull
if (VariableC != null) {
conditions.add('FieldC__c=' + Esc +VariableC + Esc);
system.debug('Added Value from Field C: '+ '' + conditions + Esc);
}
system.debug('Conditions Size is:' +conditions.size()); /number of conditions added
//////// determines what to add to SOQL query statement
if (conditions.size() > 0) {
strQuery += 'WHERE ' + conditions[0];
for (Integer i = 1; i < conditions.size(); i++)
strQuery += 'AND ' + conditions[i];
}
system.debug('the list of conditions' + conditions);
system.debug('the final query' + strQuery);
Here's an example:
If VariableA,B &C contains data the conditions list would report back 3 + 1 for recordid being automatically added
If VariableA,B contans data, the conditions list would report back 2 + 1 for recordid being automatically added
If VariableA contains data (B,C are null) then the list would report back 1 + 1 for recordid being automatically added.
Ideally I'm looking for the code to work for any combination of A,B,C (eg. B,C, C,A etc.). This makes up 3 conditions to the third power or nine combinations.
My code is below:
String strQuery = 'select id from objectname__c ';
String VariableA;
String VariableB;
String VariableC;
List<string> conditions = new List<string>();
String Esc = '\'';
conditions.add('RecordID =' + Esc +RecordID+ Esc +' ');
system.debug('the filtered value of RecordID is:' + RecordID);
//adds VariableA when VariableA not null
if(VariableA != null) {
conditions.add('FieldA__c ='+ '' + Esc + VariableA + Esc);
system.debug('Added Value from Field A: ' + conditions);
}
//adds VariableB when VariableB not null
if (VariableB != null) {
conditions.add('FieldB__c =' + '' + Esc +VariableB + Esc);
system.debug('Added Value from Field B: ' + conditions);
}
//adds VariableC when VariableC is not nuull
if (VariableC != null) {
conditions.add('FieldC__c=' + Esc +VariableC + Esc);
system.debug('Added Value from Field C: '+ '' + conditions + Esc);
}
system.debug('Conditions Size is:' +conditions.size()); /number of conditions added
//////// determines what to add to SOQL query statement
if (conditions.size() > 0) {
strQuery += 'WHERE ' + conditions[0];
for (Integer i = 1; i < conditions.size(); i++)
strQuery += 'AND ' + conditions[i];
}
system.debug('the list of conditions' + conditions);
system.debug('the final query' + strQuery);
VariableA, VariableB and VariableC are strings. Can you please try VariableA.isEmpty() instead of null.
Thanks
Anupama
All Answers
VariableA, VariableB and VariableC are strings. Can you please try VariableA.isEmpty() instead of null.
Thanks
Anupama