0

I am trying to implement the below code, but I get this error:

OR operator can only be applied to Boolean expressions

Code:

public class Segment {
    static List<Map<String, Object>> jsonMAP = new List<Map<String, Object>>();
    public string    SegName         {get; set;}
    public string    jsonstring = ' ';
    public integer   List_id         {get; set;}
    public string[]  conditionsARRAY {get; set;}
    public string    customfield     {get; set;}
    public string    fieldname       {get; set;}
    public string    operator        {get; set;}
    public string    age = '0';
    public string    andor = 'and';
    public void Maker(){

        for(integer i=0; i<=99; i++){
            if(i == 0||4||8||12||16||20||52||56||60||64||68||72){ //<--Line 15 with error
                customfield = 'Type';
                fieldname   = 'Buyer';
                operator    = 'eq';
            }
            if(i == 24||28||32||36||40||44||48||76||80||84||88||92){
                customfield = 'Type';
                fieldname   = 'Seller';
                operator    = 'eq';

            }
            if(i == 1||5||9||13||17||21||25||29||33||37||41||45||49||53||57||61||65||69||73||77||881||85||89||93||97){
                customfield = 'Age';
                if(i)
                fieldname   = age;
                operator    = 'eq';
            }
            if(i == 2||6||10||14||18||22||26||30||34||38||42||46||50||54||58||62||66||70||75||78||82||86||90||94||98){
                customfield = 'zAuto';
                fieldname   = '1';
                operator    = 'eq';
            }
            if (i == 3||7||11||15||19||23||27||31||35||39||43||47||51||55||59||63||67||71||76||77||83||87||91||95||99){
                customfield = 'DateAdded';

            }
            if (customfield == 'DateAdded'){operator='lt'; andor=' '; fieldname = age;}

        Map<String, Object> conditions = new Map<String, Object>{
            'field'   => customfield,  //custom field to filter by
            'value'   => fieldname,
            'operator'=> operator,
            'and_or'  => andor
        }; jsonmap.add(conditions);

        }
        conditionsARRAY = json.serialize(jsonmap);
        jsonstring = '{"name":"' + SegName + '","list_id":' + List_id+ ',"conditions":'+ conditionsARRAY +'}';

       } 


}
  • 3
    You will have to get funky with math class, use sets and check if contains or something else. It expects i=1 || i=2 || i=3 as you cannot use 1||2||3||4 unless i was a boolean and you were actually OR'ing the values which it does not appear you are trying to do – Eric Sep 21 '17 at 4:21
  • 1
    Just so you know, you can select a whole code block and indent it properly if you just hit the {} button in your editor. Alternatively, hit CTRL + K (⌘ + K on a mac). – Adrian Larson Sep 21 '17 at 4:31
  • Also you have a random seeming if (i) clause in the middle of all that which may give you separate issues... – Adrian Larson Sep 21 '17 at 4:34
5

You have two basic options:

  • Use the Set.contains method
  • Join complete logical clauses

The first strategy would look like:

Set<Integer> multiplesOf4 = new Set<Integer>{0,4,8,12,...};

for (Integer i = 0; ...)
{
    if (multiplesOf4.contains(i))
    {
        // do stuff
    }
}

The second strategy would look like:

if (i == 0 || i == 4 || i == 8 || i == 12 || i == ...)
{
}
1

Just for fun, if you squint your eyes, the provided code kinda looks like FizzBuzz.

Using sets (as per Adrian's example) is probably the way to go here...but I'm not writing this answer to be efficient...just purely logical (with a bit of whimsy thrown in for good measure).

Since all of your target values in a given group are always multiples of 4 away from another one, you could simplify your expressions a bit using Math.mod().

// This syntax is invalid, no matter how much we would wish it to be valid
//if(i == 0||4||8||12||16||20||52||56||60||64||68||72){

// This is equivalent
if((i <= 20 || (i >= 52  && i <= 72) && Math.mod(i,4) == 0)){

using that same idea, we can convert your other statements as well

//if(i == 24||28||32||36||40||44||48||76||80||84||88||92){
if((i >= 24 && i <= 48 || i >= 72 && i <= 92) && Math.mod(i, 4) == 0){

//if(i == 1||5||9||13||17||21||25||29||33||37||41||45||49||53||57||61||65||69||73||77||81||85||89||93||97){
if(Math.mod(i, 4) == 1){

//if(i == 2||6||10||14||18||22||26||30||34||38||42||46||50||54||58||62||66||70||75||78||82||86||90||94||98){
if(Math.mod(i, 4) == 2){

//if (i == 3||7||11||15||19||23||27||31||35||39||43||47||51||55||59||63||67||71||76||77||83||87||91||95||99){
if(Math.mod(i, 4) == 3){

Ok, so that wasn't as bad as I thought it might be at first. One aspect of being a good programmer is being able to see patterns like this. The simpler the pattern, the simpler the code (can be). Notice how when we broke the pattern in the first 2 conditions, the if statement got longer. At some point, it would definitely be better to use sets instead of Math.mod().

In this example, using sets would lead to faster execution...but with you only looping 100 times, the difference would be so negligible as to not be worth consideration.

Another thing that I see you doing a lot in your questions is using repeated if() statements where else or else if() would be more appropriate. In your example code, there is no situation where you would want to enter another conditional block after you enter one of them. Using else if and else will save a negligible amount of CPU time, but the more important thing is that using else if and else mean that you won't find yourself entering more than one of those conditional blocks.

Doing that could help make your code more robust. It also might mask some logical errors (which could be a good thing, or a bad thing, depending on your perspective).

// Better implementation.
// Code is probably the shortest possible for the situation (though not the fastest
//   running)
if((i <= 20 || (i >= 52  && i <= 72) && Math.mod(i,4) == 0)){
    // some code
} else if((i >= 24 && i <= 48 || i >= 72 && i <= 92) && Math.mod(i, 4) == 0){
    // some code
} else if(Math.mod(i, 4) == 1){
    // some code
} else if(Math.mod(i, 4) == 2){
    // some code
} else if(Math.mod(i, 4) == 3){
    // some code
} else {
    // some code for what should happen if none of the conditions are met.
    // This else block is optional, but I'd argue that explicitly stating what
    //   happens in every possible case is good practice.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.