/// <summary>
    /// Converts the lead to contact.
    /// </summary>
    /// <param name="sourceLead">The source lead.</param>
    /// <param name="accountId">The account ID.</param>
    /// <param name="createOpportunity">if set to <c>true</c> [create opportunity].</param>
    /// <param name="mergeRule">The merge rule.</param>
    private void ConvertLeadToContact(ILead sourceLead, string accountId, bool createOpportunity, string mergeRule)
    {
        if (accountId != null)
        {
            IAccount account = EntityFactory.GetById<IAccount>(accountId);
            if (account != null)
            {
                var keyGen = new SalesLogixEntityKeyGenerator();
                string key = keyGen.GenerateIds(typeof(IContact), 1).FirstOrDefault();
                IContact contact = EntityFactory.Create<IContact>();
                ((IAssignableId)contact).Id = key;
                string leadHistoryId = sourceLead.SaveLeadHistory();
                sourceLead.ConvertLeadToContact(contact, account, "Add Contact to this Account");

                if (mergeRule.ToUpper().Equals("LEADWINS"))
                {
                    sourceLead.ConvertLeadAddressToContactAddress(contact);
                }
                else
                {
                    contact.Address.Address1 = account.Address.Address1;
                    contact.Address.Address2 = account.Address.Address2;
                    contact.Address.Address3 = account.Address.Address3;
                    contact.Address.Address4 = account.Address.Address4;
                    contact.Address.City = account.Address.City;
                    contact.Address.Country = account.Address.Country;
                    contact.Address.County = account.Address.County;
                    contact.Address.Description = account.Address.Description;
                    contact.Address.PostalCode = account.Address.PostalCode;
                    contact.Address.Salutation = account.Address.Salutation;
                    contact.Address.State = account.Address.State;
                    contact.Address.TimeZone = account.Address.TimeZone;
                    contact.Address.Type = account.Address.Type;
                    contact.Address.GlobalSyncId = account.Address.GlobalSyncId;
                    contact.Address.AppId = account.Address.AppId;
                    contact.Address.Tick = account.Address.Tick;
                }

                sourceLead.MergeLeadWithAccount(account, mergeRule, contact);
                CreateContactLeadSource(sourceLead, contact);
                account.Save();
                contact.Save();

                IOpportunity opportunity = CreateOpportunity(createOpportunity, contact, sourceLead);

                IList<IAttachment> attachment = EntityFactory.GetRepository<IAttachment>().FindByProperty("LeadId",
                                                                                                          sourceLead.Id.
                                                                                                              ToString());
                foreach (IAttachment attach in attachment)
                    sourceLead.AddAttachmentsContactID(contact, account, null, attach);

                sourceLead.AddHistoryAndQualificationRecords(contact, account, opportunity, false);
                sourceLead.AddActivities(contact, account, opportunity);

                IList<ICampaignTarget> campaignTarget =
                    EntityFactory.GetRepository<ICampaignTarget>().FindByProperty("EntityId", sourceLead.Id.ToString());
                foreach (ICampaignTarget ct in campaignTarget)
                    sourceLead.ChangeCampaignTargetEntityID(contact, ct);

                ILeadHistory leadHistory = EntityFactory.GetById<ILeadHistory>(leadHistoryId);
                if (leadHistory != null)
                {
                    leadHistory.ContactId = contact.Id.ToString();
                    leadHistory.AccountId = account.Id.ToString();
                    leadHistory.Save();
                }
                sourceLead.Delete();
                EntityContext.RemoveEntityHistory(typeof (ILead), sourceLead.Id);

                Response.Redirect(
                    opportunity != null
                        ? string.Format("Opportunity.aspx?entityid={0}", opportunity.Id)
                        : string.Format("Contact.aspx?entityId={0}", contact.Id), false);
            }
        }
    }
    private void ConvertLeadToNewAccountAndContact(ILead lead, bool createOpportunity, string options)
    {
        //======================================================================
        // CFX ssommerfeldt Copied from LeadSearchAndConvert.ascx.cs
        //======================================================================
        IOpportunity opportunity = null;
        IContact contact = EntityFactory.Create<IContact>();
        IAccount account = EntityFactory.Create<IAccount>();
        string leadHistoryId = string.Empty;
        ILeadHistory leadHistory = null; EntityFactory.Create<ILeadHistory>();
        leadHistoryId = lead.SaveLeadHistory();

        lead.ConvertLeadToContact(contact, account, options);
        lead.ConvertLeadToAccount(account);
        lead.ConvertLeadAddressToAccountAddress(account);
        lead.ConvertLeadAddressToContactAddress(contact);
        account.Save();
        //==============================================================
        // CFX Customized Below this line ssommerfeldt July 28, 2011
        //===============================================================
        // CFX quite often has contacts with no Person Name
        if (contact.FirstName == null && contact.LastName == null)
        {
            contact.FirstName = "No";
            contact.LastName = "Name";
        }
        try
        {
            contact.SaveContactAccount(account);
        }
        catch (Exception)
        {

            // throw;
        }

        //==============================================================
        // CFX Customized Below this line ssommerfeldt July 28, 2011
        //===============================================================

        if (createOpportunity)
        {
            opportunity = EntityFactory.Create<IOpportunity>();
            opportunity.Account = contact.Account;
            opportunity.Description = string.Format(GetLocalResourceObject("Opportunity_Description").ToString(), lead.LeadNameLastFirst);
            opportunity.Owner = contact.Account.Owner;
            opportunity.AccountManager = contact.Account.AccountManager;
            opportunity.Save();
        }

        AddAttachmentsToLead(lead, account, contact, opportunity);
        lead.AddHistoryAndQualificationRecords(contact, account, opportunity, false);
        lead.AddActivities(contact, account, opportunity);

        IList<ICampaignTarget> campaignTargets = EntityFactory.GetRepository<ICampaignTarget>().FindByProperty("EntityId", lead.Id.ToString());
        foreach (ICampaignTarget campaignTarget in campaignTargets)
            lead.ChangeCampaignTargetEntityID(contact, campaignTarget);

        leadHistory = EntityFactory.GetById<ILeadHistory>(leadHistoryId);
        if (leadHistory != null)
        {
            leadHistory.ContactId = contact.Id.ToString();
            leadHistory.AccountId = account.Id.ToString();
            leadHistory.Save();
        }
        lead.Delete();
        //EntityContext.RemoveEntityHistory(typeof(ILead), lead.Id);

        //if (opportunity != null)
        //{
        //    Response.Redirect(string.Format("Opportunity.aspx?entityid={0}", opportunity.Id), false);
        //}
        //else
        //{
        //    //Response.Redirect(string.Format("Contact.aspx?entityId={0}", contact.Id), false); //CFX
        //    Response.Redirect(string.Format("Account.aspx?entityId={0}", account.Id), false);
        //}
    }
    /// <summary>
    /// Converts the lead to new account and contact.
    /// </summary>
    /// <param name="lead">The lead.</param>
    /// <param name="createOpportunity">if set to <c>true</c> [create opportunity].</param>
    /// <param name="options">The options.</param>
    private void ConvertLeadToNewAccountAndContact(ILead lead, bool createOpportunity, string options)
    {
        IOpportunity opportunity = null;
        IContact contact = EntityFactory.Create<IContact>();
        IAccount account = EntityFactory.Create<IAccount>();
        string leadHistoryId = string.Empty;
        ILeadHistory leadHistory = null; EntityFactory.Create<ILeadHistory>();
        leadHistoryId = lead.SaveLeadHistory();

        lead.ConvertLeadToContact(contact, account, options);
        lead.ConvertLeadToAccount(account);
        lead.ConvertLeadAddressToAccountAddress(account);
        lead.ConvertLeadAddressToContactAddress(contact);
        account.Save();
         //==================================================
        // CFX Code here
        //==================================================
        foreach (ILeadWebsite tmpWebsite in lead.LeadWebsites)
        {
            // Create Account Additional Website Record
            Sage.Entity.Interfaces.IAccountWebsite Acctwebsite = Sage.Platform.EntityFactory.Create<Sage.Entity.Interfaces.IAccountWebsite>();
            Acctwebsite.Account = account;
            Acctwebsite.Notes = tmpWebsite.Notes;
            Acctwebsite.WebAddress = tmpWebsite.WebAddress;

            try
            {
                Acctwebsite.Save();
            }
            catch (Exception)
            {

                //Exception But Continue
            }
        }
        // ===========================================================
        // End CFX Code
        //================================================================
        contact.SaveContactAccount(account);

        if (createOpportunity)
        {
            opportunity = EntityFactory.Create<IOpportunity>();
            opportunity.Account = contact.Account;
            opportunity.Description = string.Format(GetLocalResourceObject("Opportunity_Description").ToString(), lead.LeadNameLastFirst);
            opportunity.Owner = contact.Account.Owner;
            opportunity.AccountManager = contact.Account.AccountManager;
            opportunity.Save();
        }

        AddAttachmentsToLead(lead, account, contact, opportunity);
        lead.AddHistoryAndQualificationRecords(contact, account, opportunity, false);
        lead.AddActivities(contact, account, opportunity);

        IList<ICampaignTarget> campaignTargets = EntityFactory.GetRepository<ICampaignTarget>().FindByProperty("EntityId", lead.Id.ToString());
        foreach (ICampaignTarget campaignTarget in campaignTargets)
            lead.ChangeCampaignTargetEntityID(contact, campaignTarget);

        leadHistory = EntityFactory.GetById<ILeadHistory>(leadHistoryId);
        if (leadHistory != null)
        {
            leadHistory.ContactId = contact.Id.ToString();
            leadHistory.AccountId = account.Id.ToString();
            leadHistory.Save();
        }
        lead.Delete();
        EntityContext.RemoveEntityHistory(typeof(ILead), lead.Id);

        if (opportunity != null)
        {
            Response.Redirect(string.Format("Opportunity.aspx?entityid={0}", opportunity.Id),false);
        }
        else
        {
            Response.Redirect(string.Format("Contact.aspx?entityId={0}", contact.Id), false);
        }
    }
    /// <summary>
    /// Converts the lead to contact.
    /// </summary>
    /// <param name="sourceLead">The source lead.</param>
    /// <param name="accountID">The account ID.</param>
    private void ConvertLeadToContact(ILead sourceLead, string accountID, bool createOpportinity, string mergeRule)
    {
        if (accountID != null)
        {
            IAccount account = EntityFactory.GetById<IAccount>(accountID);
            if (account != null)
            {
                IContact contact = EntityFactory.Create<IContact>();
                IOpportunity opportunity = null;

                ILeadHistory leadHistory = null;
                string leadHistoryId = string.Empty;

                leadHistoryId = sourceLead.SaveLeadHistory();

                sourceLead.ConvertLeadToContact(contact, account, "Add Contact to this Account");

                if (mergeRule.ToUpper().Equals("LEADWINS"))
                {
                    sourceLead.ConvertLeadAddressToContactAddress(contact);
                    //sourceLead.ConvertLeadAddressToAccountAddress(account);
                }
                else
                {

                    contact.Address.Address1 = account.Address.Address1;
                    contact.Address.Address2 = account.Address.Address2;
                    contact.Address.Address3 = account.Address.Address3;
                    contact.Address.Address4 = account.Address.Address4;
                    contact.Address.City = account.Address.City;
                    contact.Address.Country = account.Address.Country;
                    contact.Address.County = account.Address.County;
                    contact.Address.Description = account.Address.Description;
                    contact.Address.PostalCode = account.Address.PostalCode;
                    contact.Address.Salutation = account.Address.Salutation;
                    contact.Address.State = account.Address.State;
                    contact.Address.TimeZone = account.Address.TimeZone;
                    contact.Address.Type = account.Address.Type;
                }

                sourceLead.MergeLeadWithAccount(account, mergeRule, contact);
                account.Save();
                contact.Save();

                if (createOpportinity)
                {
                    opportunity = EntityFactory.Create<IOpportunity>();
                    opportunity.Account = contact.Account;
                    opportunity.Description = string.Format("Opportunity for {0}", sourceLead.LeadNameLastFirst);
                    opportunity.Owner = contact.Account.Owner;
                    opportunity.AccountManager = contact.Account.AccountManager;
                    opportunity.Save();
                }

                IList<IAttachment> attachment = EntityFactory.GetRepository<IAttachment>().FindByProperty("LeadId", sourceLead.Id.ToString());
                foreach (IAttachment attach in attachment)
                    sourceLead.AddAttachmentsContactID(contact, account, null, attach);

                sourceLead.AddHistoryAndQualificationRecords(contact, account, opportunity, false);
                sourceLead.AddActivities(contact, account, opportunity);

                IList<ICampaignTarget> campaignTarget = EntityFactory.GetRepository<ICampaignTarget>().FindByProperty("EntityId", sourceLead.Id.ToString());
                foreach (ICampaignTarget ct in campaignTarget)
                    sourceLead.ChangeCampaignTargetEntityID(contact, ct);

                leadHistory = EntityFactory.GetById<ILeadHistory>(leadHistoryId);
                if (leadHistory != null)
                {
                    leadHistory.ContactId = contact.Id.ToString();
                    leadHistory.AccountId = account.Id.ToString();
                    leadHistory.Save();
                }
                sourceLead.Delete();
                EntityContext.RemoveEntityHistory(typeof(ILead), sourceLead.Id);

                if (opportunity != null)
                {
                    Response.Redirect(string.Format("Opportunity.aspx?entityid={0}", opportunity.Id), false);
                }
                else
                {
                    Response.Redirect(string.Format("Contact.aspx?entityId={0}", contact.Id),false);
                }
            }
        }
    }
    /// <summary>
    /// Converts the lead to new account and contact.
    /// </summary>
    /// <param name="lead">The lead.</param>
    /// <param name="createOpportunity">if set to <c>true</c> [create opportunity].</param>
    /// <param name="options">The options.</param>
    private void ConvertLeadToNewAccountAndContact(ILead lead, bool createOpportunity, string options)
    {
        var keyGen = new SalesLogixEntityKeyGenerator();
        string key = keyGen.GenerateIds(typeof(IContact), 1).FirstOrDefault();
        IContact contact = EntityFactory.Create<IContact>();
        ((IAssignableId)contact).Id = key;
        IAccount account = EntityFactory.Create<IAccount>();

        lead.ConvertLeadToContact(contact, account, options);
        lead.ConvertLeadToAccount(account);
        lead.ConvertLeadAddressToAccountAddress(account);
        lead.ConvertLeadAddressToContactAddress(contact);
        contact.SaveContactAccount(account);
        IOpportunity opportunity = CreateOpportunity(createOpportunity, contact, lead);

        AddAttachmentsToLead(lead, account, contact, opportunity);
        lead.AddHistoryAndQualificationRecords(contact, account, opportunity, false);
        lead.AddActivities(contact, account, opportunity);

        IList<ICampaignTarget> campaignTargets = EntityFactory.GetRepository<ICampaignTarget>().FindByProperty("EntityId", lead.Id.ToString());
        foreach (ICampaignTarget campaignTarget in campaignTargets)
            lead.ChangeCampaignTargetEntityID(contact, campaignTarget);

        ILeadHistory leadHistory = EntityFactory.GetById<ILeadHistory>(lead.SaveLeadHistory());
        if (leadHistory != null)
        {
            leadHistory.ContactId = contact.Id.ToString();
            leadHistory.AccountId = account.Id.ToString();
            leadHistory.Save();
        }
        lead.Delete();
        EntityContext.RemoveEntityHistory(typeof(ILead), lead.Id);

        Response.Redirect(
            opportunity != null
                ? string.Format("Opportunity.aspx?entityid={0}", opportunity.Id)
                : string.Format("Contact.aspx?entityId={0}", contact.Id), false);
    }
