+ Start a Discussion
Jugbeer BholaJugbeer Bhola 

Loop through String <List> to pull values for parameters

Could someone explain how to process a list within a list in APEX code?

String List Example:

List<String>
{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI60856},
{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI54960},
{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI57656}

There would be three calls within a loop to a service that is expecting the parameters of AgentId and CustomerIds[].


Example:

AgentId = 'AI60856'
CustomerIds[] = '700011326', '507024842', '12345678'

AgentId = 'AI54960'
CustomerIds[] = '700011326', '507024842', '12345678'

AgentId = 'AI57656'
CustomerIds[] = '700011326', '507024842', '12345678'
Best Answer chosen by Jugbeer Bhola
LBKLBK
If your list is going to be like this,
List<String> lstStrings = new List<String> {
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI60856}',
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI54960}',
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI57656}'};
This following code will give you the results you are expecting.
List<String> lstStrings = new List<String> {
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI60856}',
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI54960}',
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI57656}'};
	Map<String, List<String>> mapStrings = new Map<String, List<String>>(); 
    for (String s : lstStrings){
        s = s.replace('],',']~').replace('{','').replace('}','');
        String[] aSplit1 = s.split('~');
        String sKey = '';
        List<String> lstValue = new List<String>(); 
        if(aSplit1.size() == 2){
            sKey = aSplit1[1].split('=')[1];
            lstValue = aSplit1[0].split('=')[1].replace(']','').replace('[','').split(',');
        }
        if(sKey != ''){
            mapStrings.put(sKey, lstValue);
        }
    }
for(String sKey : mapStrings.keySet()){
    System.debug('AgentId : ' + sKey);
    System.debug('CustomerIds : ' + mapStrings.get(sKey));
}
Let me know if this helps.

All Answers

LBKLBK
If your list is going to be like this,
List<String> lstStrings = new List<String> {
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI60856}',
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI54960}',
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI57656}'};
This following code will give you the results you are expecting.
List<String> lstStrings = new List<String> {
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI60856}',
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI54960}',
'{AccountCustomerNumbers=[700011326, 507024842, 12345678], AgentId=AI57656}'};
	Map<String, List<String>> mapStrings = new Map<String, List<String>>(); 
    for (String s : lstStrings){
        s = s.replace('],',']~').replace('{','').replace('}','');
        String[] aSplit1 = s.split('~');
        String sKey = '';
        List<String> lstValue = new List<String>(); 
        if(aSplit1.size() == 2){
            sKey = aSplit1[1].split('=')[1];
            lstValue = aSplit1[0].split('=')[1].replace(']','').replace('[','').split(',');
        }
        if(sKey != ''){
            mapStrings.put(sKey, lstValue);
        }
    }
for(String sKey : mapStrings.keySet()){
    System.debug('AgentId : ' + sKey);
    System.debug('CustomerIds : ' + mapStrings.get(sKey));
}
Let me know if this helps.
This was selected as the best answer
Jugbeer BholaJugbeer Bhola
THANK YOU!