[SalesForce] Salesforce Flow Metadata list / retrieve

Strange Behaviour.
1. When i perform a list operation on Flow, I get the following response.

 [
      {
         "createdById": "00520000003eLmNAAU",
         "createdByName": "Gunish Rai Chawla",
         "createdDate": "2014-08-11T16:42:47.000Z",
         "fileName": "flows/ProjectProgressFlow.flow",
         "fullName": "ProjectProgressFlow",
         "id": "301200000000ETlAAM",
         "lastModifiedById": "00520000003eLmNAAU",
         "lastModifiedByName": "Gunish Rai Chawla",
         "lastModifiedDate": "2014-08-11T18:43:04.000Z",
         "manageableState": "unmanaged",
         "type": "Flow"
       }
    ]

So Far so Good,

But when I fire a retrieve request using the data from the listResponse.

{
    "name": "Flow",
    "members": [
      "ProjectProgressFlow"
    ]
  },

I get a blank ?

Anyone else seen similar behaviour ?

Best Answer

When retrieving a flow from Salesforce, it seems that you need to specify the version of the flow as well. For some reason, the List Metadata API doesn't show the version number for the active version of the flow. It does show version numbers.

Listing Flows

For example, if I have a Flow called My_Custom_Flow, and I make 3 versions, and don't activate any of them, List Metadata will tell me I have the following:

My_Custom_Flow-1
My_Custom_Flow-2
My_Custom_Flow-3

If I then go ahead and activate version 3 of the flow, List Metadata will now say:

My_Custom_Flow-1
My_Custom_Flow-2
My_Custom_Flow

Deploying and Retrieving Flows

However, the actual API name of version 3 is still My_Custom_Flow-3, so when you go to retrieve it, you will need to call it by that name in your package.xml

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>My_Custom_Flow-3</members>
        <name>Flow</name>
    </types>
    <version>35.0</version>
</Package>

The same applies for the metadata Deploy API; you should deploy the flow using the full API name (including version number), with the additional caveat that you can only deploy the flow version when that version has not been activated in the org before. So if I wanted to deploy a new copy of My_Custom_Flow to this org (where version 3 is activated), I would need to call the flow My_Custom_Flow-4.flow so that it does not conflict with the currently active version.

Getting the Version Number for a Flow

Unfortunately, all this means that it's pretty hard to do any useful automation around flows, because Salesforce's internal versioning strategy makes them a little hard to work with. If you need a scriptable way to get the currently active or latest version of a flow from your org, the Tooling API may help a little:

SELECT
    ActiveVersion.VersionNumber,
    LatestVersion.VersionNumber
FROM FlowDefinition
WHERE DeveloperName = 'My_Custom_Flow'

This Tooling API's "query" service can give you both the currently active and the latest version number of the flow in your org.

{
   "size": 1,
   "totalSize": 1,
   "done": true,
   "queryLocator": null,
   "entityTypeName": "FlowDefinition",
   "records": [
      {
         "attributes": {
            "type": "FlowDefinition",
            "url": "/services/data/v35.0/tooling/sobjects/FlowDefinition/..."
         },
         "Id": "...",
         "ActiveVersion": {
            "attributes": {
               "type": "Flow",
               "url": "/services/data/v35.0/tooling/sobjects/Flow/..."
            },
            "VersionNumber": 3
         },
         "LatestVersion": {
            "attributes": {
               "type": "Flow",
               "url": "/services/data/v35.0/tooling/sobjects/Flow/..."
            },
            "VersionNumber": 3
         }
      }
   ]
}

If you need to retrieve the currently active version of the flow, you can just use the flow's API name in combination with the ActiveVersion returned by this.

If you need to do something to automatically deploy new versions of the flow to your org, you can just take the LatestVersion number and add 1 to get an unused version number for the new flow.

Related Topic