static void TicketSync() { DateTime?lastSyncRecord = null; void ExecuteFreshdesk() { try { List <FreshdeskTicket> freshdeskTickets = freshdesk.GetTickets().ToList(); if (freshdeskTickets.Any()) { lastSyncRecord = freshdeskTickets.OrderByDescending(t => t.UpdatedAt).First().UpdatedAt.DateTime; } foreach (FreshdeskTicket ticket in freshdeskTickets) { try { Entity ticketEntity = ticketMapper.CreateTicket(ticket); if (ticketEntity != null) { crm.UpsertTicket(ticketEntity); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } ExecuteFreshdesk(); UpdateAppsettings("TicketLastSyncDate", 30, lastSyncRecord); }