Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added deactivate_flows_task #3664

Merged
merged 5 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cumulusci/cumulusci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 25 additions & 10 deletions cumulusci/tasks/salesforce/activate_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,58 @@ 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):
super(ActivateFlow, self)._init_options(kwargs)
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"]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lakshmi2506
Consider the test case that status was not entered in the options:

create_task(
            ActivateFlow,
            {
                "developer_names": [
                    "Auto_Populate_Date_And_Name_On_Program_Engagement",
                    "ape",
                ],
            },
        )

Would this code still pass or get a key error, since status is not a task option.

To fix this I recommend adding

self.options["status"] = process_list_arg(
            self.options.get("status"), False
        )
        

to the _init_options() section.

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:
Expand Down
56 changes: 49 additions & 7 deletions cumulusci/tasks/salesforce/tests/test_activate_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ def test_activate_some_flow_processes(self):
"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": [
Expand All @@ -49,6 +50,46 @@ def test_activate_some_flow_processes(self):
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(
Expand All @@ -57,24 +98,25 @@ def test_activate_all_flow_processes(self):
"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": [
Expand Down