diff --git a/salesforce/management/commands/update_opportunities.py b/salesforce/management/commands/update_opportunities.py index 3fa0a69c..0f3b39dc 100644 --- a/salesforce/management/commands/update_opportunities.py +++ b/salesforce/management/commands/update_opportunities.py @@ -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" @@ -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