Skip to content

Commit

Permalink
handle badly formatted uuids from SF (and log them)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwvolo committed Oct 28, 2024
1 parent fe3aa8a commit f0ff7cb
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions salesforce/management/commands/update_opportunities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.management.base import BaseCommand
from salesforce.models import AdoptionOpportunityRecord
from salesforce.salesforce import Salesforce

import sentry_sdk

class Command(BaseCommand):
help = "update book adoptions from salesforce.com for getting adoptions by account uuid"
Expand Down Expand Up @@ -41,26 +41,29 @@ def handle(self, *args, **options):
"AND Opportunity__r.Contact__r.Adoption_Status__c != 'Current Adopter'").format(base_year)

# This generally returns more than 2,000 records (the SF limit)
# See simplate_salesforce documentation for query_all: https://github.com/simple-salesforce/simple-salesforce?tab=readme-ov-file#using-bulk
# See simplate_salesforce documentation for bulk queries: https://github.com/simple-salesforce/simple-salesforce?tab=readme-ov-file#using-bulk
results = sf.bulk.Adoption__c.query(query)

# TODO: this doesn't need to be updating on the opp id, the info never changes
for i, record in enumerate(results):
opportunity, created = AdoptionOpportunityRecord.objects.update_or_create(
account_uuid=uuid.UUID(record['Opportunity__r']['Contact__r']['Accounts_UUID__c']),
book_name=record['Opportunity__r']['Book__r']['Name'],
defaults={'opportunity_id': record['Id'],
'opportunity_stage': record['Opportunity__r']['StageName'],
'adoption_type': record['Adoption_Type__c'],
'base_year': record['Base_Year__c'],
'confirmation_date': record['Confirmation_Date__c'],
'confirmation_type': record['Confirmation_Type__c'],
'how_using': record['How_Using__c'],
'savings': record['Savings__c'],
'students': record['Students__c']
}
)
opportunity.save()
try:
opportunity, created = AdoptionOpportunityRecord.objects.update_or_create(
account_uuid=uuid.UUID(record['Opportunity__r']['Contact__r']['Accounts_UUID__c']),
book_name=record['Opportunity__r']['Book__r']['Name'],
defaults={'opportunity_id': record['Id'],
'opportunity_stage': record['Opportunity__r']['StageName'],
'adoption_type': record['Adoption_Type__c'],
'base_year': record['Base_Year__c'],
'confirmation_date': record['Confirmation_Date__c'],
'confirmation_type': record['Confirmation_Type__c'],
'how_using': record['How_Using__c'],
'savings': record['Savings__c'],
'students': record['Students__c']
}
)
opportunity.save()
except ValueError:
sentry_sdk.capture_message("Adoption {} has a badly formatted Account UUID: {}".format(record['Id'], record['Opportunity__r']['Contact__r']['Accounts_UUID__c']))

# TODO: need to grab current adoptions and if the info has changed, update it so the user sees most recent adoption info
# re-submitting the form will update the current year adoption numbers, which the user might not expect

0 comments on commit f0ff7cb

Please sign in to comment.