def retrieveSfdcOpportunities(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None): try: #company_id = request.user.company_id existingIntegration = CompanyIntegration.objects(company_id = company_id).first() # code commented out since we are no longer getting only Mkto related opportunities into Cx # if existingIntegration is not None and 'mkto' in existingIntegration['integrations']: # if this system is connected to Marketo # company_qry = 'company_id' # type_field_qry = 'leads__mkto__exists' # sfdc_account_field_qry = 'leads__mkto__sfdcAccountId__ne' # querydict = {company_qry: company_id, type_field_qry: True, sfdc_account_field_qry: None} # leads_with_sfdc_opps = Lead.objects(**querydict).only('mkto_id').only('leads__mkto__sfdcAccountId') # sfdc = Salesforce() # code commented out since we are no longer getting only Mkto related opportunities into Cx # account_list = '(' # for lead in leads_with_sfdc_opps: # account_list += '\'' + lead['leads']['mkto']['sfdcAccountId'] + '\'' + ', ' # account_list = account_list[:-2] # account_list += ')' if sinceDateTime is None: sinceDateTime = (datetime.now() - timedelta(days=30)).date() oppList = sfdc.get_opportunities_delta(user_id, company_id, _str_from_date(sinceDateTime), run_type) print 'got opps ' + str(len(oppList['records'])) #create list of Opp IDs to send for get_contacts call oppid_list = '(' for opp in oppList['records']: oppid_list += '\'' + opp['Id'] + '\'' + ', ' oppid_list = oppid_list[:-2] oppid_list += ')' contactList = sfdc.get_contacts_for_opportunities(user_id, company_id, oppid_list) # needed because SFDC does not have the Contact ID within the Opp record print 'got contacts for opps ' + str(len(contactList['records'])) saveSfdcOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, contactList=contactList, job_id=job_id, run_type=run_type) try: message = 'Opportunities retrieved from Salesforce' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Opportunities' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) return oppList except Exception as e: send_notification(dict(type='error', success=False, message=str(e)))