function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aruna Dhavaleswarapu 4Aruna Dhavaleswarapu 4 

Help on Javascript button code

I have a situation where I am trying to check when the country(picklist) is "United States" or "Mexico" or "Brazil", the state cannot be blank.
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/20.0/apex.js")} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js')} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js')} 

country = '{!JSENCODE(TEXT(Account.Country__c))}';
state ='{!JSENCODE(TEXT(Account.State__c))}';

if (state != ""  && !!(country=='United States' || country=='Mexico' || country=='Brazil')){
continue with the process}
}else 
alert('State is required for conversion of this account.'); 


My condition is throwing me alert even if my country is not any of these.
Thanks in advance.

Aruna
 
Best Answer chosen by Aruna Dhavaleswarapu 4
Raj VakatiRaj Vakati
{!REQUIRESCRIPT("/soap/ajax/42.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/42.0/apex.js")} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js')} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js')} 
 
 
var country= '{!JSENCODE(Text(Account.Country__c))}';


var state = '{!JSENCODE(Text(Account.State__c))}';

alert ( country) ; 
alert ( state ) ;

if ( (country=='United States' || country=='Mexico' || country=='Brazil' ) &&  
( state=== undefined || state == null || state.length <= 0)) {
    alert('State is required for conversion of this account.'); 

}else{
alert("working");

}

 

All Answers

Raj VakatiRaj Vakati
{!REQUIRESCRIPT("/soap/ajax/42.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/42.0/apex.js")} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js')} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js')} 


var country ="{!Account.Country__c}";
var state ="{!Account.State__c}";
alert ( country) ; 
alert ( state ) ;

if ( (country=='United States' || country=='Mexico' || country=='Brazil' )  &&  
( state!="")) {
    alert("working");
}else{

alert('error');

}
 

 
Aruna Dhavaleswarapu 4Aruna Dhavaleswarapu 4
Hi Raj 
I still see the same issue.
Country and State both are picklist values which contain some special characters, so to avoid errors I am using JSENCODE
Raj VakatiRaj Vakati
Below code is working in my instance .. can you share the error message screenshot to see the issue. 

 
{!REQUIRESCRIPT("/soap/ajax/42.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/42.0/apex.js")} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js')} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js')} 



var country= '{!JSENCODE(Text(Account.Country__c))}'; 


var state = '{!JSENCODE(Text(Account.State__c))}'; 

alert ( country) ; 
alert ( state ) ; 

if ( (country=='United States' || country=='Mexico' || country=='Brazil' ) && 
( state!="")) { 
alert("working"); 
}else{ 

alert('error'); 

}

 
Aruna Dhavaleswarapu 4Aruna Dhavaleswarapu 4
It does not show any error but if the country is China, it still alerts me to fill the state 
Raj VakatiRaj Vakati
{!REQUIRESCRIPT("/soap/ajax/42.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/42.0/apex.js")} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js')} 
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js')} 
 
 
var country= '{!JSENCODE(Text(Account.Country__c))}';


var state = '{!JSENCODE(Text(Account.State__c))}';

alert ( country) ; 
alert ( state ) ;

if ( (country=='United States' || country=='Mexico' || country=='Brazil' ) &&  
( state=== undefined || state == null || state.length <= 0)) {
    alert('State is required for conversion of this account.'); 

}else{
alert("working");

}

 
This was selected as the best answer
Aruna Dhavaleswarapu 4Aruna Dhavaleswarapu 4
Thank you Raj
Aruna Dhavaleswarapu 4Aruna Dhavaleswarapu 4
Hi Raj
In the same button, I am also checking for various conditions where I have to see that TaxId cannot be blank for a list of countries.

if (!(taxid == '' && (country == 'Netherlands Antilles' || country == 'Austria' || country == 'Belgium' || country == 'Bulgaria' || country == 'Cyprus' || country == 'Czech Republic' || country == 'Germany' || country == 'Denmark' || country == 'Estonia' || country == 'Spain' || country == 'Finland' || country == 'France' || country == 'United Kingdom' || country == 'French Guiana' || country == 'Greece' || country == 'Croatia' || country == 'Hungary' || country == 'Ireland' || country == 'Italy' || country == 'Lithuania' || country == 'Luxembourg' || country == 'Latvia' || country == 'Malta' || country == 'Netherlands' || country == 'Poland' || country == 'Portugal' || country == 'Romania' || country == 'Sweden' || country == 'Slovenia'))) 

Is there a better way I can write this condition.
Thanks in advance