def find_company_in_marketo(organization): # Search for the company in Marketo # Try id app.logger.debug('Trying to fetch company data from Marketo with id=%s', organization.marketoid) company = marketo.Company(get_marketo_client(), organization.marketoid) if company.id is None: # Then external id # TODO remove because useless? company_external_id = marketo.compute_external_id('organization', organization.id) app.logger.debug('Trying to fetch company data from Marketo with external_id=%s', company_external_id) company = marketo.Company(get_marketo_client(), company_external_id, 'externalCompanyId') if company.id is None: # Finally name app.logger.debug('Trying to fetch company data from Marketo with name=%s', organization.name) company = marketo.Company(get_marketo_client(), organization.name, 'company') return company
def company_name_to_org_id(lead): org_id = None if lead.company: import tasks rv = tasks.create_or_update_organization_in_pipedrive( lead.externalCompanyId) if rv and 'id' in rv: # Case Company object org_id = rv['id'] else: # Case company form fields company = marketo.Company(sync.get_marketo_client()) company.externalCompanyId = marketo.compute_external_id( 'lead-company', lead.id, 'mkto') company.company = lead.company company.billingStreet = lead.street company.billingCity = lead.city company.billingState = lead.state company.billingPostalCode = lead.postalCode company.billingCountry = lead.country company.mainPhone = lead.mainPhone company.industry = lead.industry company.annualRevenue = lead.annualRevenue company.numberOfEmployees = lead.numberOfEmployees company.save() lead.externalCompanyId = company.externalCompanyId lead.save() rv = tasks.create_or_update_organization_in_pipedrive( company.externalCompanyId) org_id = rv['id'] if rv and 'id' in rv else org_id return org_id
def create_or_update_organization_in_pipedrive(company_external_id): """ Create or update an organization in Pipedrive. Update can be performed if the organization is already associated to a company (field marketoid is populated) or if they share the same name or the same email domain. If the organization is already up-to-date with the associated company, does nothing. Data to set is defined in mappings. :param company_external_id: The company external id to synchronize data from :return: A custom response object containing the synchronized entity status and id """ app.logger.info('Fetching company data from Marketo with external_id=%s', str(company_external_id)) company = marketo.Company(get_marketo_client(), company_external_id, 'externalCompanyId') if company.id is not None: organization = find_organization_in_pipedrive(company) if organization.id is None: app.logger.info('New organization created') status = 'created' else: app.logger.info( 'Organization data fetched from Pipedrive with id=%s', str(organization.id)) status = 'updated' data_changed = False for pd_field in mappings.ORGANIZATION_TO_COMPANY: data_changed = update_field(company, organization, pd_field, mappings.ORGANIZATION_TO_COMPANY[pd_field]) \ or data_changed if data_changed: # Perform the update only if data has actually changed app.logger.info( 'Sending company data with external_id=%s to Pipedrive%s', str(company_external_id), ' for organization with id=%s' % str(organization.id) if organization.id is not None else '') organization.save() else: app.logger.info( 'Nothing to do in Pipedrive for organization with id=%s', organization.id) status = 'skipped' response = {'status': status, 'id': organization.id} else: message = 'No company found in Marketo with external_id=%s' % str( company_external_id) app.logger.error(message) response = {'error': message} return response