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
Ritesh001Ritesh001 

how to convert soql result to Json format using wrapper class

Need Urgent Help in converting the soql result to get as per the below json format as shown in image.


List<Custom_Shipping__mdt> = [selelct Country__c,State__c,Countrycode__c,Sattecode__c from Custom_Shipping__mdt];

example soql result-
Coutry - India , Country code - 34 , State - MH,Delhi State code - 44 ,45
Country Code & Satte Code - 
34-44 , 34-45
This query is giving me all the data from custom metadata
Need to Convert the country & state with their code in below Json format  using wrapper & pass to lightining component.

User-added image
MKRMKR
Hi,

Try creating a wrapper class with two attributes:
public class CustomSelectOption {
   
    @AuraEnabled
    public String label { get; set; }

    @AuraEnabled
    public String value { get; set; }

}
Then create AuraEnabled method to create a map as requested:
@AuraEnabled
public static Map<String, Object> getCountryAndProvinceOptions() {
    Map<String, Object> returnMap = new Map<String, Object>();
    List<Custom_Shipping__mdt> customShippingMetadata = [select Country__c,State__c,Countrycode__c,Sattecode__c from Custom_Shipping__mdt];

    Map<String, List<CustomSelectOption>> countryProvinceMap = new Map<String, List<CustomSelectOption>>();
    List<CustomSelectOption> countryOptions = new List<CustomSelectOption>();
    for(Custom_Shipping_mdt customShippingRecord : customShippingMetadata) {
        if(!countryProvinceMap.containsKey()) {
            countryProvinceMap.put(customShippingRecord.Countrycode__c, new List<CustomSelectOption>());

            CustomSelectOption countryOption = new CustomSelectOption();
            countryOption.label = customShippingRecord.Country__c;
            countryOption.value = customShippingRecord.Countrycode__c;
            countryOptions.add(countryOption);
        }

        CustomSelectOption provinceSelectOption = new CustomSelectOption();
        provinceSelectOption.label = customShippingRecord.State__c;
        provinceSelectOption.value = customShippingRecord.Sattecode__c;
        countryProvinceMap.get(customShippingRecord.Countrycode__c).add(provinceSelectOption);
    }
    
    returnMap.put('countryProvinceMap', countryProvinceMap);
    returnMap.put('countryOptions', countryOptions);
    return returnMap;
}

You cannot create the Javascript functions directly in Apex. Instead you need to add getProvinceOptions and getCountryOptions in Lightning controller.

Regards,
Mkr
Ajay K DubediAjay K Dubedi
Hi Ritesh,
You need to read about the JSON class of Salesforce through this you can create the desire JSON.
Here is the link of JSON Class:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm

https://salesforce.stackexchange.com/questions/126212/how-to-convert-list-data-into-json-in-apex

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

Thanks,
Ajay Dubedi