I work on a .NET program retrieving some metadata of a given Salesforce org via the API.

I have already managed to retrieve a list of all standard and custom objects in my salesforce org via the REST API, so I know the names and labels of the objects and I also have a list of all the fields an object contains.

However, I could not find any way to retrieve the names of the FieldSets of an object and—more importantly—the fields that are contained in the FieldSets without having to implement another APEX class on the Salesforce org itself, as suggested in this answer.

How do I retrieve the FieldSets and their Fields of any object via the Salesforce API? I would prefer to do it via REST, but a solution via SOAP is fine if the former is not possible. More important to me is that there is minimum change done on the salesforce org itself, so that the program will "just work" with any org (setting up OAuth is fine, though).

  • Looks like you can get fieldsets using the tooling api, but not the members. – battery.cord Mar 6 at 14:52
  • Have you tried using the metadata api? reference – battery.cord Mar 6 at 14:57
  • @battery.cord I have tried to use it and I am able to login and create a new MetadataService object, but since there are very few code examples, I have no clue how to use the API to get the fieldsets of an object. – Danghor Mar 7 at 7:38
up vote 0 down vote accepted

As @battery-cord commented, you can start with a SOQL query against the Tooling API. Demonstrated here using the Developer Console, which uses the REST version of the Tooling API to run the query (Note the checked "Use Tooling API" option). You will want to filter on the EntityDefinitionId.

enter image description here

That will find you the FieldSet, but doesn't go so far as to enumerate the FieldSetItems. These are accessible via the Metadata API (again as noted by @battery-cord).

That's easily said, but required some perseverance to get going. It turns out all the work is getting the valid name to pass into readMetadata.

SalesforceSession salesforceSession = //... Get an active Session
MetadataServiceWrapper metadataService = salesforceSession.GetMetadataService();

SalesforceMetadata.Metadata[] md = metadataService.readMetadata("FieldSet", 
    new String[] { "DFB__Design__c.DFB__MyFieldSet", "Case.DFB__Case_Field_Set", "OpportunityLineItem.DFB__DropFieldSet" });
foreach (SalesforceMetadata.Metadata m in md)
{
    // You will get null Metadata back if the name is wrong.
    if (m != null && m is FieldSet)
    {
        FieldSet flow = (FieldSet)m;
        // Check collection size etc...
        FieldSetItem fsi = flow.availableFields[0];
    }
}

I'd initially thought I could pull the names directly from the following. However, the FileProperties.fullName didn't include the namespace of my dev org. As a result, I got an empty Metadata record back.

ListMetadataQuery query = new ListMetadataQuery();
query.type = "FieldSet";
FileProperties[] fps = metadataService.listMetadata(new ListMetadataQuery[] { query }, salesforceSession.ApiVersion);

As another approach, you might look at how SFDX handles and deploys FieldSets. Is is listed as a supported component type.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.