コード例 #1
0
        static void ContactSync()
        {
            DateTime?lastSyncRecord = null;

            void ExecuteFreshdesk()
            {
                try
                {
                    IEnumerable <FreshdeskContactDto> contactDtos = freshdesk.GetContacts();

                    if (contactDtos.Any())
                    {
                        lastSyncRecord = contactDtos.OrderByDescending(t => t.UpdatedAt).First().UpdatedAt.DateTime;
                    }

                    foreach (FreshdeskContactDto contact in contactDtos)
                    {
                        try
                        {
                            Entity contactEntity = contactMapper.CreateContact(contact);

                            crm.UpsertContact(contactEntity);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            ExecuteFreshdesk();

            /*
             *
             * int page = 1;
             * var crmContacts = crm.GetContacts(500, page);
             *
             * void ExecuteCrm()
             * {
             *  foreach (var contact in crmContacts.Entities)
             *  {
             *      try
             *      {
             *          FreshdeskContactForCreation freshdeskContact = contactMapper.CreateContact(contact);
             *
             *
             *          if (contact.Contains("isw_freshdeskid"))
             *          {
             *              long freshdeskId = long.Parse(contact["isw_freshdeskid"].ToString());
             *              freshdesk.UpdateContact(freshdeskId, freshdeskContact);
             *          }
             *
             *          FreshdeskContactDto createdFreshdeskContact = freshdesk.CreateContact(freshdeskContact);
             *
             *          Entity contactEntity = contactMapper.CreateContact(createdFreshdeskContact);
             *
             *          crm.UpsertContact(contactEntity);
             *      }
             *      catch (Exception)
             *      {
             *      }
             *  }
             * }
             *
             * do
             * {
             *  ExecuteCrm();
             *  page++;
             *
             *  if (crmContacts.MoreRecords)
             *      crmContacts = crm.GetAccounts(500, page, crmContacts.PagingCookie);
             *  else
             *      break;
             * } while (crmContacts.MoreRecords);
             *
             */

            UpdateAppsettings("ContactLastSyncDate", 30, lastSyncRecord);
        }