Misunderstanding $Name.contains

Hi,

I’m trying to run a simple action to test whether a $Name contains either of the strings (NW) or (SW), and then ticking or unticking $NWOnly and $SWOnly accordingly.

The code is:

if($Name.contains("\(NW\)"))
	{$NWOnly = true; $SWOnly = false};
if($Name.contains("\(SW\)"))
	{$NWOnly = false; $SWOnly = true};

I thought this should work, but it doesn’t… What am I doing wrong, please? (Something stupid, no doubt…)

Thanks!

Try,

if($Name.contains("\(NW\)")){
   $NWOnly = true; $SWOnly = false;};
if($Name.contains("\(SW\)")){
   $NWOnly = false; $SWOnly = true;};

The issue seems to be a line break inside the if(){... statement. I know there are a number of differing styles of laying out conditional code statements, but it seems–possible as a side effect of recent code parser improvements that not all such styles are understood. I generally used this form:

if(condition){
   // code
}else{
   // code
};

Indenting between the { ...} should not matter, not white space immediately before/after the curly braces. But, I think a line break before { is currently problematic.

1 Like

Yes, that was it!

I was baffled, because the .contains worked fine in an Agent Query, so I couldn’t see how it wouldn’t work in a stamp… but it was the if() that was causing the problems as you say.

Thanks again for being so quick and helpful!