Esempio n. 1
0
def saveSfdcOpportunitiesToMaster(user_id=None, company_id=None, job_id=None, run_type=None):  
    #job_id = ObjectId("56a3f89f8afb003c13a59e26")
    
    if run_type == 'initial':
        opps = TempData.objects(Q(company_id=company_id) & Q(record_type='opportunity') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) 
        contacts = TempData.objects(Q(company_id=company_id) & Q(record_type='contact') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) 
    else:
        opps = TempDataDelta.objects(Q(company_id=company_id) & Q(record_type='opportunity') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) 
        contacts = TempDataDelta.objects(Q(company_id=company_id) & Q(record_type='contact') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id) 
  
    oppListTemp = list(opps)
    oppList = [i['source_record'] for i in oppListTemp]
    
    contactListTemp = list(contacts)
    contactList = [i['source_record'] for i in contactListTemp]
     
    try:
        allOpps = oppList #['records']
        # below code copied from contacts.tasks
        for newContact in contactList: #['records']:
            if 'OpportunityContactRoles' not in newContact or newContact['OpportunityContactRoles'] is None: # if this contact has no opportunities
                continue # move to next contact
            # company_id = request.user.company_id
            sfdc_account_id = None
            thisLeadsOppsIds = newContact['OpportunityContactRoles']['records']
            thisLeadsOpps = []
            for opp in thisLeadsOppsIds: #loop through all the Opp records in the Contact record
                print 'trying for opp with id ' + str(opp['OpportunityId'])
                thisOpp = next((x for x in allOpps if x['Id'] == opp['OpportunityId']), None) # if this opp is found in the list of opps retrieved separately
                if thisOpp is not None: # if found
                    print 'found this opp'
                    sfdc_account_id = thisOpp['AccountId']
                    thisLeadsOpps.append(thisOpp) #add it
            
            sfdc_contact_Id = str(newContact['Id']) 
            print 'contact id is ' + sfdc_contact_Id
            # sfdc_mkto_id = str(newLead['sfdcLeadId']) #check if there is a corresponding lead from MKTO
            existingLeadMkto = None
            existingLeadSfdc = None
            existingLeadHspt = None
            existingContact = Lead.objects(Q(company_id=company_id) & Q(sfdc_contact_id=sfdc_contact_Id)).first()
            
            if existingContact is not None:  # we found this contact already in the DB
                print ' eC is not none'        
                if 'sfdc' not in existingContact.opportunities:
                    opportunities = {}
                    opportunities['sfdc'] = []
                    opportunities['sfdc'].extend(thisLeadsOpps)  
                    existingContact.update(opportunities__sfdc = opportunities['sfdc'])
                    existingContact.update(sfdc_account_id = sfdc_account_id)
                    print 'just updated acct id 1'
                else:
                    for newOpp in thisLeadsOpps:
                        print ' nefre get' 
                        if not any (e.get('Id', None) == newOpp['Id'] for e in existingContact.opportunities['sfdc']): # does an opportunity with this Id already exist
                            opportunities = existingContact.opportunities['sfdc']
                            opportunities.append(newOpp)
                            existingContact.sfdc_account_id = sfdc_account_id
                            # save this opportunity        
                            existingContact.update(opportunities__sfdc = opportunities)
                            existingContact.update(sfdc_account_id = sfdc_account_id)
                            print 'just updated acct id 2'
                        else: #this opp already exists
                            for i in range(len(existingContact.opportunities['sfdc'])):
                                if existingContact.opportunities['sfdc'][i]['Id'] == newOpp['Id']:
                                    existingContact.opportunities['sfdc'][i] = newOpp
                                    existingContact.sfdc_account_id = sfdc_account_id
                                    existingContact.save()
                                    print 'just updated acct id 3'
            elif existingContact is None:  # this lead does not exist 
                print ' eC is much none' 
                existingLeadSfdc = Lead.objects(Q(company_id=company_id) & Q(leads__sfdc__ConvertedContactId=sfdc_contact_Id)).first()
                if existingLeadSfdc is not None:
                    
                    if 'sfdc' not in existingLeadSfdc.opportunities:
                        opportunities = {}
                        opportunities['sfdc'] = []
                        opportunities['sfdc'].extend(thisLeadsOpps)  
                        existingLeadSfdc.update(opportunities__sfdc = opportunities['sfdc'])
                        existingLeadSfdc.update(sfdc_account_id = sfdc_account_id)
                        print 'just updated acct id 4'
                    else:
                        for newOpp in thisLeadsOpps:
                            if not any (e.get('Id', None) == newOpp['Id'] for e in existingLeadSfdc.opportunities['sfdc']): # does an opportunity with this Id already exist
                                opportunities = existingLeadSfdc.opportunities['sfdc']
                                opportunities.append(newOpp)
                                # save this opportunity        
                                existingLeadSfdc.update(opportunities__sfdc = opportunities)
                                existingLeadSfdc.update(sfdc_account_id = sfdc_account_id)
                                print 'just updated acct id 5'
                            else: #this opp already exists
                                for i in range(len(existingLeadSfdc.opportunities['sfdc'])):
                                    if existingLeadSfdc.opportunities['sfdc'][i]['Id'] == newOpp['Id']:
                                        existingLeadSfdc.opportunities['sfdc'][i] = newOpp
                                        existingLeadSfdc.sfdc_account_id = sfdc_account_id
                                        existingLeadSfdc.save()
                                        print 'just updated acct id 6'
                else:
                    existingLeadMkto = Lead.objects(Q(company_id=company_id) & Q(leads__mkto__sfdcContactId=sfdc_contact_Id)).first()
                    if existingLeadMkto is not None:  # we found a MKto lead record which is matched to this new Sfdc lead
                        
                        if 'sfdc' not in existingLeadMkto.opportunities:
                            opportunities = {}
                            opportunities['sfdc'] = []
                            opportunities['sfdc'].extend(thisLeadsOpps)  
                            existingLeadMkto.update(opportunities__sfdc = opportunities['sfdc'])
                            existingLeadMkto.update(sfdc_account_id = sfdc_account_id)
                        else:
                            for newOpp in thisLeadsOpps:
                                if not any (e.get('Id', None) == newOpp['Id'] for e in existingLeadMkto.opportunities['sfdc']): # does an opportunity with this Id already exist
                                    opportunities = existingLeadMkto.opportunities['sfdc']
                                    opportunities.append(newOpp)
                                    # save this opportunity        
                                    existingLeadMkto.update(opportunities__sfdc = opportunities)
                                    existingLeadMkto.update(sfdc_account_id = sfdc_account_id)
                                else: #this opp already exists
                                    for i in range(len(existingLeadMkto.opportunities['sfdc'])):
                                        if existingLeadMkto.opportunities['sfdc'][i]['Id'] == newOpp['Id']:
                                            existingLeadMkto.opportunities['sfdc'][i] = newOpp
                                            existingLeadMkto.sfdc_account_id = sfdc_account_id
                                            existingLeadMkto.save()
                        
                    existingLeadHspt = Lead.objects(Q(company_id=company_id) & Q(leads__hspt__properties__salesforcecontactid=sfdc_contact_Id)).first()
                    if existingLeadHspt is not None:  # we found a Hspt lead record which is matched to this new Sfdc lead
                        
                        if 'sfdc' not in existingLeadHspt.opportunities:
                            opportunities = {}
                            opportunities['sfdc'] = []
                            opportunities['sfdc'].extend(thisLeadsOpps)  
                            existingLeadHspt.update(opportunities__sfdc = opportunities['sfdc'])
                            existingLeadHspt.update(sfdc_account_id = sfdc_account_id)
                        else:
                            for newOpp in thisLeadsOpps:
                                if not any (e.get('Id', None) == newOpp['Id'] for e in existingLeadHspt.opportunities['sfdc']): # does an opportunity with this Id already exist
                                    opportunities = existingLeadHspt.opportunities['sfdc']
                                    opportunities.append(newOpp)
                                    # save this opportunity        
                                    existingLeadHspt.update(opportunities__sfdc = opportunities)
                                    existingLeadHspt.update(sfdc_account_id = sfdc_account_id)
                                else: #this opp already exists
                                    for i in range(len(existingLeadHspt.opportunities['sfdc'])):
                                        if existingLeadHspt.opportunities['sfdc'][i]['Id'] == newOpp['Id']:
                                            existingLeadHspt.opportunities['sfdc'][i] = newOpp
                                            existingLeadHspt.sfdc_account_id = sfdc_account_id
                                            existingLeadHspt.save()
            
            if existingLeadSfdc is None and existingLeadMkto is None and existingLeadHspt is None and existingContact is None:  # no matches found so save new record
                lead = Lead()
                lead.sfdc_contact_id = sfdc_contact_Id
                lead.company_id = company_id
                lead.source_first_name = newContact['FirstName']
                lead.source_last_name = newContact['LastName']
                lead.source_email = newContact['Email']
                lead.source_created_date = str(newContact['CreatedDate'])
                lead.source_source = newContact['LeadSource']
                lead.sfdc_account_id = sfdc_account_id
                lead.save()
                contacts = {}
                contacts['sfdc'] = newContact
                #lead.contacts["sfdc"] = newContact
                lead.update(contacts__sfdc = contacts['sfdc'])
                print 'just updated acct id 7'
                opportunities = {}
                opportunities['sfdc'] = []
                opportunities['sfdc'].extend(thisLeadsOpps)  
                lead.update(opportunities__sfdc = opportunities['sfdc'])
        
        #new code added on 1/24/2016 - add each opportunity to related account (to capture opps with no contacts)
        for opp in allOpps:
            account = Account.objects(Q(company_id=company_id) & Q(sfdc_id=opp['AccountId'])).first()
            if account is None:
                print 'no account for opp with ID ' + str(opp['Id'])
                sfdc = Salesforce()
                accountList = sfdc.get_single_account(user_id, company_id, opp['AccountId'])
                for newAccount in accountList['records']:
                    account = _saveSfdcNewAccount(newAccount['Id'], newAccount, None, company_id)
            if 'opportunities' not in account:
                account.update(opportunities = {})
            if 'sfdc' not in account.opportunities: #no opps exist for this account, so add this opp
                opportunities = {}
                opportunities['sfdc'] = []
                opportunities['sfdc'].append(opp)  
                account.update(opportunities__sfdc = opportunities['sfdc'])
            else:
                if not any (e.get('Id', None) == opp['Id'] for e in account.opportunities['sfdc']): # there is no opportunity with this Id already exist
                    opportunities = account.opportunities['sfdc']
                    opportunities.append(opp)
                    # save this opportunity        
                    account.update(opportunities__sfdc = opportunities)
                else: # this opp already exists
                    for i in range(len(account.opportunities['sfdc'])):
                        if account.opportunities['sfdc'][i]['Id'] == opp['Id']:
                            account.opportunities['sfdc'][i] = opp
                            account.save()
                
            
        
