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 

Deactivate flows in a beta package by apex

Using the following code I am able to deactivate a flow via code in any org, but if I use the same code in an org where a package has been installed. although you can still manually deactivate the flow, the code no longer works

Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'OAuth ' + '{!$Credential.OAuthToken}');
req.setHeader('Content-Type', 'application/json');
req.setEndpoint('callout:Connect_to_this_org/services/data/v43.0/tooling/sobjects/FlowDefinition/<<flow id>>');
req.setMethod('PATCH');
String reqMsg = '{"Metadata": {"activeVersionNumber": 0}}';
req.setBody(reqMsg);
HttpResponse res = h.send(req);
        h.send(req);
system.debug('## res1 ' + res.getbody());
InzamamulInzamamul (Salesforce Developers) 
Hello Steven,

I would request you to please confirm if the package is a managed package or not where you are trying to use the code to deactivate the flow so that I can help you more?

And If you are using on a managed package then I would suggest you to please check the below article:

https://salesforce.stackexchange.com/questions/362957/can-we-deactivate-a-flow-from-managed-package

Thank You!
steven.fouracresteven.fouracre
Its still a beta package but even for a release I believe you can manually deactivate flows so wouldnt think that should cause an issue.
the thing that is strange is the same code used in a normal org works perfectly fine
steven.fouracresteven.fouracre
I have solved my own issue. The code to use is very similar to the above. All that is required is to enter a # at the end of the Flow id
Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'OAuth ' + '{!$Credential.OAuthToken}');
req.setHeader('Content-Type', 'application/json');
req.setEndpoint('callout:Connect_to_this_org/services/data/v43.0/tooling/sobjects/FlowDefinition/<<flow id>>#');
req.setMethod('PATCH');
String reqMsg = '{"Metadata": {"activeVersionNumber": 0}}';
req.setBody(reqMsg);
HttpResponse res = h.send(req);
        h.send(req);
system.debug('## res1 ' + res.getbody());

This will work even if you have a managed package and have installed the package in an org. With a bit more work you can mass deactivate Flows