+ Start a Discussion
NitishMishraNitishMishra 

How to Access dependent picklist(MailingState) in apex ?

Hi, 
I have enabled state and picklist value in client's unlimited org. I need to access only dependent state based on selected Country code. There are some integration and other apex changes where I need only dependent state value in a Map( like  : 'MH'=> 'Maharastra')... I am using below code but It will give all the state values. I need only states based on country values. e.g. : For Indian it should have only 35 state which belong to India only. 
Schema.DescribeFieldResult F = Contact.MailingStateCode.getDescribe();
List<Schema.PicklistEntry> P = F.getPicklistValues();

System.debug('-----P--->>'+P[0]);
Map<String, String> tempMap = new Map<String, String>();
for(Schema.PicklistEntry sp : P){
    tempMap.put(sp.getValue(), sp.getLabel());
}

System.debug('----tempMap-----'+tempMap.get('MH'));  // Here it is returning 'Meath' as MH will be state code for some other states as well 
System.debug('----tempMapVALUE-----'+tempMap.get('UP'));

Any help will be appreciated. Thanks in Adance !!
 
Best Answer chosen by NitishMishra
Praful GadgePraful Gadge

Hi Nitish,

I faced same scenarion long time back. I came across an amazing solution written by geek like us.

Please refer this (http://salesforce.stackexchange.com/a/4876)
You can find a code for this here (https://gist.github.com/boxfoot/4166342)

This can be used only if you are using a page for this functionality, you can't use it in trigger/batch.

Its very easy to use, you just need to update field API names here. Please implement and let me know if you face any issue.

If this post is your solution, kindly mark this as the solution to the post so that others may benefit.

Regards,
Praful G.

All Answers

Praful GadgePraful Gadge

Hi Nitish,

I faced same scenarion long time back. I came across an amazing solution written by geek like us.

Please refer this (http://salesforce.stackexchange.com/a/4876)
You can find a code for this here (https://gist.github.com/boxfoot/4166342)

This can be used only if you are using a page for this functionality, you can't use it in trigger/batch.

Its very easy to use, you just need to update field API names here. Please implement and let me know if you face any issue.

If this post is your solution, kindly mark this as the solution to the post so that others may benefit.

Regards,
Praful G.
This was selected as the best answer
NitishMishraNitishMishra
Thanks Praful.. I had also found the same link which resolved my problem.. !