Good day!
Task: Upon changing the Type field in the account card, export all records from the section to a CSV file on the user's computer.
You encountered an error message: 'File' does not contain a definition for 'WriteAllText'. However, you have included the using System.IO;
directive.
Code:
namespace Terrasoft.Configuration
{
using Common;
using System;
using System.Linq;
using Terrasoft.Core;
using Terrasoft.Core.Entities;
using Terrasoft.Core.Entities.Events;
using Terrasoft.Core.Factories;
using Newtonsoft.Json;
using System.IO;
#region Class: CHAccount_Custom_1EventListener
[EntityEventListener(SchemaName = "Account")]
public class CHAccount_Custom_1EventListener : BaseEntityEventListener
{
Entity _entity;
UserConnection _userConnection;
public UserConnection UserConnection => _userConnection ?? (_userConnection = _entity?.UserConnection);
#region Methods: Public
public override void OnSaved(object sender, EntityAfterEventArgs e)
{
base.OnSaved(sender, e);
_entity = (Entity)sender;
_userConnection = _entity.UserConnection;
var typeId = _entity.GetTypedColumnValue("TypeId");
// Check if the saving event occurred for the Account object.
if (typeId == new Guid ("f2c0ce97-53e6-df11-971b-001d60e938c6"))
{
// Retrieve data of the accounts from the Account object.
EntitySchemaQuery esq = new EntitySchemaQuery(_entity.Schema);
esq.AddColumn("Name");
esq.AddColumn("CreatedOn");
EntityCollection collection = esq.GetEntityCollection(UserConnection);
if (collection != null && collection.Count > 0)
{
// Create a CSV file.
string csvContent = "Name,Date\n";
foreach (Entity entity in collection)
{
string name = entity.GetTypedColumnValue("Name");
DateTime date = entity.GetTypedColumnValue("CreatedOn");
csvContent += $"{name},{date.ToString("yyyy-MM-dd")}\n";
}
// Save the CSV file to the C drive of the user's computer.
string filePath = @"C:\Accounts.csv";
File.WriteAllText(filePath, csvContent);
}
}
}
#endregion
}
#endregion
}