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
Sai ThejaSai Theja 

How to parse multi select picklist to array in apex

Below is my code:

Location__c loc = [SELECT Id,Name,Company__r.company_id__c,Location_Address__c,Location_City__c,Location_Zip__c,Location_State__c,Location_Participation_Method__c from Location__c where id = 'a0P2C000001KkEa'] ;  
JSONGenerator gen = JSON.createGenerator(true);  
    Decimal companyID = loc.Company__r.Company_id__c;


    gen.writeStartObject();      
    gen.writeStringField('location_name',loc.name);
    system.debug(loc.Company__r.company_id__c);
    system.debug('https://api-qa.retrotax-aci.com/companies/'+CompanyID+'/locations');
    // gen.writeStringField('Company_id',String.valueOf(loc.Company__r.company_id__c));
    gen.writeStringField('address_line_1',loc.Location_Address__c);
    gen.writeStringField('sf_id',loc.id);
    gen.writeStringField('city',loc.Location_City__c );
    gen.writeStringField('zip_code',loc.Location_Zip__c );
    gen.writeStringField('state_code',loc.Location_State__c);
    gen.writeSrtingField('participation_methods', loc.Location_Participation_Method__c); 
gen.writeEndObject();    
    String jsonS = gen.getAsString();
System.debug('Location Data'+jsonS);

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api-qa.retrotax-aci.com/companies/'+CompanyID+'/locations');
request.setMethod('POST');
request.setHeader('X-AUTH-TOKEN','xxxxxxxxxxx);
request.setHeader('X-API-KEY', 'xxxxxxxxxxxx');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody(jsonS );
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() == 200) {
Location__c result = (Location__c) JSON.deserialize(response.getBody(), Location__c.class);
System.debug('Location:'+result.id);
loc.Location_Id__c = Decimal.valueOf(result.id);
update loc;

    System.debug('Location Created Successfully: ' +
        response.getStatusCode() + ' ' + response.getBody());
} else {
    System.debug('Unable to create Location: ' +
        response.getStatusCode() + ' ' + response.getBody());
     
}

Here "Loaction_Participation_method__c"  is MutliSelect Picklist.
i need to get these values in array format i.e. (["Combo","Paper"]).
below is my result:
User-added image
Best Answer chosen by Sai Theja
Maharajan CMaharajan C
Hi Sai,

try the below one:

Location__c loc = [SELECT Id,Name,Company__r.company_id__c,Location_Address__c,Location_City__c,Location_Zip__c,Location_State__c,Location_Participation_Method__c from Location__c where id = 'a0P2C000001KkEa'] ;  
JSONGenerator gen = JSON.createGenerator(true);  
    Decimal companyID = loc.Company__r.Company_id__c;

    String lpm1 = '';
    for(String str : loc.Location_Participation_Method__c.split(';'))
    {
        String lpmstr = '"' +  str  + '"';
        lpm1 = lpm1 + lpmstr + ',' ;
    }
    String lpm = '[' + lpm1.removeEnd(',')  + ']';

    system.debug('Str ==> ' + lpm );
    
    gen.writeStartObject();      
    gen.writeStringField('location_name',loc.name);
    system.debug(loc.Company__r.company_id__c);
    system.debug('https://api-qa.retrotax-aci.com/companies/'+CompanyID+'/locations');
    // gen.writeStringField('Company_id',String.valueOf(loc.Company__r.company_id__c));
    gen.writeStringField('address_line_1',loc.Location_Address__c);
    gen.writeStringField('sf_id',loc.id);
    gen.writeStringField('city',loc.Location_City__c );
    gen.writeStringField('zip_code',loc.Location_Zip__c );
    gen.writeStringField('state_code',loc.Location_State__c);
    //gen.writeSrtingField('participation_methods', loc.Location_Participation_Method__c);
    gen.writeStringField('participation_methods', lpm ); 
gen.writeEndObject();    
    String jsonS = gen.getAsString();