# code commented out since we are no longer getting only Mkto related opportunities into Cx       
#         for newOpp in oppList['records']:
# 
#             # company_id = request.user.company_id
#             sfdc_account_id = str(newOpp['AccountId']) # find the account ID
#             print 'account id is ' + sfdc_account_id
#             # sfdc_mkto_id = str(newLead['sfdcLeadId']) #check if there is a corresponding lead from MKTO
#             existingLeadMkto = None
#             existingLeadSfdc = None
#             existingLeadHspt = None
#             #existingContact = Lead.objects(Q(company_id=company_id) & Q(sfdc_contact_id=sfdc_contact_Id)).first()
#             existingLeadSfdc = Lead.objects(Q(company_id=company_id) & Q(leads__sfdc__ConvertedAccountId=sfdc_account_id)).first()
#             if existingLeadSfdc is not None:
#                 if 'sfdc' not in existingLeadSfdc.opportunities:
#                     opportunities = {}
#                     opportunities['sfdc'] = []
#                     opportunities['sfdc'].append(newOpp) # add this opp to the new array
#                     existingLeadSfdc.update(opportunities__sfdc = opportunities)
#                 else:
#                     if not any (e.get('Id', None) == newOpp['Id'] for e in existingLeadSfdc.opportunities['sfdc']): # does an opportunity with this Id already exist
#                         opportunities = existingLeadSfdc.opportunities['sfdc']
#                         opportunities.append(newOpp)
#                         # save this opportunity        
#                         existingLeadSfdc.update(opportunities__sfdc = opportunities)
#                     else: #this opp already exists
#                         for i in range(len(existingLeadSfdc.opportunities['sfdc'])):
#                             if existingLeadSfdc.opportunities['sfdc'][i]['Id'] == newOpp['Id']:
#                                 existingLeadSfdc.opportunities['sfdc'][i] = newOpp
#                                 existingLeadSfdc.save()
#                 
#                 # and move to the next opportunity in the loop
#                 continue
#             else: #this opp does not exist within an SFDC lead
#                 # check if it is a Marketo lead   
#                 existingLeadMkto = Lead.objects(Q(company_id=company_id) & Q(leads__mkto__sfdcAccountId=sfdc_account_id)).first()
#                 if existingLeadMkto is not None:  # we found a MKto lead record which is matched to this opp
#                     print 'found mkto lead' + existingLeadMkto.mkto_id
#                     if 'sfdc' not in existingLeadMkto.opportunities:
#                         opportunities = []
#                         opportunities.append(newOpp) # add this opp to the new array
#                         existingLeadMkto.update(opportunities__sfdc = opportunities)
#                         print 'saved opps'
#                     else: # if ['opportunities']['sfdc'] already exists
#                         print 'opp exist'
#                         if not any (e.get('Id', None) == newOpp['Id'] for e in existingLeadMkto.opportunities['sfdc']): # does an opportunity with this Id already exist
#                             opportunities = existingLeadMkto.opportunities['sfdc']
#                             opportunities.append(newOpp)
#                             # save this opportunity        
#                             existingLeadMkto.update(opportunities__sfdc = opportunities)
#                             print 'saved sfdc'
#                         else: #this opp already exists
#                             print 'opp does not exist'
#                             for i in range(len(existingLeadMkto.opportunities['sfdc'])):
#                                 if existingLeadMkto.opportunities['sfdc'][i]['Id'] == newOpp['Id']:
#                                     existingLeadMkto.opportunities['sfdc'][i] = newOpp
#                                     existingLeadMkto.save()
#                                     print 'saved update'
#                     #move on to the next opp
#                     continue
#                 existingLeadHspt = Lead.objects(Q(company_id=company_id) & Q(leads__hspt__properties__salesforceaccountid=sfdc_account_id)).first()
#                 if existingLeadHspt is not None:  # we found a MKto lead record which is matched to this new Sfdc lead
#                     if 'sfdc' not in existingLeadHspt.opportunities:
#                         opportunities = {}
#                         opportunities['sfdc'] = []
#                         opportunities['sfdc'].append(newOpp) # add this opp to the new array
#                         existingLeadHspt.update(opportunities__sfdc = opportunities)
#                     else:
#                         if not any (e.get('Id', None) == newOpp['Id'] for e in existingLeadHspt.opportunities['sfdc']): # does an opportunity with this Id already exist
#                             opportunities = existingLeadHspt.opportunities['sfdc']
#                             opportunities.append(newOpp)
#                             # save this opportunity        
#                             existingLeadHspt.update(opportunities__sfdc = opportunities)
#                         else: #this opp already exists
#                             for i in range(len(existingLeadHspt.opportunities['sfdc'])):
#                                 if existingLeadHspt.opportunities['sfdc'][i]['Id'] == newOpp['Id']:
#                                     existingLeadHspt.opportunities['sfdc'][i] = newOpp
#                                     existingLeadHspt.save()
#                     # move onto the next opp
#                     continue
#                 
#             if existingLeadSfdc is None and existingLeadMkto is None and existingLeadHspt is None:  # no matches found so throw error i,e, not possible
#                 #raise ValueError('Opportunity found without lead or contact')
#                 lead = Lead()
#                 lead.sfdc_id = sfdc_Id
#                 lead.company_id = company_id
#                 lead.source_first_name = newLead['FirstName']
#                 lead.source_last_name = newLead['LastName']
#                 lead.source_email = newLead['Email']
#                 lead.source_created_date = str(newLead['CreatedDate'])
#                 lead.source_source = newLead['LeadSource']
#                 lead.source_status = newLead['Status']
#                 lead.leads["sfdc"] = newLead
#                 print '5th save'
#                 lead.save()
    except Exception as e:
        print 'Error while saving SFDC opportunities ' + str(e)
        send_notification(dict(type='error', success=False, message=str(e))) 
