You need to sign in to do that
Don't have an account?
roni shore
how to call custom setting in if condition
Hi guys- I am writing a piece of code that will exclude countries form validation if those are present in custom setting, but m getting null pointer exception on that.
Map<String, Map<String, String>> mapCountry = new Map<String, Map<String, String>>();
for(State__c st : [Select id, Name, Country__r.Name from State__c]) {
if(!mapCountry.containsKey(st.Country__r.Name)) {
Map<String, String> mapST = new Map<String, String>();
mapCountry.put(st.Country__r.Name, mapST);
}
mapCountry.get(st.Country__r.Name).put(st.Name, st.Name);
}
//quering some account here
for(Account acc : accountList){
billingCountry = acc.BillingCountry;
// Check Country & State is valid
if(!mapCountry.containsKey(acc.BillingCountry)) {
errorFlag = true;
errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.Bad_Country_Value);
ApexPages.addMessage(errorMsg);
} else if(!mapCountry.get(acc.BillingCountry).containsKey(acc.BillingState)) {
errorFlag = true;
errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.Bad_Value);
ApexPages.addMessage(errorMsg);
}
In this second else if condition i have to check if the countries are in custom setting (bypass_counry__c) in the field (country__c) then bypass validation.
PLease suggest
Map<String, Map<String, String>> mapCountry = new Map<String, Map<String, String>>();
for(State__c st : [Select id, Name, Country__r.Name from State__c]) {
if(!mapCountry.containsKey(st.Country__r.Name)) {
Map<String, String> mapST = new Map<String, String>();
mapCountry.put(st.Country__r.Name, mapST);
}
mapCountry.get(st.Country__r.Name).put(st.Name, st.Name);
}
//quering some account here
for(Account acc : accountList){
billingCountry = acc.BillingCountry;
// Check Country & State is valid
if(!mapCountry.containsKey(acc.BillingCountry)) {
errorFlag = true;
errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.Bad_Country_Value);
ApexPages.addMessage(errorMsg);
} else if(!mapCountry.get(acc.BillingCountry).containsKey(acc.BillingState)) {
errorFlag = true;
errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.Bad_Value);
ApexPages.addMessage(errorMsg);
}
In this second else if condition i have to check if the countries are in custom setting (bypass_counry__c) in the field (country__c) then bypass validation.
PLease suggest
Try this.
else if(!mapCountry.get(acc.BillingCountry).containsKey(acc.BillingState) && !bypass_counry__c.getAll().values().contains(acc.BillingCountry)) {
errorFlag = true;
errorMsg = new ApexPages.Message(ApexPages.Severity.ERROR, System.Label.Bad_Value);
ApexPages.addMessage(errorMsg);
}