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
Vivek GuptVivek Gupt 

How to get state list by country using apex

Hi Community,

I need help to get state list by country using apex. 
Thanks
KrishnaAvvaKrishnaAvva
Hi Vivek,

I am assuming that you are using standard State and country codes. Please find the solution here:
https://help.salesforce.com/articleView?id=000338321&type=1&mode=1

Please mark this as SOLVED, if it had helped you. Thanks

Regards,
Krishna Avva
Vivek GuptVivek Gupt

Hi Krishna,

Thanks for your answer. 

I know this solution & used before to get states but now i'm looking to get state code with the country code. Any other help will appreciable.

Regards,

Vivek Gupta 

Ajay K DubediAjay K Dubedi
Hi Vivek,

First of all you should have knowlegde about REST api integration. and this code get state by pincode:
 
public class PincodeGet
{
    
    public static List<JSON2Apex> getNameById(String pincode){
        try{
            system.debug('enter apex class');
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint('https://api.postalpincode.in/pincode/'+pincode);
            request.setMethod('GET');
            HttpResponse response = http.send(request);
            system.debug('response1 :: '+response.getStatusCode());
            List<JSON2Apex> j2s = new List<JSON2Apex>();
            if(response.getStatusCode() == 200)
            {
                j2s = (List<JSON2Apex>) JSON.deserialize( response.getBody(), List<JSON2Apex>.class); 
                return j2s;
            }
        }catch(Exception exp){
            System.debug('Exception Cause-->>'+exp.getMessage() + 'Line Number-->>'+exp.getLineNumber());
        }
        return null;
    }
     
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com