Esempio n. 2
0
def saveSfdcContactsToMaster(user_id=None, company_id=None, job_id=None, run_type=None):
    # delete later
    # job_id = ObjectId("569adcfc8afb00205c799f28")
    if run_type == "initial":
        contacts = TempData.objects(
            Q(company_id=company_id) & Q(record_type="contact") & Q(source_system="sfdc") & Q(job_id=job_id)
        ).only(
            "source_record"
        )  # & Q(job_id=job_id)
    else:
        contacts = TempDataDelta.objects(
            Q(company_id=company_id) & Q(record_type="contact") & Q(source_system="sfdc") & Q(job_id=job_id)
        ).only(
            "source_record"
        )  # & Q(job_id=job_id)
    print "co id is " + str(company_id)
    contactListTemp = list(contacts)
    contactList = [i["source_record"] for i in contactListTemp]
    # print 'saving sfdc contacts'
    try:
        # get the custom field for Contact Status, if it exists
        existingIntegration = CompanyIntegration.objects(company_id=company_id).first()
        contact_status = None
        if "sfdc" in existingIntegration["integrations"]:
            contact_status = existingIntegration["mapping"].get("sfdc_contact_status", None)

        for newContact in contactList:  # ['records']:

            # company_id = request.user.company_id
            sfdc_contact_Id = str(newContact["Id"])
            print "contact id is " + sfdc_contact_Id
            # sfdc_mkto_id = str(newLead['sfdcLeadId']) #check if there is a corresponding lead from MKTO
            existingLeadMkto = None
            existingLeadSfdc = None
            existingLeadHspt = None
            existingContact = Lead.objects(Q(company_id=company_id) & Q(sfdc_contact_id=sfdc_contact_Id)).first()

            if existingContact is not None:  # we found this contact already in the DB
                print "found contact match for " + str(sfdc_contact_Id)
                if "sfdc" in existingContact.contacts:
                    existingContact.source_first_name = newContact["FirstName"]
                    existingContact.source_last_name = newContact["LastName"]
                    existingContact.source_email = newContact["Email"]
                    # existingContact.source_created_date = str(newContact['CreatedDate'])
                    existingContact.source_source = newContact["LeadSource"]
                    if contact_status is not None and contact_status in newContact:
                        existingContact.source_status = newContact[contact_status]
                    existingContact.contacts["sfdc"] = newContact
                    existingContact.save()
                    # Lead.objects(Q(company_id=company_id) & Q(sfdc_contact_id=sfdc_contact_Id)).update(contacts__sfdc=newContact)
                else:
                    existingContact.contacts["sfdc"] = {}
                    existingContact.contacts["sfdc"] = newContact
                    existingContact.save()
            # elif existingContact is None:  # this lead does not exist
            else:
                existingLeadSfdc = (
                    Lead.objects(Q(company_id=company_id) & Q(leads__sfdc__ConvertedContactId=sfdc_contact_Id))
                    .hint("company_id_1_leads.sfdc.convertedContactId_1")
                    .first()
                )
                if existingLeadSfdc is not None:
                    print "found match for sfdc lead for contact " + str(sfdc_contact_Id)
                    # existingLeadSfdcList = list(existingLeadSfdc)
                    existingLeadSfdc.sfdc_contact_id = sfdc_contact_Id
                    if contact_status is not None and contact_status in newContact:
                        existingLeadSfdc.source_status = newContact[contact_status]
                    existingLeadSfdc.contacts = {}
                    existingLeadSfdc.contacts["sfdc"] = newContact
                    existingLeadSfdc.save()
                    # remove below comments after figuring out how Mkto stored SFDC contact ID
                else:
                    existingLeadMkto = Lead.objects(
                        Q(company_id=company_id) & Q(leads__mkto__sfdcContactId=sfdc_contact_Id)
                    ).first()
                    if (
                        existingLeadMkto is not None
                    ):  # we found a MKto lead record which is matched to this new Sfdc lead
                        print "found mkto lead" + existingLeadMkto.mkto_id
                        existingLeadMkto.sfdc_contact_id = sfdc_contact_Id
                        # existingLeadMkto.contacts = {}
                        existingLeadMkto.contacts["sfdc"] = newContact
                        existingLeadMkto.save()
                    else:
                        existingLeadHspt = Lead.objects(
                            Q(company_id=company_id) & Q(leads__hspt__properties__salesforcecontactid=sfdc_contact_Id)
                        ).first()
                        if (
                            existingLeadHspt is not None
                        ):  # we found a MKto lead record which is matched to this new Sfdc lead
                            existingLeadHspt.sfdc_contact_id = sfdc_contact_Id
                            existingLeadHspt.contacts = {}
                            existingLeadHspt.contacts["sfdc"] = newContact
                            existingLeadHspt.save()
            if (
                existingLeadSfdc is None
                and existingLeadMkto is None
                and existingLeadHspt is None
                and existingContact is None
            ):  # no matches found so save new record
                lead = Lead()
                lead.sfdc_contact_id = sfdc_contact_Id
                lead.company_id = company_id
                lead.source_first_name = newContact["FirstName"]
                lead.source_last_name = newContact["LastName"]
                lead.source_email = newContact["Email"]
                lead.source_created_date = str(newContact["CreatedDate"])
                lead.source_source = newContact["LeadSource"]
                if contact_status is not None and contact_status in newContact:
                    lead.source_status = newContact[contact_status]
                lead.contacts = {}
                lead.contacts["sfdc"] = newContact
                lead.save()
            # lead = Lead()
    #             company_id = request.user.company_id
    #             derived_id = 'sfdc_' + str(newLead['Id'])
    #             Lead.objects(derived_id = derived_id).modify(upsert=True, new=True, set__leads__sfdc = newLead, set_on_insert__derived_id = derived_id, set_on_insert__company_id = company_id)
    #
    #             oldLead = Lead.objects(derived_id = lead.derived_id)
    #             if oldLead.count() == 0:
    #                 lead.leads["sfdc"] = newLead
    #                 lead.save()
    #             else:
    #                 oldLead.leads["sfdc"] = newLead
    #                 Lead.objects(derived_id = lead.derived_id).update(oldLead)
    except Exception as e:
        print "exception while saving SFDC contact " + str(e)
        send_notification(dict(type="error", success=False, message=str(e)))