Issue with If statement in Trigger

I have created a trigger with several if statements, which has been working perfectly. However due to some changes in the business logic, a new field was added and needs to be included in the trigger.

The if statement I’m updating now, doesn’t seem to work as intended, but I fail to see where the problem lies.

Basically, I have 3 fields:

Checkbox field: Branding

Checkbox field: Form completed

Picklist field: Customer Type <– this is the new field

Previously, the following worked perfectly:

if(obj.Branding__c == true && obj.FormCompleted__c == false){
   Do Something;
}

Now the business logic specifies that for 2 customer types, the above should not apply (Form is not required), so I updated the if statement to:

if(obj.Branding__c == true && obj.FormCompleted__c == false && (obj.CustomerType__c != 'Type1' || obj.CustomerType__c != 'Type2')){
   Do Something;
}

Would anyone be able to identify the cause of my issue? To me it appears correct, but I’ve been looking at it too much to see the problem I guess.

Thanks in advance,
Aaron

Answer

It is always either not one or not the other. Change your evaluation to:

static Set<String> whitelistedTypes = new Set<String> { 'Type1', 'Type2' };
// use the above in your Boolean evaluation:
if (obj.Branding__c && !obj.FormCompleted__c &&
    !whitelistedTypes.contains(obj.CustomerType__c))
{
    // do stuff
}

Attribution
Source : Link , Question Author : Aaron , Answer Author : Adrian Larson

Leave a Comment