0

I am trying to update a picklist values through REST API, I'am having a problem getting the list of values so I can add new picklist values.

I'am having an error

System.TypeException: Invalid conversion from runtime type List to List<Map<String,ANY>>

in:

List<Map<String, Object>> valueMap = (List<Map<String, Object>>)  valueSetDefinitionMap.get('value');enter code here

Here is my code:

Set<String> newValues = new Set<String>{'Alpha','Omega','Beta'};

//GET TOKEN
HttpRequest req = new HttpRequest();
req.setMethod('POST');   
req.setEndpoint('https://test.salesforce.com/services/oauth2/token');

String CLIENT_ID = 'XXXXXXXXXXXXX';
String CLIENT_SECRET = 'XXXXXXXXXXXXX';
String USERNAME = 'XXXXXXXXXXXXXXX';
String PASSWORD = 'XXXXXXXXXXXXXX';

req.setBody('grant_type=password' + '&client_id='+CLIENT_ID + 
        '&client_secret='+CLIENT_SECRET + '&username='+USERNAME + '&password='+PASSWORD);

Http http = new Http();
HTTPResponse response = http.send(req);
System.debug('Body ' + response.getBody());
System.debug('Status ' + response.getStatus());
System.debug('Status code ' + response.getStatusCode());

Map<String, Object> tokenResult = (Map<String, Object>) 
JSON.deserializeUntyped(response.getBody());


//GET EXISTING PICKLIST METADATA

HttpRequest request = new HttpRequest();
request.setEndpoint('instance/services/data/v41.0/tooling/sobjects/CustomField/id');
request.setHeader('Authorization','Bearer ' + tokenResult .get('access_token'));
request.setMethod('GET');
HttpResponse res = http.send(request);
// If the request is successful, parse the JSON response.

 

if(res.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());

//Get Metadata
Map<String, Object> metadataMap = (Map<String, Object>) results.get('Metadata');

//Get valueSet
Map<String, Object> valueSetMap = (Map<String, Object>)  metadataMap.get('valueSet');

//Get valueSetDefinition
Map<String, Object> valueSetDefinitionMap = (Map<String, Object>)  
valueSetMap.get('valueSetDefinition');

//I'am trying to get the value list so I can add new picklist values
List<Map<String, Object>> valueMap = (List<Map<String, Object>>)  
valueSetDefinitionMap.get('value');

}

I hope someone encountered this before and be able to help. Thank you.

1 Answer 1

0

As the error says, this conversion is invalid, first convert to list and then maybe iterate over the list and convert to Map like below

List<Object> values = (List<Object>) valueSetDefinitionMap.get('value'); 
for(Object val: values) {
    Map<String, Object> mapVal = (Map<String, Object>)val;
    System.debug(mapVal);
}
                                        
                                        
     

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .