function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
steven.fouracresteven.fouracre 

Use Metadata api to read Flows

I have been trying to read the metadata of Flows from apex using api calls but it doesnt seem to work. 
A few things that may be important to know, my org has a namespace.

I have followed Andy Fawcetts blog at https://andyinthecloud.com/2015/11/28/automating-the-creation-of-flow-screens-with-apex-metadata-api/ but there are some issues

So Andy suggests to use UserInfo.getSessionId(); to set the session Id. that used to be possible but Salesforce now prevents setting the session id with this method.
The solution to provide connection is below :

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('callout:<<Named Credential>>/services/data/v40.0/limits');
    req.setMethod('GET');
    HttpResponse res = h.send(req);
    
    ses.MetadataService.MetadataPort service = new ses.MetadataService.MetadataPort();
    service.endpoint_x = 'callout:<<Named Credential>>/services/Soap/m/40.0';
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = '{!$Credential.OAuthToken}';

This will successfully give the connection. Code after this is basically the same as Andy's:

MetadataService.Flow flow =
    (MetadataService.Flow) service.readMetadata('Flow',
        new String[] { 'NewFlow-1' }).getRecords()[0];

However the output that is returned just has nulls. There is no error messages, just nothing returned:

Flow:[Metadata.fullName=null, actionCalls=null, actionCalls_type_info=(actionCalls, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), apexPluginCalls=null, apexPluginCalls_type_info=(apexPluginCalls, http://soap.sforce.com/2006/04/metadata, null, 0, -1, false), apex_schema_type_info=(http://soap.sforce.com/2006/04/metadata, true, false), assignments=(FlowAssignment:[FlowBaseElement.processMetadataValues=null, FlowElement.description=null,........


Ive tried entering the namespace for the Flow, removing "-1" and other different possible options and none seem to work
 
steven.fouracresteven.fouracre
Note: If you havent used the connection method above this does work because when I use it to get details about CustomObjects this does return correct results