[SalesForce] How to Search Workflow Rules or Validation Rules etc in Apex i.e. Metadata Search via Apex

Recently I'm working on building something for developers/administrators – a handy 100% force.com app allowing to search metadata. Currently this app allows you to quickly search (from sidebar) custom code components (Pages, Apex Classes, Components and Apex Triggers).

Now I'm trying to add feature to search workflows or validations or email templates or fields etc, but stuck with some apex limitations. There's no APEX or direct api (methods) to search(query) above components. What are the options here for me?

  1. Use Metadata WSDL, parse it as apex class and then use that to retrieve/search the necessary component list ??
  2. Use Metadata REST API ??

  3. Crawler in VF using JS (just a wild hacky thought, not even sure if this's possible)

Best Answer

UPDATE: 11th Nov, I discovered the WorkflowRule (as apposed to the Workflow) metadata type! Combined with the ValidationRule metadata type. This means that the Metadata listMetadata API call now returns exactly the two lists you need to implement your search tool.

This will allow you to list the Validation Rules and Workflow Rules from Apex.

MetadataService.MetadataPort service = MetadataServiceExamples.createService();     
List<MetadataService.ListMetadataQuery> queries = new List<MetadataService.ListMetadataQuery>();        
MetadataService.ListMetadataQuery queryWorkflow = new MetadataService.ListMetadataQuery();
queryWorkflow.type_x = 'WorkflowRule';
queries.add(queryWorkflow);     
MetadataService.ListMetadataQuery queryValidationRule = new MetadataService.ListMetadataQuery();
queryValidationRule.type_x = 'ValidationRule';
queries.add(queryValidationRule);           
MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, 25);
for(MetadataService.FileProperties fileProperty : fileProperties)
    System.debug(fileProperty.fullName);

For more information and the MetadataService and MetadataServiceExamples class used in the above code. Please take a look at this Github repo and Readme file.

Related Topic