Hi,
Import CSV file into Dynamics 365 using X++
class ShyamCSVRead
{
///
/// Runs the class with the specified arguments.
///
/// The specified arguments.
public static void main(Args _args)
{
AsciiStreamIo file;
Array fileLines;
FileUploadTemporaryStorageResult fileUpload;
ShyamCSVRead csvRead=new ShyamCSVRead();
//Upload a file
fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
file = AsciiStreamIo::constructForRead(fileUpload.openResult());
if (file)
{
if (file.status())
{
throw error("@SYS52680");
}
file.inFieldDelimiter(','); // CSV must have Comma , separator
file.inRecordDelimiter('\r\n');
}
//Read a CSV File
container rec;
while (!file.status())
{
rec = file.read();
if (conLen(rec))
{
info(strFmt("%1 - %2",conPeek(rec,1),conPeek(rec,2)));
//With Converter for all type Especially for date columns
info(strFmt("%1",csvRead.getCSVValueConversion(tableNum(CustTrans ), fieldNum(CustTrans, Voucher), conPeek(rec, 1))));
info(strFmt("%1",csvRead.getCSVValueConversion(tableNum(CustTrans), fieldNum(CustTrans, TransDate ), str2Date(conPeek(rec, 2),321)))));
//str2Date Sample
// https://msdn.microsoft.com/en-us/library/aa554244.aspx
}
}
info("done");
}
/// <summary>
/// Converts value read from CSV file to it's corresponding data type.
/// </summary>
/// <param name="_tableId">
/// ID of the table to which field belongs to.
/// </param>
/// <param name="_fieldId">
/// ID of the field.
/// </param>
/// <param name="_value">
/// Value got from CSV file.
/// </param>
/// <returns>
/// Value converted into corresponding data type.
/// </returns>
public anytype getCSVValueConversion(TableId _tableId,
FieldId _fieldId,
anytype _value)
{
DictField dictField;
DictType dictType;
Types fieldType;
;
dictField = new DictField(_tableId, _fieldId);
// Get the field type.
dictType = new DictType(dictField.typeId());
if(dictType)
{
fieldType = dictType.baseType();
}
else
{
fieldType = dictField.baseType();
}
// Return value in a correct type.
switch (fieldType)
{
case Types::Date:
return any2date(_value);
case Types::Enum:
return any2enum(_value);
case Types::Int64:
return any2int64(_value);
case Types::Integer:
return any2int(_value);
case Types::Real:
return any2real(_value);
case Types::String:
return any2str(_value);
case Types::Time:
return str2time(_value);
}
return _value;
}
}
Reference
Reference
Reference
Reference
Reference
Import EXCEL in D365
http://daxture.blogspot.ae/2017/11/read-excel-through-x-in-d365fo.html
Import CSV file into Dynamics 365 using X++
class ShyamCSVRead
{
///
/// Runs the class with the specified arguments.
///
/// The specified arguments.
public static void main(Args _args)
{
AsciiStreamIo file;
Array fileLines;
FileUploadTemporaryStorageResult fileUpload;
ShyamCSVRead csvRead=new ShyamCSVRead();
//Upload a file
fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
file = AsciiStreamIo::constructForRead(fileUpload.openResult());
if (file)
{
if (file.status())
{
throw error("@SYS52680");
}
file.inFieldDelimiter(','); // CSV must have Comma , separator
file.inRecordDelimiter('\r\n');
}
//Read a CSV File
container rec;
while (!file.status())
{
rec = file.read();
if (conLen(rec))
{
info(strFmt("%1 - %2",conPeek(rec,1),conPeek(rec,2)));
//With Converter for all type Especially for date columns
info(strFmt("%1",csvRead.getCSVValueConversion(tableNum(CustTrans ), fieldNum(CustTrans, Voucher), conPeek(rec, 1))));
info(strFmt("%1",csvRead.getCSVValueConversion(tableNum(CustTrans), fieldNum(CustTrans, TransDate ), str2Date(conPeek(rec, 2),321)))));
//str2Date Sample
// https://msdn.microsoft.com/en-us/library/aa554244.aspx
}
}
info("done");
}
/// <summary>
/// Converts value read from CSV file to it's corresponding data type.
/// </summary>
/// <param name="_tableId">
/// ID of the table to which field belongs to.
/// </param>
/// <param name="_fieldId">
/// ID of the field.
/// </param>
/// <param name="_value">
/// Value got from CSV file.
/// </param>
/// <returns>
/// Value converted into corresponding data type.
/// </returns>
public anytype getCSVValueConversion(TableId _tableId,
FieldId _fieldId,
anytype _value)
{
DictField dictField;
DictType dictType;
Types fieldType;
;
dictField = new DictField(_tableId, _fieldId);
// Get the field type.
dictType = new DictType(dictField.typeId());
if(dictType)
{
fieldType = dictType.baseType();
}
else
{
fieldType = dictField.baseType();
}
// Return value in a correct type.
switch (fieldType)
{
case Types::Date:
return any2date(_value);
case Types::Enum:
return any2enum(_value);
case Types::Int64:
return any2int64(_value);
case Types::Integer:
return any2int(_value);
case Types::Real:
return any2real(_value);
case Types::String:
return any2str(_value);
case Types::Time:
return str2time(_value);
}
return _value;
}
}
Reference
Reference
Reference
Reference
Reference
Import EXCEL in D365
http://daxture.blogspot.ae/2017/11/read-excel-through-x-in-d365fo.html
subcupda-su William Walker https://wakelet.com/wake/Gop77Hg6w8OiFAofppocR
ReplyDeletepassnewmesi