public void Execute() { MetadataHelper metadataHelper = new MetadataHelper(Service); LogWriter.LogInfo("ExportCommand: Retrieving entity list"); typeCodeList = metadataHelper.RetrieveEntityList(); FetchXmlHelper fetchXmlHelper = new FetchXmlHelper(); LogWriter.LogInfo("ExportCommand: Buidling Fetch XML"); string fetchXml = fetchXmlHelper.Build(entityName, metadataHelper.RetrieveEntity(entityName).EntityMetadata.Attributes); LogWriter.LogInfo(fetchXml); LogWriter.LogInfo($"ExportCommand: Exporting data from {OrganisationUrl}"); EntityCollection entities = Service.RetrieveMultiple(new FetchExpression(fetchXml)); LogWriter.LogInfo($"ExportCommand: Retrieved data, Count: {entities.Entities.Count}"); Parallel.ForEach(entities.Entities, entity => { entity["entityname"] = getEntityName(entity); } ); EntityCollectionSerializer ecSerializer = new EntityCollectionSerializer() { FileName = FileName, }; LogWriter.LogInfo("ExportCommand: Serializing data"); ecSerializer.Serialize(entities); compressFile(); }
private async Task SyncStockItems() { Log.Information("Items sync started on {Started}.", DateTime.Now); var itemsResult = _stockRepository.GetAllItems(); if (itemsResult.IsSuccess) { foreach (StockItem item in itemsResult.Value) { Log.Debug("Stock Item No is {StockItemNo}", item.StockCode); await _crmRepository.GetWithFetchXmlAsync <Product>( PluralNameConstants.Products, FetchXmlHelper.GetProductBy(item.StockCode)) .Tap(product => UpsertProduct(product, item) .Tap(productId => UpsertPriceLevel(productId, item)) .Tap(productId => SetActiveProductStatus(productId))); } } }
private async Task <Result <Guid> > UpsertPriceLevel(Guid productId, StockItem stockItem) { return(await _crmRepository.GetWithFetchXmlAsync <ProductPriceLevel>( PluralNameConstants.ProductPriceLevels, FetchXmlHelper.FindProductPriceLevelBy( productId, _options.SalesOptions.DefaultPriceList)) .Bind(priceLevel => { Task <Result <Guid> > upsertResult; if (priceLevel == null) { priceLevel = new ProductPriceLevel { Id = Guid.NewGuid(), DefaultPriceList = _options.SalesOptions.DefaultPriceList, ProductId = productId, DefaultUomId = _options.SalesOptions.UoMId, Amount = stockItem.ResellerPriceInc }; upsertResult = _crmRepository.CreateAsync(PluralNameConstants.ProductPriceLevels, priceLevel) .Tap(id => Log.Debug("Product({@ProductId}) Price was created on {@CreatedOn}.", productId, DateTime.Now)); } else { if (priceLevel.Amount == stockItem.ResellerPriceInc) { return Task.FromResult( Result.Ok(priceLevel.Id) .Tap(id => Log.Information("Product Price for Product({@ProductPriceId}) was unchanged.", id))); } priceLevel.Amount = Math.Round(stockItem.ResellerPriceInc, 2); upsertResult = _crmRepository.UpdateAsync(PluralNameConstants.ProductPriceLevels, priceLevel, priceLevel.Id) .Tap(id => Log.Debug("Product({@ProductId}) Price was updated on {@ModifiedOn}.", productId, DateTime.Now)); } return upsertResult; })); }