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

ContentVersion and ProfileId functions+Tests #421

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 14 additions & 7 deletions cumulusci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,36 @@ tasks:
num_records: 10
num_records_tablename: Account

generate_users_profiles_permission_sets:
class_path: cumulusci.tasks.bulkdata.generate_and_load_data_from_yaml.GenerateAndLoadDataFromYaml
options:
generator_yaml: examples/salesforce/ContentVersion.recipe.yml

flows:
test_everything:
steps:
1:
task: generate_sf_accounts
1.1:
task: generate_users_profiles_permission_sets
2:
task: generate_sf_contacts
3:
task: generate_sf_opportunities
4:
flow: npsp:install_prod
task: generate_content_documents
5:
task: generate_npsp_accounts
flow: npsp:install_prod
6:
task: generate_npsp_contacts
task: generate_npsp_accounts
7:
task: generate_npsp_opportunities
task: generate_npsp_contacts
8:
task: generate_opportunity_contact_roles
task: generate_npsp_opportunities
9:
task: generate_opportunities_and_contacts
task: generate_opportunity_contact_roles
10:
task: generate_content_documents
task: generate_opportunities_and_contacts

contacts_for_accounts:
steps:
Expand Down
4 changes: 3 additions & 1 deletion docs/extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ class PluginThatCounts(SnowfakeryPlugin):
```

Plugins also have access to a dictionary called `self.context.field_vars()` which
represents the values that would be available to a formula running in the same context.
represents the values that would be available to a formula running in the same context
and `self.context.current_filename` which is the filename of the YAML file being
processed.

Plugins can return normal Python primitive types, `datetime.date`, `ObjectRow` or `PluginResult` objects. `ObjectRow` objects represent new output records/objects. `PluginResult` objects
expose a namespace that other code can access through dot-notation. `PluginResult` instances can be
Expand Down
39 changes: 28 additions & 11 deletions docs/salesforce.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ $ cci flow run test_everything
## Incorporating Information from Salesforce

There are various cases where it might be helpful to relate newly created synthetic
data to existing data in a Salesforce org. For example, that data might have
been added in a previous CumulusCI task or some other process.
data to existing data in a Salesforce org. Perhaps that data was added
in a previous CumulusCI task or some other process. If your use cases are
intensive, please remember to read the section
[A Note On Performance](#a-note-on-performance)

For example, if you have a Campaign object and would like to associate
### SalesforceQuery.find_record and SalesforceQuery.random_record

Let's use an example where you have a Campaign object and would like to associate
Contacts to it through CampaignMembers.

Here is an example where we query a particular Campaign object:
Expand Down Expand Up @@ -113,6 +117,11 @@ NOTE: The features we are discussing in this section are for linking to records
that are in the Salesforce org _before_ the recipe iteration started. These features
are not for linking to records created by the recipe itself.

In extremely large loads, CumulusCI can be configured to upload "portions" or "batches" of
records from the recipe. Previous portions _are_ in the org and therefore can be queried.

### Querying Field Data

Sometimes we want to do more than just link to the other record. For example,
perhaps we want to create Users for Contacts and have the Users have the same
name as the Contacts.
Expand Down Expand Up @@ -152,6 +161,8 @@ have Contacts, but you can see how you would connect a
synthetic object to a pre-existing object, while also getting
access to other fields.

### SOQL Datasets

If you would like to use Salesforce query as a Dataset, that's
another way that you can ensure that every synthetic record you
create is associated with a distinct record from Salesforce.
Expand Down Expand Up @@ -319,24 +330,30 @@ There is also an alternate syntax which allows nicknaming:
Files can be used as Salesforce ContentVersions like this:

```yaml
- plugin: snowfakery.standard_plugins.base64.Base64
- plugin: snowfakery.standard_plugins.file.File
# examples/salesforce/ContentVersion.recipe.yml
- plugin: snowfakery.standard_plugins.Salesforce
- object: Account
nickname: FileOwner
fields:
Name:
fake: company
- object: ContentVersion
nickname: FileAttachment
fields:
Title: Attachment for ${{Account.Name}}
PathOnClient: example.pdf
Description: example.pdf
Description: The example.pdf file
VersionData:
Base64.encode:
- File.file_data:
encoding: binary
file: ${{PathOnClient}}
Salesforce.ContentFile:
file: example.pdf
FirstPublishLocationId:
reference: Account
```

### A Note On Performance

Calling into Salesforce is much slower than most things you do in
Snowfakery. The network traffic alone is comparatively slow.

Consider using [variables](index.md#defining-variables) to "remember"
data you've queried, and if you need to pull down a lot of data, use
a SOQL Dataset which will intrinsically cache the data for you.
32 changes: 32 additions & 0 deletions examples/base64_file.recipe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
- plugin: snowfakery.standard_plugins.base64.Base64
- plugin: snowfakery.standard_plugins.file.File
- plugin: snowfakery.standard_plugins.Salesforce
- object: Account
nickname: FileOwner
fields:
Name:
fake: company
- object: ContentVersion
nickname: FileAttachment
fields:
Title: Attachment for ${{Account.Name}}
PathOnClient: salesforce/example.pdf
Description: example.pdf
VersionData:
Base64.encode:
- File.file_data:
encoding: binary
file: ${{PathOnClient}}
FirstPublishLocationId:
reference: Account
- object: ContentVersion
nickname: FileAttachment2
fields:
Title: Attachment for ${{Account.Name}}
PathOnClient: salesforce/example.pdf
Description: example.pdf
VersionData:
Salesforce.ContentFile:
file: ${{PathOnClient}}
FirstPublishLocationId:
reference: Account
88 changes: 0 additions & 88 deletions examples/salesforce/CommunityUsers.yml

This file was deleted.

12 changes: 4 additions & 8 deletions examples/salesforce/ContentVersion.recipe.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
- plugin: snowfakery.standard_plugins.base64.Base64
- plugin: snowfakery.standard_plugins.file.File
- plugin: snowfakery.standard_plugins.Salesforce
- object: Account
nickname: FileOwner
fields:
Name:
fake: company
- object: ContentVersion
nickname: FileAttachment
fields:
Title: Attachment for ${{Account.Name}}
PathOnClient: example.pdf
Description: example.pdf
Description: The example.pdf file
VersionData:
Base64.encode:
- File.file_data:
encoding: binary
file: ${{PathOnClient}}
Salesforce.ContentFile:
file: example.pdf
FirstPublishLocationId:
reference: Account
50 changes: 50 additions & 0 deletions examples/salesforce/UsersProfilesPermissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
- plugin: snowfakery.standard_plugins.Salesforce
- plugin: snowfakery.standard_plugins.Salesforce.SalesforceQuery

- object: User
fields:
Alias: Grace
Username:
fake: Username
LastName: Wong
Email: ${{Username}}
TimeZoneSidKey: America/Bogota
LocaleSidKey: en_US
EmailEncodingKey: UTF-8
LanguageLocaleKey: en_US
ProfileId:
Salesforce.ProfileId: Identity User

friends:
- object: PermissionSetAssignment
fields:
AssigneeId:
reference: User
PermissionSetId:
SalesforceQuery.find_record:
from: PermissionSet
where: Name='CommerceUser'
- object: PermissionSetAssignment
fields:
AssigneeId:
reference: User
PermissionSetId:
SalesforceQuery.find_record:
from: PermissionSet
where: Name='SalesConsoleUser'
- object: User
nickname: RandomizedUser
fields:
Username:
fake: Username
LastName:
fake: last_name
Email:
fake: email
Alias: Grace
TimeZoneSidKey: America/Bogota
LocaleSidKey: en_US
EmailEncodingKey: UTF-8
LanguageLocaleKey: en_US
ProfileId:
Salesforce.ProfileId: Identity User
4 changes: 4 additions & 0 deletions snowfakery/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ def evaluate(self, field_definition):
else:
raise f"Cannot simplify {field_definition}. Perhaps should have used evaluate_raw?"

@property
def current_filename(self):
return self.interpreter.current_context.current_template.filename


def lazy(func: Any) -> Callable:
"""A lazy function is one that expects its arguments to be unparsed"""
Expand Down
Loading