3

It is my understanding right now the only way to deactivate a Salesforce flow is to retrieve the FlowDefinition file and set the active version number to zero

Is using workbench or VS Code the only way to retrieve this file? I see GearSet has a way to do this, so theoretically, do they have like a script that pulls this file down and sets the version number to 0?

Is there really not a way to do this via the Metadata API?

<?xml version ="1.0" encoding="UTF-8"?>
<FlowDefinition xmlns="http://soap.sforce.com/2006/04/metadata">
    <activeversionNumber>0</activeversionNumber>
</FlowDefinition>

https://help.salesforce.com/s/articleView?id=000387737&type=1

enter image description here

7
  • Not sure why I got the downvote? If Im mistaken please correct me.
    – andrew
    Commented Feb 15, 2023 at 19:20
  • I assume you mean "deactivate through the API" here? Clicking the "deactivate" button in the flow itself worked perfectly fine in my instance the last time I tried it (maybe a week ago?), but I'm guessing that isn't what you're looking to do here. Commented Feb 15, 2023 at 19:24
  • Yes - via the API - thank you Stephen
    – andrew
    Commented Feb 15, 2023 at 19:28
  • 1
    the link you posted is using the MDAPI (i.e. Workbench uses the MDAPI to deploy the change)
    – cropredy
    Commented Feb 15, 2023 at 19:44
  • Yes but my understanding is that you would need to retrieve that update the file and then deploy it like a package.xml file? As an app I would like the ability to deactivate and activate the flow by passing some kind of status to the target org. Is that possible?
    – andrew
    Commented Feb 16, 2023 at 0:24

1 Answer 1

3
+100

Updating flow status can be achieved using Tooling REST API by FlowDefinitionId

To disable flow definition, set version number 0
req.setBody( '{"Metadata": {"activeVersionNumber":0}}');

To activate flow definition, specify the version number to activate e.g 20
req.setBody( '{"Metadata": {"activeVersionNumber":20}}');

Here's a sample code in apex using user session Id as authorization for simplicity, use access token (OAuth) when calling the API from the external app.

Static string myDomain = 'https://yourdomain.my.salesforce.com';

private static HttpRequest buildHttpReq(String endpoint, String method){
   HttpRequest req = new HttpRequest();
   req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
   req.setHeader('Content-Type', 'application/json');
   req.setEndpoint(endpoint);
   req.setMethod(method);
   return req;
}

public static String queryFlowDef(){
   //find the flowdefinitionid

   String flowLabel='FlowMasterLabel';
   //adapt query accordingly, here retrieving latest flow version
   String queryEndpoint = myDomain+ '/services/data/v55.0/tooling/query/?q=Select+DefinitionId+from+Flow+Where+MasterLabel=\'' + flowLabel +  '\'order+by+VersionNumber+desc+limit+1';

   HttpRequest req = buildHttpReq(queryEndpoint,'GET');

   Http httpreq = new Http();
   HttpResponse res = httpreq.send(req);
   String response = res.getBody();
   Map<String,Object> rmp = (Map<String,Object>)JSON.deserializeuntyped(response);
   List<Object> lstObt = (List<Object>)rmp.get('records');
   Map<String,Object> mapRec = (Map<String,Object>)lstObt[0];

   String defId = (String)mapRec.get('DefinitionId');
   System.debug(LoggingLevel.DEBUG, defId );
   return defId;
}

public static void disableFlow(){
  String flowDefId = queryFlowDef();
  HttpRequest req = buildHttpReq(myDomain+'/services/data/v55.0/tooling/sobjects/FlowDefinition/'+flowDefId,'PATCH');

  //to disable flow set version number 0
  req.setBody( '{"Metadata": {"activeVersionNumber":0}}');

  //to reactivate flow version specify the version number e.g 20
  //req.setBody( '{"Metadata": {"activeVersionNumber":20}}');

  Http http = new Http();
  HttpResponse res = http.send(req);
  String response = res.getBody();
  System.debug(LoggingLevel.DEBUG, response );
  System.debug(LoggingLevel.DEBUG, res.getStatusCode() );    
}

Additional references:

FlowDefinition tooling API

Metadatype FlowDefintion

3
  • 1
    Thank you so much!
    – andrew
    Commented Feb 23, 2023 at 18:33
  • @andrew mark the answer as accepted, if this answers your question :)
    – Shamina
    Commented Feb 23, 2023 at 19:14
  • Will need to test this but this is helpful from the other stuff Ive seen so I will!
    – andrew
    Commented Feb 24, 2023 at 20:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .