Repository: SFDO-Tooling/CumulusCI
Pull request #3664 (2023-09-22 11:48:36)

Added deactivate_flows_task
Hi @davidmreed 

lakshmi2506: Hi @davidmreed 

Added the deactivate_flow task through adding the status option in the activate_flow.py task and wrote the necessary test case.

Please review the PR 
Thank you

Patch:
diff --git a/cumulusci/cumulusci.yml b/cumulusci/cumulusci.yml
index ab6adc8a5..3714013e8 100644
--- a/cumulusci/cumulusci.yml
+++ b/cumulusci/cumulusci.yml
@@ -5,6 +5,15 @@ tasks:
         group: Metadata Transformations
         description: Activates Flows identified by a given list of Developer Names
         class_path: cumulusci.tasks.salesforce.activate_flow.ActivateFlow
+        options:
+            status: True
+
+    deactivate_flow:
+        group: Metadata Transformations
+        description: deactivates Flows identified by a given list of Developer Names
+        class_path: cumulusci.tasks.salesforce.activate_flow.ActivateFlow
+        options:
+            status: False
     add_page_layout_related_lists:
         group: Metadata Transformations
         description: Adds specified Related List to one or more Page Layouts.
diff --git a/cumulusci/tasks/salesforce/activate_flow.py b/cumulusci/tasks/salesforce/activate_flow.py
index 9e6a203f1..9bbdbc2c6 100644
--- a/cumulusci/tasks/salesforce/activate_flow.py
+++ b/cumulusci/tasks/salesforce/activate_flow.py
@@ -12,7 +12,11 @@ class ActivateFlow(BaseSalesforceApiTask):
         "developer_names": {
             "description": "List of DeveloperNames to query in SOQL",
             "required": True,
-        }
+        },
+        "status": {
+            "description": "Flag to check whether to activate or deactivate the flow",
+            "required": False,
+        },
     }
 
     def _init_options(self, kwargs):
@@ -20,35 +24,46 @@ class ActivateFlow(BaseSalesforceApiTask):
         self.options["developer_names"] = process_list_arg(
             self.options.get("developer_names")
         )
-        self.api_version = "43.0"
+        self.api_version = "58.0"
         if not self.options["developer_names"]:
             raise TaskOptionsError(
                 "Error you are missing developer_names definition in your task cumulusci.yml file. Please pass in developer_names for your task configuration or use -o to developer_names as a commandline argument"
             )
 
     def _run_task(self):
-        self.logger.info(
-            f"Activating the following Flows: {self.options['developer_names']}"
-        )
+        if self.options["status"]:
+            self.logger.info(
+                f"Activating the following Flows: {self.options['developer_names']}"
+            )
+        else:
+            self.logger.info(
+                f"Deactivating the following Flows: {self.options['developer_names']}"
+            )
+
         self.logger.info("Querying flow definitions...")
         result = self.tooling.query(
             "SELECT Id, ActiveVersion.VersionNumber, LatestVersion.VersionNumber, DeveloperName FROM FlowDefinition WHERE DeveloperName IN ({0})".format(
                 ",".join([f"'{n}'" for n in self.options["developer_names"]])
             )
         )
+
         results = []
         for listed_flow in result["records"]:
             results.append(listed_flow["DeveloperName"])
             self.logger.info(f'Processing: {listed_flow["DeveloperName"]}')
             path = f"tooling/sobjects/FlowDefinition/{listed_flow['Id']}"
+
             urlpath = self.sf.base_url + path
-            data = {
-                "Metadata": {
-                    "activeVersionNumber": listed_flow["LatestVersion"]["VersionNumber"]
-                }
-            }
+
+            if self.options["status"]:
+                updated_version_number = listed_flow["LatestVersion"]["VersionNumber"]
+            else:
+                updated_version_number = 0
+            data = {"Metadata": {"activeVersionNumber": updated_version_number}}
+            self.logger.info(urlpath)
             response = self.tooling._call_salesforce("PATCH", urlpath, json=data)
             self.logger.info(response)
+
         excluded = []
         for i in self.options["developer_names"]:
             if i not in results:


Test patch:
diff --git a/cumulusci/tasks/salesforce/tests/test_activate_flow.py b/cumulusci/tasks/salesforce/tests/test_activate_flow.py
index 30c03d25c..c89a7c2a2 100644
--- a/cumulusci/tasks/salesforce/tests/test_activate_flow.py
+++ b/cumulusci/tasks/salesforce/tests/test_activate_flow.py
@@ -18,18 +18,19 @@ class TestActivateFlow:
                 "developer_names": [
                     "Auto_Populate_Date_And_Name_On_Program_Engagement",
                     "ape",
-                ]
+                ],
+                "status": True,
             },
         )
         record_id = "3001F0000009GFwQAM"
         activate_url = (
-            "{}/services/data/v43.0/tooling/sobjects/FlowDefinition/{}".format(
+            "{}/services/data/v58.0/tooling/sobjects/FlowDefinition/{}".format(
                 cc_task.org_config.instance_url, record_id
             )
         )
         responses.add(
             method="GET",
-            url="https://test.salesforce.com/services/data/v43.0/tooling/query/?q=SELECT+Id%2C+ActiveVersion.VersionNumber%2C+LatestVersion.VersionNumber%2C+DeveloperName+FROM+FlowDefinition+WHERE+DeveloperName+IN+%28%27Auto_Populate_Date_And_Name_On_Program_Engagement%27%2C%27ape%27%29",
+            url="https://test.salesforce.com/services/data/v58.0/tooling/query/?q=SELECT+Id%2C+ActiveVersion.VersionNumber%2C+LatestVersion.VersionNumber%2C+DeveloperName+FROM+FlowDefinition+WHERE+DeveloperName+IN+%28%27Auto_Populate_Date_And_Name_On_Program_Engagement%27%2C%27ape%27%29",
             body=json.dumps(
                 {
                     "records": [
@@ -49,6 +50,46 @@ class TestActivateFlow:
         cc_task()
         assert 2 == len(responses.calls)
 
+    @responses.activate
+    def test_deactivate_some_flow_processes(self):
+        cc_task = create_task(
+            ActivateFlow,
+            {
+                "developer_names": [
+                    "Auto_Populate_Date_And_Name_On_Program_Engagement",
+                    "ape",
+                ],
+                "status": False,
+            },
+        )
+        record_id = "3001F0000009GFwQAM"
+        activate_url = (
+            "{}/services/data/v58.0/tooling/sobjects/FlowDefinition/{}".format(
+                cc_task.org_config.instance_url, record_id
+            )
+        )
+        responses.add(
+            method="GET",
+            url="https://test.salesforce.com/services/data/v58.0/tooling/query/?q=SELECT+Id%2C+ActiveVersion.VersionNumber%2C+LatestVersion.VersionNumber%2C+DeveloperName+FROM+FlowDefinition+WHERE+DeveloperName+IN+%28%27Auto_Populate_Date_And_Name_On_Program_Engagement%27%2C%27ape%27%29",
+            body=json.dumps(
+                {
+                    "records": [
+                        {
+                            "Id": record_id,
+                            "DeveloperName": "Auto_Populate_Date_And_Name_On_Program_Engagement",
+                            "LatestVersion": {"VersionNumber": 1},
+                        }
+                    ]
+                }
+            ),
+            status=200,
+        )
+        data = {"Metadata": {"activeVersionNumber": 0}}
+        responses.add(method=responses.PATCH, url=activate_url, status=204, json=data)
+
+        cc_task()
+        assert 2 == len(responses.calls)
+
     @responses.activate
     def test_activate_all_flow_processes(self):
         cc_task = create_task(
@@ -57,24 +98,25 @@ class TestActivateFlow:
                 "developer_names": [
                     "Auto_Populate_Date_And_Name_On_Program_Engagement",
                     "ape",
-                ]
+                ],
+                "status": True,
             },
         )
         record_id = "3001F0000009GFwQAM"
         record_id2 = "3001F0000009GFwQAW"
         activate_url = (
-            "{}/services/data/v43.0/tooling/sobjects/FlowDefinition/{}".format(
+            "{}/services/data/v58.0/tooling/sobjects/FlowDefinition/{}".format(
                 cc_task.org_config.instance_url, record_id
             )
         )
         activate_url2 = (
-            "{}/services/data/v43.0/tooling/sobjects/FlowDefinition/{}".format(
+            "{}/services/data/v58.0/tooling/sobjects/FlowDefinition/{}".format(
                 cc_task.org_config.instance_url, record_id2
             )
         )
         responses.add(
             method="GET",
-            url="https://test.salesforce.com/services/data/v43.0/tooling/query/?q=SELECT+Id%2C+ActiveVersion.VersionNumber%2C+LatestVersion.VersionNumber%2C+DeveloperName+FROM+FlowDefinition+WHERE+DeveloperName+IN+%28%27Auto_Populate_Date_And_Name_On_Program_Engagement%27%2C%27ape%27%29",
+            url="https://test.salesforce.com/services/data/v58.0/tooling/query/?q=SELECT+Id%2C+ActiveVersion.VersionNumber%2C+LatestVersion.VersionNumber%2C+DeveloperName+FROM+FlowDefinition+WHERE+DeveloperName+IN+%28%27Auto_Populate_Date_And_Name_On_Program_Engagement%27%2C%27ape%27%29",
             body=json.dumps(
                 {
                     "records": [