System.debug('Location Data'+jsonS);

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api-qa.retrotax-aci.com/companies/'+CompanyID+'/locations');
request.setMethod('POST');
request.setHeader('X-AUTH-TOKEN','xxxxxxxxxxx);
request.setHeader('X-API-KEY', 'xxxxxxxxxxxx');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody(jsonS );
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() == 200) {
Location__c result = (Location__c) JSON.deserialize(response.getBody(), Location__c.class);
System.debug('Location:'+result.id);
loc.Location_Id__c = Decimal.valueOf(result.id);
update loc;

    System.debug('Location Created Successfully: ' +
        response.getStatusCode() + ' ' + response.getBody());
} else {
    System.debug('Unable to create Location: ' +
        response.getStatusCode() + ' ' + response.getBody());
     
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Sai,

try the below one:

Location__c loc = [SELECT Id,Name,Company__r.company_id__c,Location_Address__c,Location_City__c,Location_Zip__c,Location_State__c,Location_Participation_Method__c from Location__c where id = 'a0P2C000001KkEa'] ;  
JSONGenerator gen = JSON.createGenerator(true);  
    Decimal companyID = loc.Company__r.Company_id__c;

    String lpm1 = '';
    for(String str : loc.Location_Participation_Method__c.split(';'))
    {
        String lpmstr = '"' +  str  + '"';
        lpm1 = lpm1 + lpmstr + ',' ;
    }
    String lpm = '[' + lpm1.removeEnd(',')  + ']';

    system.debug('Str ==> ' + lpm );
    
    gen.writeStartObject();      
    gen.writeStringField('location_name',loc.name);
    system.debug(loc.Company__r.company_id__c);
    system.debug('https://api-qa.retrotax-aci.com/companies/'+CompanyID+'/locations');
    // gen.writeStringField('Company_id',String.valueOf(loc.Company__r.company_id__c));
    gen.writeStringField('address_line_1',loc.Location_Address__c);
    gen.writeStringField('sf_id',loc.id);
    gen.writeStringField('city',loc.Location_City__c );
    gen.writeStringField('zip_code',loc.Location_Zip__c );
    gen.writeStringField('state_code',loc.Location_State__c);
    //gen.writeSrtingField('participation_methods', loc.Location_Participation_Method__c);
    gen.writeStringField('participation_methods', lpm ); 
gen.writeEndObject();    
    String jsonS = gen.getAsString();
System.debug('Location Data'+jsonS);

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api-qa.retrotax-aci.com/companies/'+CompanyID+'/locations');
request.setMethod('POST');
request.setHeader('X-AUTH-TOKEN','xxxxxxxxxxx);
request.setHeader('X-API-KEY', 'xxxxxxxxxxxx');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody(jsonS );
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() == 200) {
Location__c result = (Location__c) JSON.deserialize(response.getBody(), Location__c.class);
System.debug('Location:'+result.id);
loc.Location_Id__c = Decimal.valueOf(result.id);
update loc;

    System.debug('Location Created Successfully: ' +
        response.getStatusCode() + ' ' + response.getBody());
} else {
    System.debug('Unable to create Location: ' +
        response.getStatusCode() + ' ' + response.getBody());
     
}

Thanks,
Maharajan.C
This was selected as the best answer
Sai ThejaSai Theja
@maharajan 
USER_DEBUG   "participation_methods" : "[\"Paper\",\"Combo\"]"
Sai ThejaSai Theja
Thanks Maharajan 
 .split(;) works. I figured out the solution.


Location__c loc = [SELECT Id,Name,Company__r.company_id__c,Location_Address__c,Location_City__c,Location_Zip__c,Location_State__c,Location_Participation_Method__c from Location__c where id = 'a0P2C000001KkEa'] ;  
JSONGenerator gen = JSON.createGenerator(true);  
    Decimal companyID = loc.Company__r.Company_id__c;
List<String> participation_method_list = new List<String>();
for (integer i=0; i<loc.Location_Participation_Method__c.split(';').size(); i++) {
    system.debug(i); 
    participation_method_list.add(loc.Location_Participation_Method__c.split(';')[i]);
}


system.debug(loc.Location_Participation_Method__c.split(';').size());

system.debug(loc.Location_Participation_Method__c.split(';'));
gen.writeStartObject();      
gen.writeStringField('location_name',loc.name);
    system.debug(loc.Company__r.company_id__c);
    system.debug('https://api-qa.retrotax-aci.com/companies/'+CompanyID+'/locations');
    // gen.writeStringField('Company_id',String.valueOf(loc.Company__r.company_id__c));
    gen.writeStringField('address_line_1',loc.Location_Address__c);
    gen.writeStringField('sf_id',loc.id);
    gen.writeStringField('city',loc.Location_City__c );
    gen.writeStringField('zip_code',loc.Location_Zip__c );
    gen.writeStringField('state_code',loc.Location_State__c);
  
    gen.writeObjectField('participation_methods', participation_method_list);
system.debug(participation_method_list);
gen.writeEndObject();    
    String jsonS = gen.getAsString();
System.debug('Location Data'+jsonS);

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api-qa.retrotax-aci.com/companies/'+CompanyID+'/locations');
request.setMethod('POST');
request.setHeader('X-AUTH-TOKEN','xxxxxxxx');
request.setHeader('X-API-KEY', 'xxxxxxxxx');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody(jsonS );
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() == 200) {
Location__c result = (Location__c) JSON.deserialize(response.getBody(), Location__c.class);
System.debug('Location:'+result.id);
//loc.Location_Id__c = Decimal.valueOf(result.id);
//update loc;

    System.debug('Location Created Successfully: ' +
        response.getStatusCode() + ' ' + response.getBody());
} else {
    System.debug('Unable to create Location: ' +
        response.getStatusCode() + ' ' + response.getBody());
     
}
Maharajan CMaharajan C
Okays. I already have this approach for you. The below code i used.

String[] strs = new String[]{};
for(String str : loc.Location_Participation_Method__c.split(';'))
{
    strs.add(str);
}
JSONGenerator gen = JSON.createGenerator(true);  
gen.writeStartObject();      
        gen.writeObjectField('participation_methods', strs ); 
gen.writeEndObject();    
String jsonS = gen.getAsString();

System.debug('Location Data'+jsonS );

Please Mark the best answer which helps to you.

Thanks,
Maharajan.C