SFDO-Tooling / Snowfakery

A tool for generating fake data that has relations between tables.
BSD 3-Clause "New" or "Revised" License
143 stars 32 forks source link

Error when SaleforceQuery can't find matching record. #658

Open MannyAdumbire opened 2 years ago

MannyAdumbire commented 2 years ago

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.

snowfakery-sf-upsert-null-error

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,nytggkdrfigge@snow.fake
RealCompany,jlqdnidzdbvgic@snow.fake
RealCompany,ppqkxfzxxyojwnwhke@snow.fake
RealCompany,mlkbarzbjvbykqjm@snow.fake
RealCompany,inmfgbpufljyhmrycp@snow.fake
RealCompany,phrojiaydrbmjosiu@snow.fake
RealCompany,fcgshlpfccnavyhgu@snow.fake
RealCompany,numrbbqyrmqrqrwql@snow.fake
RealCompany,blqyqpugry@snow.fake
RealCompany,rttcjftzhr@snow.fake

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
MannyAdumbire commented 2 years ago

ps - I initially tried to find matches using "SalesforceQuery.find_record" and only switched to"SalesforceQuery.random_record" after that didn't work

prescod commented 2 years ago

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.

MannyAdumbire commented 2 years ago

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 ;)