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
Vinay G 25Vinay G 25 

apex code to fetch the state based country picklist using cutsom object

I have problem in fetching state based on country selected in picklist.
1.I have configured default state and country picklist .
2.implemented a apex method to fetch states and country but not able map state based on country selected since i am using custom object.
Can please anyone help me to resolve this?
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vijay,

Country & state fields are picklist fields?

If yes, Then create field dependecy. here country is controlling field and state is dependent field.

Refer the link.
https://trailhead.salesforce.com/content/learn/projects/customize-a-salesforce-object/picklists-field-dependencies

If this helps, please mark it as best answer!!

Thanks!!
Vinay G 25Vinay G 25
Hi  Ankaiah ,
I know the field dependency configuration setup but in my case i am listing state and country dynamically with apex code and one more thing is the custom object value where I am storing country and state is type text not picklist.
One more constraint is on what basis can I configure field dependency?
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vinay,

State field should have one value or multiple values?

for example, I have selected country as India then what is state the value?

Thanks!!
Vinay G 25Vinay G 25
Hi  Ankaiah ,,
Basically I have configured the default state and country salesforce picklist configuration and used apex code to fetch country states.
to answer your question if i select india it should list all the states related to india.
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vinay,

I think your approach was not correct.

Better you can go with state as a picklist field and create a field dependecy.

While creating a record, When you select the country as India then state field should display all states and choose the state picklist value.

field depency:
Controlling field: country
Dependent field: State

User-added image

If you go with Apex code, you need to store the state values in apex code then you can populate them in state text field. In future if you want change some values then again you need to modify the values in apex code/trigger

let me know if you still need in apex code.

Thanks!!

 
Vinay G 25Vinay G 25
Hi Ankaiah ,
I am listing the state which are avialble in salesforce end itself not list any custom values.
Do I need to make both country and state as picklist type to configure field dependency?
It would be helpful if you are some sample code for this?
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vinay,

Do I need to make both country and state as picklist type to configure field dependency? Yes.

field depency:
Controlling field: country
Dependent field: State

User-added image

If this helps, Please mark it as best answer.

Thanks!!

 
Vinay G 25Vinay G 25
Hi Ankaiah,

When i am trying to change field type from text to picklist for custom object its expecting to provide values as below.
User-added image
But my values will be populating to picklist dynamicallly through apex code.
Can you please help is their any other alternative?
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vinay,

As per your issue description, country field is a picklist  data type right?

Sorry, I am not clear on your question.

Thanks!!
AnkaiahAnkaiah (Salesforce Developers) 
Refer the below link for the similar kind of ask.
https://salesforce.stackexchange.com/questions/65774/how-can-i-add-a-new-value-to-a-picklist-using-apex

Thanks!!
Vinay G 25Vinay G 25
Ankaiah 
actually in the custom object it of type text, but we have implemented a picklist field using visual force code and to that picklist I have written the apex code to fetch country and state which configured in salesforce 
My question was how to make this dependent is their any apex code where i can map state based country code ?
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vinay,

Refer the below link will help you to proceed further.
https://salesforce.stackexchange.com/questions/249304/how-to-create-dependent-picklist-in-vf-page

Thanks!!
Vinay G 25Vinay G 25
Hi Ankaiah ,
above resource is creating for dependent picklist using queries but  my problem is not what basis can I add condiction to filter states based on country selected.The resources is helpful but not getting how to validate the condition.
Or do we have any alternatives like creating one more custom object to store all country and state name , code and have lookup relationship will that work but using this approach i have problem how to load bulk data to that custom object?