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 create_activity_in_pipedrive(lead_id): """ Create an activity in Pipedrive. Data to set is defined in mappings. :param lead_id: The lead id to synchronize data from :return: A custom response object containing the synchronized entity status and id """ app.logger.info('Fetching lead data from Marketo with id=%s', str(lead_id)) lead = marketo.Lead(get_marketo_client(), lead_id) if lead.id is not None: activity = pipedrive.Activity(get_pipedrive_client()) app.logger.info('New activity created') status = 'created' for pd_field in mappings.ACTIVITY_TO_LEAD: update_field(lead, activity, pd_field, mappings.ACTIVITY_TO_LEAD[pd_field]) app.logger.info('Sending lead data with id=%s to Pipedrive activity', str(lead_id)) activity.save() response = {'status': status, 'id': activity.id} else: message = 'No lead found in Marketo with id=%s' % str(lead_id) app.logger.error(message) response = {'error': message} return response
def delete_lead_in_marketo(pipedrive_marketo_id): """ Delete a lead in Marketo. :param pipedrive_marketo_id: The person related lead id to delete :return: A custom response object containing the synchronized entity status and id """ app.logger.info('Deleting person in Marketo with id=%s', str(pipedrive_marketo_id)) lead = marketo.Lead(get_marketo_client(), pipedrive_marketo_id) if lead.id is not None: lead.toDelete = True lead.save() if lead.id is not None: response = {'status': 'Ready for deletion', 'id': lead.id} else: message = 'Could not prepare lead for deletion with id=%s' % str( pipedrive_marketo_id) app.logger.error(message) response = {'error': message} else: message = 'No lead found with id=%s' % str(pipedrive_marketo_id) app.logger.error(message) response = {'error': message} return response
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_activity_in_pipedrive_for_email_sent(lead_id): """ Create one or several activities for one or several Email sent in Pipedrive. Data to set is defined in mappings. :param lead_id: The lead id to synchronize data from :return: A custom response object containing the synchronized entity statuses and ids """ app.logger.info('Fetching lead data from Marketo with id=%s', str(lead_id)) lead = marketo.Lead(get_marketo_client(), lead_id) if lead.id is not None: mkto_activities = lead.get_activities(['Send Email']) statuses = [] ids = [] for mkto_activity in mkto_activities: activity = pipedrive.Activity(get_pipedrive_client()) app.logger.info('New activity created') statuses.append('created') for pd_field in mappings.ACTIVITY_TO_EMAIL_SENT: update_field(lead, activity, pd_field, mappings.ACTIVITY_TO_EMAIL_SENT[pd_field]) activity.subject = mkto_activity['primaryAttributeValue'] email_content = get_marketo_client().get_asset( 'email', mkto_activity['primaryAttributeValueId'], 'content') if email_content and 'value' in email_content: content_text = [ value['value'] for value in email_content['value'] if value['type'] == 'Text' ] if content_text: activity.note = content_text[0] app.logger.info( 'Sending lead activities with id=%s to Pipedrive activities', str(lead_id)) activity.save() ids.append(activity.id) response = {'status': statuses, 'id': ids} else: message = 'No lead found in Marketo with id=%s' % str(lead_id) app.logger.error(message) response = {'error': message} return response
def create_or_update_lead_in_marketo(person_id): """ Create or update a lead in Marketo. Update can be performed if the person is already associated to a lead (field marketoid is populated). If the lead is already up-to-date with the associated person, does nothing. Data to set is defined in mappings. :param person_id: The person id to synchronize data from :return: A custom response object containing the synchronized entity status and id """ app.logger.info('Fetching person data from Pipedrive with id=%s', str(person_id)) person = pipedrive.Person(get_pipedrive_client(), person_id) if person.id is not None: lead = marketo.Lead(get_marketo_client(), person.marketoid) if lead.id is None: app.logger.info('New lead created') status = 'created' else: app.logger.info('Lead data fetched from Marketo with id=%s', str(lead.id)) status = 'updated' data_changed = False for mkto_field in mappings.LEAD_TO_PERSON: data_changed = update_field( person, lead, mkto_field, mappings.LEAD_TO_PERSON[mkto_field]) or data_changed if data_changed: # Perform the update only if data has actually changed app.logger.info( 'Sending person data with id=%s to Marketo%s', str(person_id), ' with id=%s' % str(person.id) if person.id is not None else '') lead.save() if not person.marketoid or len( person.marketoid.split(',')) > 1 or int( person.marketoid) != lead.id: app.logger.info( 'Updating marketo_id=%s in Pipedrive%s', lead.id, ' (old=%s)' % person.marketoid if person.marketoid else '') person.marketoid = lead.id person.save() else: app.logger.info('Nothing to do in Marketo for lead with id=%s', lead.id) status = 'skipped' response = {'status': status, 'id': lead.id} else: message = 'No person found with id %s' % str(person_id) app.logger.error(message) response = {'error': message} return response
def create_or_update_person_in_pipedrive(lead_id): """ Create or update a person in Pipedrive. Update can be performed if the lead is already associated to a person (field pipedriveId is populated). If the person is already up-to-date with the associated lead, does nothing. Data to set is defined in mappings. :param lead_id: The lead id to synchronize data from :return: A custom response object containing the synchronized entity status and id """ app.logger.info('Fetching lead data from Marketo with id=%s', str(lead_id)) lead = marketo.Lead(get_marketo_client(), lead_id) if lead.id is not None: person = pipedrive.Person(get_pipedrive_client(), lead.pipedriveId) if person.id is None: app.logger.info('New person created') status = 'created' else: app.logger.info('Person data fetched from Pipedrive with id=%s', str(person.id)) status = 'updated' data_changed = False for pd_field in mappings.PERSON_TO_LEAD: data_changed = update_field( lead, person, pd_field, mappings.PERSON_TO_LEAD[pd_field]) or data_changed if data_changed: # Perform the update only if data has actually changed app.logger.info( 'Sending lead data with id=%s to Pipedrive%s', str(lead_id), ' for person with id=%s' % str(person.id) if person.id is not None else '') person.save() if not lead.pipedriveId or lead.pipedriveId != person.id: app.logger.info( 'Updating pipedrive_id=%s in Marketo%s', person.id, ' (old=%s)' % lead.pipedriveId if lead.pipedriveId else '') lead.pipedriveId = person.id lead.save() else: app.logger.info('Nothing to do in Pipedrive for person with id=%s', person.id) status = 'skipped' response = {'status': status, 'id': person.id} else: message = 'No lead found in Marketo with id=%s' % str(lead_id) app.logger.error(message) response = {'error': message} return response
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
def create_or_update_opportunity_in_marketo(deal_id): """ Create or update an opportunity in Marketo. Update can be performed if the opportunity external id comes from the deal id. If the opportunity is already up-to-date with the associated deal, does nothing. Role cannot be updated (the only updateable field would be isPrimary). Data to set is defined in mappings. :param deal_id: The deal id to synchronize data from :return: A custom response object containing the synchronized entity status and id """ app.logger.info('Fetching deal data from Pipedrive with id=%s', str(deal_id)) deal = pipedrive.Deal(get_pipedrive_client(), deal_id) if deal.id is not None: # Filter deals pipeline = pipedrive.Pipeline(get_pipedrive_client(), deal.pipeline_id) if pipeline.name in mappings.PIPELINE_FILTER_NAMES: # Opportunity opportunity_external_id = marketo.compute_external_id( 'deal', deal.id) opportunity = marketo.Opportunity(get_marketo_client(), opportunity_external_id, 'externalOpportunityId') data_changed = False if opportunity.id is None: app.logger.info('New opportunity created') opportunity_status = 'created' opportunity.externalOpportunityId = opportunity_external_id data_changed = True else: app.logger.info( 'Opportunity data fetched from Marketo with id=%s/external_id=%s', str(opportunity.id), opportunity_external_id) opportunity_status = 'updated' for mkto_field in mappings.OPPORTUNITY_TO_DEAL: data_changed = update_field(deal, opportunity, mkto_field, mappings.OPPORTUNITY_TO_DEAL[mkto_field]) \ or data_changed if data_changed: # Perform the update only if data has actually changed app.logger.info( 'Sending deal data with id=%s to Marketo%s', str(deal_id), ' for opportunity with id=%s/external_id=%s' % (str(opportunity.id), opportunity_external_id) if opportunity.id is not None else '') opportunity.save() else: app.logger.info( 'Nothing to do in Marketo for opportunity with id=%s/external_id=%s', opportunity.id, opportunity_external_id) opportunity_status = 'skipped' response = { 'opportunity': { 'status': opportunity_status, 'id': opportunity.id } } # Role if deal.contact_person and deal.contact_person.marketoid: # Ensure person has been synced # TODO create if not? # Role will automatically be created or updated using these 3 fields ("dedupeFields") role = marketo.Role(get_marketo_client()) role.externalOpportunityId = opportunity.externalOpportunityId role.leadId = deal.contact_person.marketoid role.role = deal.champion.title if deal.champion and deal.champion.title else 'Default Role' role.isPrimary = deal.champion and deal.champion.marketoid == role.leadId app.logger.info( 'Sending deal data with id=%s to Marketo for role with (externalOpportunityId=%s, leadId=%s, role=%s)', str(deal_id), role.externalOpportunityId, role.leadId, role.role) role.save() response['role'] = {'id': role.id} else: message = 'Deal synchronization with id=%s not enabled for pipeline=%s' % ( deal_id, pipeline.name) app.logger.info(message) response = {'status': 'skipped', 'message': message} else: message = 'No deal found in Pipedrive with id=%s' % str(deal_id) app.logger.error(message) response = {'error': message} return response
def program_id_to_name(id_): return sync.get_marketo_client().get_asset('program', id_)['name'] if id_ else None