Play Games

Search This Blog

Saturday, November 20, 2021

How to parse the data stored as map(key,value) in custom Label in Apex - Salesforce Globe For You

How to parse the data stored as map(key,value) in custom Label in Apex - Salesforce Globe For You

Problem: Assume that the data in the custom label stored as shown below

key1:value1;key2:value2;key3:value3

Now get the value from custom label and prepare map.

Solution: Get the value from Label and then split using ';' and store it in string array.Now iterate that array and do split using ':' and finally prepare map.

Example: The MapValues label is created as shown below


Sample Code:

List<string> lstValues = system.label.MapValues.split(';');

Map<String,string> mapValues = new Map<String,string>();

for(String val : lstValues){

    list<String> lstVal = val.split(':');

    mapValues.put(lstVal[0],lstVal[1]);

}

system.debug('mapValues:'+mapValues);

system.debug('keyset:'+mapValues.keyset());

system.debug('values:'+mapValues.values());

Output:



No comments:

Post a Comment