certinia / apex-mdapi

Apex Wrapper for the Salesforce Metadata API
BSD 3-Clause "New" or "Revised" License
689 stars 976 forks source link

valueSetDefinition returning null value for valueSet on a field with a global picklist #267

Closed Picksley closed 3 years ago

Picksley commented 3 years ago

Hi,

For the use cases where we would use mdapi to retrieve all picklistValues on a custom field. All of the examples are using the deprecated picklistValues code, whereas today we would need to use valueSet.

Currently, I need to return a list of picklistValues (they're a global value set) related to a custom field. The service has been instantiated and everything has been deployed using the github loader.

The code I believe should work is

 MetadataService.CustomField customField = (MetadataService.CustomField) service.readMetadata(
'CustomField', new String[] {'Opportunity.Area_of_Work2_Taxonomy__c' }
).getRecords()[0];

List<MetadataService.CustomValue > pickVals = new List<MetadataService.CustomValue >();
for (MetadataService.CustomValue  pk : customField.valueSet.valueSetDefinition.value ) {
   //pickVals.add(pk);
  System.debug('CS: Individual PK ' + pk );
 }

However, when outputting this information. A null item is returned for customField.valueSet.valueSetDefinition.

System debug of customField.valueSet

ValueSet:[apex_schema_type_info=(http://soap.sforce.com/2006/04/metadata, true, false),
controllingField=null,
controllingField_type_info=(controllingField, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false),
field_order_type_info=(controllingField, restricted, valueSetDefinition, valueSetName, valueSettings),
restricted=true,
restricted_type_info=(restricted, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false),
**valueSetDefinition=null,** 
valueSetDefinition_type_info=(valueSetDefinition, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false), 
valueSetName=Pitch_category,
valueSetName_type_info=(valueSetName, http://soap.sforce.com/2006/04/metadata, null, 0, 1, false),
valueSettings=null,
valueSettings_type_info=(valueSettings, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false)]

Why is this code failing? Is there something else in the documentation that suits the request better?