You need to sign in to do that
Don't have an account?
Tomeka Wray
Picklist Field Validation and Apex Code
I have a Apex class that automatically creates a lead when certain conditions apply to case from another Org. However, the state field is a picklist and somestimes the field values from the sending org are not always in the receiving orgs picklist. How can I validate that the State received is actually in the picklist.
My initial thought was to create a string array with all of the valid values and compare the state against that, but if the picklist changes, then my code will not be accurate. Any suggestions would be much appreciated.
My initial thought was to create a string array with all of the valid values and compare the state against that, but if the picklist changes, then my code will not be accurate. Any suggestions would be much appreciated.
I understood that in the options map you are able to get all the picklist values present in the stateCode.(All countries).
Your query is how to limit it for 'US'.
I got this reference to check the ISO-3166 standard state/country codes.
https://help.salesforce.com/HTViewHelpDoc?id=admin_state_country_picklists_standard_countries.htm&language=en_US (https://help.salesforce.com/HTViewHelpDoc?id=admin_state_country_picklists_standard_countries.htm&language=en_US" target="_blank)
with this i made a check in the for loop to the picklist values to the options map . see updated code below. Let me know if it helps or you want any other.
Please mark this as best answer to help others in future.
Thanks and Regards,
Sai Krishna Tavva.
All Answers
Edit the picklist field, set the check box labeled Strictly enforce picklist value true to prevent the values otherthan the picklist values. see
below screenshot for more info.
1. Edit the picklist field . (Eg: here in this screenshot Primary is the custom field type picklist.)
2. check the checkbox as highlighted in the below screenshot.
Then it only checks the picklist values,other than the picklistvalues will not be considered.
Please let me know if you have any queries. Also mark it as best answer if this answers to your question as it may helpful to others.
Thanks and Regards,
Sai Krishna Tavva.
Firstly, sorry for delay.
You can get the picklist values of a field by using the below lines
Here you will get the all the picklist values in a Schema.PicklistEntry class data type. then you can convert it to a Map of string,string by using this below code.
In the options Map you will have all the picklist values. Here you can check the values by this map.
Let me know if you have any quires.
Thanks and Regards,
Sai Krishna Tavva.
I hope state field is custom field.if you need to use the api name of the field (state__c). I have a picklist field in my lead object whose API name is "Authority__c". I run the below code and it worked fine for me.
List<Schema.PicklistEntry> fieldResult = Lead.Authority__c.getDescribe().getPicklistValues();
system.debug('States = ' + fieldResult);
Map<string,string> options = new Map<string,string>();
for( Schema.PicklistEntry f : fieldResult )
{
options.put(f.getLabel(),f.getLabel());
system.debug('State =' + f.getLabel());
}
Debug Logs :
see the below image to view the debug logs.
Can you post the debug logs/Error you are facing.
waiting for your reply.
Thanks and Regards,
Sai Krishna Tavva.
By your comment as below "but the state field is part of the Address type, which is a compound field". I see that the state field which is of part of Address standard field is not the picklist type. as it is not the picklist type you cant use the above field. Let me know if i am wrong.
if this is the case we need to store all the state field values as a record in custom settings and do a check with the record before saving.
Let me know if any
Regards,
Sai Krishna Tavva.
I understood that in the options map you are able to get all the picklist values present in the stateCode.(All countries).
Your query is how to limit it for 'US'.
I got this reference to check the ISO-3166 standard state/country codes.
https://help.salesforce.com/HTViewHelpDoc?id=admin_state_country_picklists_standard_countries.htm&language=en_US (https://help.salesforce.com/HTViewHelpDoc?id=admin_state_country_picklists_standard_countries.htm&language=en_US" target="_blank)
with this i made a check in the for loop to the picklist values to the options map . see updated code below. Let me know if it helps or you want any other.
Please mark this as best answer to help others in future.
Thanks and Regards,
Sai Krishna Tavva.
I hope doing well!!. if you get answer to this question please mark it as best answer as it may useful to others. if you have any queriers/issues let me know.
Thanks and Regards,
Sai Krishna Tavva.