Field Validation not firing as expected

Posted by ByronB on 14-Jul-2015 03:22

Hi guys

So I have a field validation:

/*if('{!isvCountries}'==='' || '{!isvCountries}' === null){
  if('{!worldwide}' === 'false'){
	return 'At least one Country needs to be selected!';	    
  }
} else{
  return null;
}*/


/*if ('{!worldwide}' === 'false' && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){
  return 'At least one Country needs to be selected!';
}*/

/*if (!{!worldwide} && ('{!isvCountries}'.length >= 1 || '{!isvCountries}' === null)){
  return 'At least one Country needs to be selected!';
}*/

if (!{!worldwide} && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){
  return 'At least one Country needs to be selected!';
}


No I have tried every variation I can think of, debugging works fine. The result is exactly as expected. However, when creating a new record validation fires regardless of whether there is a value in countries or not. Seems strange to me.

All Replies

Posted by Santosh Patel on 14-Jul-2015 03:40

A comprehensive javascript empty string check is as simple as 

if (!'{!isvCountries}') {
...
}

Your code probably fails for the case when the value is 'undefined' which is different from null.

Your final code could be...

if (!{!worldwide} && !'{!isvCountries}'.trim()){
  return 'At least one Country needs to be selected!';
}

Posted by ByronB on 14-Jul-2015 03:43

Thanks for that mate, still getting the validation firing after selecting a country

Posted by Santosh Patel on 14-Jul-2015 04:35

Could you use the logging helpers and log the values in the validation code, to get a feel of the actual values when the problem occurs?

Posted by ByronB on 14-Jul-2015 05:03

Using the field validations field debugger I get the following:

When worldwide is selected:

Countries:

Worldwide: true

When worldwide is not selected and a country exists:

Debug

Countries: 23404

Worldwide: false

Result

NULL

Posted by Santosh Patel on 14-Jul-2015 05:18

I tried your scenario and it seems to work fine. Could you revisit everything once?

What I tried: checkbox worldwide field, country picklist

- validation trigger code is

if (!{!worldwide} && !'{!country}'){

 return 'At least one Country needs to be selected!';

}

When worldwide selected, even if the country is not set, the record gets saved. Otherwise throws the validation error message.

P.S: I just realized my suggested code has .trim() which is wrong(fails) when no country is selected.

Posted by ByronB on 14-Jul-2015 08:53

Hi Santosh

I have literally copied and pasted your code and changed the country to isvCountries:

/*if('{!isvCountries}'==='' || '{!isvCountries}' === null){

 if('{!worldwide}' === 'false'){

return 'At least one Country needs to be selected!';    

 }

} else{

 return null;

}*/

/*if ('{!worldwide}' === 'false' && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){

 return 'At least one Country needs to be selected!';

}*/

/*if (!{!worldwide} && ('{!isvCountries}'.length >= 1 || '{!isvCountries}' === null)){

 return 'At least one Country needs to be selected!';

}*/

/*if (!{!worldwide} && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){

 return 'At least one Country needs to be selected!';

}*/

/*if (!{!worldwide} && !'{!isvCountries}'.trim()){

 return 'At least one Country needs to be selected!';

}*/

if (!{!worldwide} && !'{!isvCountries}'){

return 'At least one Country needs to be selected!';

}

I works when debugging perfectly, but client side its just not firing correctly

Posted by ByronB on 15-Jul-2015 07:31

Bump, anything else?

Posted by Harrie Kuijper on 15-Jul-2015 07:50

'{!isvCountries}' === null

can never be true, since:

'' === null = false

'null' === null = false

so if you place the not (!), the message will be shown because it turns to true.

if(!('{isvCountries}'))

can only be true when isvCountries is '' (empty string)

try '{!isvCountries}' != ''

Posted by ByronB on 15-Jul-2015 08:06

Nah mate, not working. validation doesnt fire at all. Even in debugging now its not firing...

if (!{!worldwide} && '{!isvCountries}' != ''){

return 'At least one Country needs to be selected!';

}

Posted by Harrie Kuijper on 15-Jul-2015 08:27

Okay.. For our understanding. What is your usecase? When should this error be fired? If "country is checked and isvCountries has not a value selected" the statement should be:

if('{!worldwide#value}' == 'true' && '{!isvCountries}' == '') { // error message}

{!workdwide#value} gives true or false

{!worldwide} gives the html representation of the checkbox. <-- think that is the cause

Posted by ByronB on 15-Jul-2015 09:05

Ok, let me explain rather. Yes the use case is as you say, when worldwide is NOT checked and there is NO countries selected then throw validation error. Easy enough. The previous code worked perfectly through the debugger in every scenario but when going client side the validation would fire as expected for the above use case, however it would also fire when there was one or more countries selected and worldwide was unchecked. see below

if ('{!worldwide}' === 'false' && ('{!isvCountries}'==='' || '{!isvCountries}' === null)){

 return 'At least one Country needs to be selected!';

}

Your suggestion behaves in the same manner:

if ('{!worldwide#value}' == 'false' && '{!isvCountries}' == ''){

return 'At least one Country needs to be selected!';

}

Posted by Santosh Patel on 15-Jul-2015 10:50

I tried the same with relationship fields (as you mentioned countrie(s) ). Have attached a demo app for the purpose as tried out on Rollbase public cloud. And it seems to work as you desire.

Can you try out this app and see if it works as expected? The Company object is what you would create and select countries for the ISV Countries field. Selecting no country and unselecting worldwide throws the desired error. With countries selected the record saves fine.

Please confirm with this app.[View:~/cfs-file.ashx/__key/communityserver-discussions-components-files/25/Demo-App_5F00_v2.xml:550:0] 

Posted by ByronB on 20-Jul-2015 02:39

Thanks Santosh

Can you have a look at the screenshot and explain why the debugger is reporting no value for {!countries} token? This explains why the trigger is firing the way it is.

Posted by ByronB on 20-Jul-2015 03:08

And whats even stranger is that I imported your app and the validation fired as expected, using the exact same code on my object it doesnt work (obviously because the countries token is not being passed correctly).

Posted by ByronB on 28-Jul-2015 11:34

Ok, I think we have found the problem. The issue arises when we have renamed the integration name of the field. With the Demo App supplied above the only difference between the 2 applications was the fact that they demo app above was using the default integration name "RXXXX" for the Countries field. When I changed the integration name of the demo  field the validation would not fire client side (the string token is actually empty). Going back to our applications, as soon as I changed the integration name to its original "Rxxxxx" the validation fires.

Now this isnt a problem in the current scenario but presents a big problem throughout our other applications with all the custom coding that has been done. Looks like a bug?

This thread is closed