Esempio n. 6
0
    /// <summary>
    /// Converts the lead to new account and contact.
    /// </summary>
    /// <param name="lead">The lead.</param>
    /// <param name="createOpportunity">if set to <c>true</c> [create opportunity].</param>
    /// <param name="options">The options.</param>
    private void ConvertLeadToNewAccountAndContact(ILead lead, bool createOpportunity, string options)
    {
        IOpportunity opportunity = null;
        IContact contact = EntityFactory.Create<IContact>();
        IAccount account = EntityFactory.Create<IAccount>();
        string leadHistoryId = string.Empty;
        ILeadHistory leadHistory = null; EntityFactory.Create<ILeadHistory>();
        leadHistoryId = lead.SaveLeadHistory();

        lead.ConvertLeadToContact(contact, account, options);
        lead.ConvertLeadToAccount(account);
        lead.ConvertLeadAddressToAccountAddress(account);
        lead.ConvertLeadAddressToContactAddress(contact);
        account.Save();
        contact.SaveContactAccount(account);

        if (createOpportunity)
        {
            opportunity = EntityFactory.Create<IOpportunity>();
            opportunity.Account = contact.Account;
            opportunity.Description = string.Format(GetLocalResourceObject("Opportunity_Description").ToString(), lead.LeadNameLastFirst);
            opportunity.Owner = contact.Account.Owner;
            opportunity.AccountManager = contact.Account.AccountManager;
            opportunity.Save();
        }

        AddAttachmentsToLead(lead, account, contact, opportunity);
        lead.AddHistoryAndQualificationRecords(contact, account, opportunity, false);
        lead.AddActivities(contact, account, opportunity);

        IList<ICampaignTarget> campaignTargets = EntityFactory.GetRepository<ICampaignTarget>().FindByProperty("EntityId", lead.Id.ToString());
        foreach (ICampaignTarget campaignTarget in campaignTargets)
            lead.ChangeCampaignTargetEntityID(contact, campaignTarget);

        leadHistory = EntityFactory.GetById<ILeadHistory>(leadHistoryId);
        if (leadHistory != null)
        {
            leadHistory.ContactId = contact.Id.ToString();
            leadHistory.AccountId = account.Id.ToString();
            leadHistory.Save();
        }
        lead.Delete();
        EntityContext.RemoveEntityHistory(typeof(ILead), lead.Id);

        if (opportunity != null)
        {
            Response.Redirect(string.Format("Opportunity.aspx?entityid={0}", opportunity.Id),false);
        }
        else
        {
            Response.Redirect(string.Format("Contact.aspx?entityId={0}", contact.Id), false);
        }
    }