IF ELSE with String

I have the variable ${valor_body_decodifiacdo} that receives the value of a Json object Evalute.
I created an IF ELSE as follows:
IF “$valor_body_decoded” == “Unfortunately I can’t help you with that”
I can either receive the text Unfortunately, I can’t help you with that or any other text. However, this is how the IF is, if I receive the text Unfortunately, I cannot help you with that, it does not enter the IF.

I need a comparison that works for both cases.

Just as a first check have you printed out what that variable value is? To ensure when it is your expected value for the condition that it is in fact formatted and reads the same? For when you expect it to step into it.

It’s also hard to see how formatted your IF block, but you’d hope to to some form of exception thrown if it’s not formatted correctly etc… providing a code snippet could be useful and the full block

You could also wrap your variable in triple double quotes to oppose to one set of single double quotes.

Yes, I printed it and the value is correct. It seems to me that it is a variable type conflict. When ${valor_body_encoded} receives something other than Unfortunately I can’t help you with that, it receives a list.
However, I can’t find a solution for comparing IF that solves both cases.
I’ll try your triple quote idea.

It be the double quotes to why its not evlauating but given you said it can also be of type list, then this could be a possibel solution:

${type}=        Evaluate    type($valor_body_decoded).__name__
IF    "${type}" != "list" and $valor_body_decoded == "Unfortunately I can’t help you with that"
    # do something
END

Hope that helps :slight_smile:

Sorry probably best just ensuring its type str instead of list, just change it to this:

"${type}" == "str"

Hi,

From what I understand the compared string is not the same, so 1st problem is here:

  • Unfortunately, I can’t help you with that
  • Unfortunately, I cannot help you with that

So to pass in IF you would have two conditions.
You could do:

VAR   ${STRING1}    Unfortunately, I can’t help you with that
VAR   ${STRING2}    Unfortunately, I cannot help you with that
IF    ${valor_body_decoded} in [${STRING1}, ${STRING2}]
# actions

Then second issue seems to be the difference in variable (string or list), but this might comes from the json extraction.
There maybe you could try something like this:

VAR   ${STRING1}    Unfortunately, I can’t help you with that
VAR   ${STRING2}    Unfortunately, I cannot help you with that
FOR    ${string}   IN    ${STRING1}    ${STRING2}    
    ${hascondition}   Evaluate  ${string}  in ${valor_body_decoded}
    IF    ${hascondition}    BREAK
END
IF    ${hascondition}
   # actions

This would allow veryfying condition in both case, variable or list.

Regards
Charlie

2 Likes

Kudos, I do like this better and you’ve got the “IN” and both strings :clap:

Probably could had met more of what was asked… I did rush to throw an answer out there, but that’s the joy of coding there is so many different approaches.

1 Like

That’s what worked for me. I created two variables, and the variable that receives the list, I converted to String. And then comparing the two variables, the IF started to work correctly. Thank you, you helped a lot.

1 Like