Snowfakery
Snowfakery copied to clipboard
Error when SaleforceQuery can't find matching record.
I'm trying to seed Salesforce org using values from Spreadsheet, and seem close to having it work, but getting some errors.
The recipe should use SalesforceQuery to prematch any Contacts with the same email addresses as one in CSV Dataset. If no match was found, a new Contact record should be created.
This recipe works when a Contact matching the email address already exists in the Salesforce org. However, the same recipe throws an error if no match was found.

How can I conditionally upsert pre-existing matched record in recipe, or create a new one if no matches were found?
Really appreciate any help!
CSV Dataset
Company,Email
RealCompany,[email protected]
RealCompany,[email protected]
RealCompany,[email protected]
RealCompany,[email protected]
RealCompany,[email protected]
RealCompany,[email protected]
RealCompany,[email protected]
RealCompany,[email protected]
RealCompany,[email protected]
RealCompany,[email protected]
Recipe
- plugin: snowfakery.standard_plugins.Salesforce.SalesforceQuery
- plugin: snowfakery.standard_plugins.datasets.Dataset
- object: Account
nickname: account_new
for_each:
var: contact_row
value:
Dataset.iterate:
dataset: contact-list-company-email.csv
fields:
Name: ${{contact_row.Company | trim }}
# Create hidden field to store found record.
__contact_found:
SalesforceQuery.random_record:
from: Contact
fields: Id,Email
where: Contact.Email='${{contact_row.Email | trim}}'
# Create hidden field for the new record to be used if no pre-existing matches Contact is found.
__contact_new:
- object: Contact
fields:
Email: ${{contact_row.Email | trim }}
hed__AlternateEmail__c: ${{contact_row.Email | trim }}
hed__Preferred_Email__c: Alternate Email
friends:
- object: Campaign
nickname: Campaign1
fields:
Name:
fake: CatchPhrase
IsActive: true
- object: CampaignMember
fields:
Campaign:
reference: Campaign1
ContactId:
if:
- choice:
# If a Contact was found matching this email address, the use the found record.
when: ${{ account_new.__contact_found.Id!=NULL }}
pick: ${{ account_new.__contact_found.Id }}
- choice:
# Else default to making a new Contact
pick:
reference: __contact_new
ps - I initially tried to find matches using "SalesforceQuery.find_record" and only switched to"SalesforceQuery.random_record" after that didn't work
There is a new, beta, feature for Snowfakery for doing upserts. I will try to get it into this week's CumulusCI release so you can try it.
The new feature sounds exciting!
I just tried( and failed with error) to use upsert feature in cci mapping step- https://cumulusci.readthedocs.io/en/latest/data.html?highlight=snowfakery#upsert
Honestly, I'm amazed it actually almost works just using Snowfakery! The well-written documentation has helped me out a ton too.
I'm always happy to beta test ;)