Skip to content

Commit

Permalink
@W-15946941: New task added to display files in the salesforce org (#…
Browse files Browse the repository at this point in the history
…3799)

Files uploaded via the app launcher in the orgs are currently not
included in the metadata listings.

To address this, the "display_files" task has been introduced to list
these files efficiently.

- This task gives the list of files in a tabular format querying the
`ContentDocument`, fetching the fields Id, Filetype, and Filename, as
filenames alone are not unique identifiers.
- Upon uploading a new file, a fresh entry is generated in the
`ContentDocument` entity, establishing it as the latest version in the
`ContentVersion` entity. Subsequent uploads of new versions of already
tracked documents are noted in the ContentVersion entity, without
creating additional ContentDocument records.
- Retrieving the latest version involves filtering the ContentVersion
entity by setting IsLatest to true and employing the file's
ContentDocumentId as the filter criterion.
  • Loading branch information
lakshmi2506 authored Jun 12, 2024
1 parent 29853a8 commit 2f8a94e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cumulusci/cumulusci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ tasks:
description: Prints the Community Templates available to the current org
class_path: cumulusci.tasks.salesforce.ListCommunityTemplates
group: Salesforce Communities
list_files:
description: Display documents that has been uploaded to a library in Salesforce CRM Content or Salesforce Files.
class_path: cumulusci.tasks.salesforce.salesforce_files.ListFiles
group: Salesforce Metadata
list_metadata_types:
description: Prints the metadata types in a project
class_path: cumulusci.tasks.util.ListMetadataTypes
Expand Down
30 changes: 30 additions & 0 deletions cumulusci/tasks/salesforce/salesforce_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from cumulusci.tasks.salesforce import BaseSalesforceApiTask


class ListFiles(BaseSalesforceApiTask):
task_docs = """
Lists the available documents that has been uploaded to a library in Salesforce CRM Content or Salesforce Files
"""

def _run_task(self):
self.return_values = [
{
"Id": result["Id"],
"FileName": result["Title"],
"FileType": result["FileType"],
}
for result in self.sf.query(
"SELECT Title, Id, FileType FROM ContentDocument"
)["records"]
]
self.logger.info(f"Found {len(self.return_values)} files")
if len(self.return_values) > 0:
self.logger.info(f"{'Id':<20} {'FileName':<50} {'FileType':<10}")

# Print each row of the table
for file_desc in self.return_values:
self.logger.info(
f"{file_desc['Id']:<20} {file_desc['FileName']:<50} {file_desc['FileType']:<10}"
)

return self.return_values
26 changes: 26 additions & 0 deletions cumulusci/tasks/salesforce/tests/test_salesforce_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from unittest.mock import Mock

from cumulusci.tasks.salesforce.salesforce_files import ListFiles
from cumulusci.tasks.salesforce.tests.util import create_task


class TestDisplayFiles:
def test_display_files(self):
task = create_task(ListFiles, {})
task._init_api = Mock()
task._init_api.return_value.query.return_value = {
"totalSize": 2,
"records": [
{"Title": "TEST1", "Id": "0PS000000000000", "FileType": "TXT"},
{"Title": "TEST2", "Id": "0PS000000000001", "FileType": "TXT"},
],
}
task()

task._init_api.return_value.query.assert_called_once_with(
"SELECT Title, Id, FileType FROM ContentDocument"
)
assert task.return_values == [
{"Id": "0PS000000000000", "FileName": "TEST1", "FileType": "TXT"},
{"Id": "0PS000000000001", "FileName": "TEST2", "FileType": "TXT"},
]

0 comments on commit 2f8a94e

Please sign in to comment.