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
jboardmanjboardman 

Change wherestr to allow an OR

I need a little help making a slight change to an apex class. 

Current code:

if (UnitType == 'New Unit')
       wherestr += 'And Family != \'Refurbished\' ';
   else if (UnitType == 'Refurbished Unit')
       wherestr += 'And Family = \'Refurbished\' ';

Need it to have an OR statement to exclude or include (respectively) a second product family of Spare Parts like whats below, but that isn't correct and is where I need help.

if (UnitType == 'New Unit')
      wherestr += 'And Family != \'Refurbished\'  OR \'Spare Parts\' ';
  else if (UnitType == 'Refurbished Unit')
       wherestr += 'And Family = \'Refurbished\' OR \'Spare Parts\' ';
Best Answer chosen by jboardman
GlynAGlynA
Beware!  

'AND (Family != \'Refurbished\' OR Family != \'Spare Parts\') '

will include all records because if it's "Refurbished", then it's not "Spare Parts" and vice versa.  It should be:

'AND (Family != \'Refurbished\' AND Family != \'Spare Parts\') '

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy@blogspot.com
Twitter: @GlynAtClosedWon

All Answers

jboardmanjboardman
Got some help, here's the result:

if (UnitType == 'New Unit')
    wherestr += 'AND (Family != \'Refurbished\' OR Family != \'Spare Parts\') ';
else if (UnitType == 'Refurbished Unit')
    wherestr += 'AND (Family = \'Refurbished\' OR Family = \'Spare Parts\') ';
GlynAGlynA
Beware!  

'AND (Family != \'Refurbished\' OR Family != \'Spare Parts\') '

will include all records because if it's "Refurbished", then it's not "Spare Parts" and vice versa.  It should be:

'AND (Family != \'Refurbished\' AND Family != \'Spare Parts\') '

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy@blogspot.com
Twitter: @GlynAtClosedWon
This was selected as the best answer
jboardmanjboardman
yes, thank you GlynA.  I was getting some results that I didn't expect in the query and your post fixes that issue.  Thanks!
GlynAGlynA
Glad it worked.  Could you mark my post as the "answer"?  This will mark the whole thread as "solved".  Thanks!

-Glyn