Beispiel #1
0
        public void LocalCrmDatabaseOrganizationServiceExecuteTests_InitializeFromRequest()
        {
            var service  = GetService();
            var currency = new TransactionCurrency();

            currency.Id = service.Create(currency);

            var lead = new LeadBuilder
            {
                Lead = new Lead
                {
                    Address1_Line2                 = "Test Address1_Line2",
                    Address1_Line3                 = "Test Address1_Line3",
                    Description                    = "Test Description",
                    Fax                            = "555-555-1234",
                    JobTitle                       = "Sales Agent",
                    LeadSourceCodeEnum             = Lead_LeadSourceCode.Advertisement,
                    PreferredContactMethodCodeEnum = Lead_PreferredContactMethodCode.Phone,
                    WebSiteUrl                     = "https://github.com/daryllabar/XrmUnitTest",
                    TransactionCurrencyId          = currency.ToEntityReference()
                }
            }
            .WithAddress1()
            .WithAddress2()
            .WithDoNotContact(false)
            .WithEmail()
            .WithName()
            .WithPhone()
            .Build();

            lead.Id = service.Create(lead);
            lead    = service.GetEntity <Lead>(lead.Id);

            var contact = service.InitializeFrom <Contact>(lead.ToEntityReference(), TargetFieldType.ValidForCreate);

            foreach (var attribute in lead.Attributes)
            {
                var key   = attribute.Key;
                var value = attribute.Value;
                switch (key)
                {
                case Lead.Fields.LeadId:
                    key   = Contact.Fields.OriginatingLeadId;
                    value = lead.ToEntityReference();
                    break;

                case Lead.Fields.CreatedOn:
                case Lead.Fields.CreatedBy:
                case Lead.Fields.ModifiedOn:
                case Lead.Fields.ModifiedBy:
                    Assert.IsFalse(contact.Contains(key));
                    continue;
                }

                Assert.IsTrue(
                    contact.Contains(key) && contact[key].Equals(value),
                    $"Field {attribute.Key} was not mapped correctly.");
            }
            service.Create(contact);
        }
        public async Task <int> AddLead(SignUpDTO model)
        {
            // Build Lead
            var buildLead = new LeadBuilder();

            buildLead.Name(model.Lead.EducationType, GetInterestingEvent(), model.Lead.Name);
            buildLead.Price(model.Lead.Price);
            buildLead.Guid(model.Lead.Guid);
            buildLead.Date(model.Lead.Date);
            buildLead.EducationType(model.Lead.EducationForm);
            var lead = (Lead)buildLead;

            try
            {
                currentLogger.LogInformation("Подготовлена сделка для добавления - {@Lead}", lead);

                var query = await crm.Leads.Add(lead.Adapt <LeadDTO>(mapper));

                lead = query.Adapt <Lead>(mapper);

                currentLogger.LogInformation("Создана сделка с сайта Lead Id - {Lead}", lead.Id);
            }
            catch (Exception ex)
            {
                currentLogger.LogWarning(ex, "Ошибка создания сделки с сайта");
                throw new Exception();
            }


            // Add Lead's Notes
            var systemNote = new NoteDTO()
            {
                ElementId   = lead.Id,
                ElementType = (int)ElementTypeEnum.Сделка,
                NoteType    = 25,
                Params      = new NoteParams
                {
                    Text    = "Адрес отправки запроса: Сайт \\ Лендинг",
                    Service = "WebApi | "
                }
            };

            var modelNote = new NoteDTO()
            {
                ElementId   = lead.Id,
                ElementType = (int)ElementTypeEnum.Сделка,
                NoteType    = 4,
                Text        = "Заявка с сайта \r\n"
                              + "ФИО - " + model.Contact.Name + "\r\n" +
                              "Тел.: - " + model.Contact.Phone + "\r\n" +
                              "Email: - " + model.Contact.Email + "\r\n" +
                              "Город - " + model.Contact.City + "\r\n" +
                              "Мероприятие - " + model.Lead.Name + "\r\n" +
                              "Дата - " + model.Lead.Date.ToString("dd-MM-yyyy") + "\r\n"
            };

            try
            {
                await crm.Notes.Add(new [] { systemNote, modelNote });
            }
            catch (Exception ex)
            {
                currentLogger.LogWarning(ex, "Ошибка создания примечания для сделки - " + lead.Id);
            }


            // Contact for Lead
            var contact = await FindOrCreateContact(model);

            await LinkContactToLead(lead.Id, contact.Id);

            return(lead.Id);
        }