1

I am not sure what I am missing, I am trying to execute something only if the value of 2 parameters are not empty. So if 1 or both are empty, excecute code a, if both values are not empty, excecute code B. My code looks like this:

system.debug('@@ val1: ' + val1 + ' val2: ' + val2);
if(val1 != '' || val2 != ''){
    //CODE A
    //CODE: I dont understand why it is never getting here, even if either or both values are empty
}else{
    //CODE B
}

my debug log print

DEBUG|@@ val1:  val2:

It is wrong in apex to have a conditional OR ?

2

possibly the variables are holding null or white space instead of empty string.. Its always better to use the string methods isBlank , isNotBlank

if(String.isNotBlank(val1)  || String.isNotBlank(val2)){
    //CODE A
}else{
    //CODE B
}

oh and it seems you want CODE A to run if one or both the values are empty..

then you just need to check for == instead of != (or use isBlank method instead of isNotBlank method)

if(String.isBlank(val1)  || String.isBlank(val2)){
    //CODE A
}else{
    //CODE B
}
| improve this answer | |

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.