I am trying to get the count of all the components available in the org using Metadata API. But when I try to get the count of flows, it gives me the total count of visual flow plus process builder. I came across this document and I think I need to specify processType to differentiate between the flows. I am having issue trying to figure out how to use this in my code. Any idea or help please let me know. Below is my method

public Integer getSizeofComponents(String metaType){
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();

        List<MetadataService.ListMetadataQuery> queries = new List<MetadataService.ListMetadataQuery>();    
        MetadataService.ListMetadataQuery queryLayout =new MetadataService.ListMetadataQuery();
        queryLayout.folder = '';
        queryLayout.type_x = metaType;
        if(metaType == 'flow')
        system.debug('metaType ----------->> ' + metaType);
        MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, 40);
        if(metaType == 'flow')
        system.debug('fileProperties ----------->> ' + fileProperties);
        if(fileProperties != null){
            return fileProperties.size();
            return 0;

If all you want is the size/count, just use the Tooling API by querying the Flow object:

HttpRequest req = new HttpRequest();
// Change query as necessary
String query = 'SELECT COUNT() FROM Flow WHERE ProcessType = \'Flow\'';
req.setHeader('Authorization','OAuth '+UserInfo.getSessionId());
HttpResponse res = new Http().send(req);
Map<String, Object> resAsJson = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
System.debug(resAsJson.get('totalSize')); // outputs total size

The listMetadata call doesn't allow filtering, so if you go that route, you need to process ALL the results then parse out the ones you want, while with the Tooling API, you can instantly get the total results without any processing at all.

  • Thanks a lot. Didn't knew I could use processType in SOQL. This saved me lot of efforts. – Samir Apr 2 '18 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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