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 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_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