Create movement journal through X++ code in D365FO(Import code)
class DAL_MovementJournal
{
public static void main(Args _args)
{
System.IO.Stream stream;
InventJournalTable inventJournalTable;
InventJournalTrans inventJournalTrans,inventJournalTransDel;
InventJournalNameId inventJournalName;
InventDim inventDim;
MainAccount mainAccount;
MainAccountNum MainAccountNum;
SysInfologMessageStruct
infoMessageStruct;
JournalId
journalId;
itemid itemNum;
SysInfoLogEnumerator infoLogEnum;
//NumberSeq
numberseq;
DAL_MovementJournalCreate
DAL_MovementJournalCreate;
str InventTransdate;
str errorMsgs;
container packedlist,errormsg;
List list = new List(Types::String);
ListIterator iterator;
str findim,_bankAccount,_customer,_department,_device,_fixedAsset,_itemGroup,_location,_project,_worker,_BusinessUnit
;
// JournalCheckPost
journalCheckPost;
ExcelSpreadsheetName sheeet;
FileUploadBuild fileUpload;
DialogGroup dlgUploadGroup;
FileUploadBuild fileUploadBuild;
FormBuildControl formBuildControl;
Linenum LineNum;
Dialog dialog = new Dialog("Import the movement details");
;
dlgUploadGroup = dialog.addGroup("Select excel file");
formBuildControl =
dialog.formBuildDesign().control(dlgUploadGroup.name());
fileUploadBuild = formBuildControl.addControlEx(classstr(FileUpload), 'Upload');
fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
fileUploadBuild.fileTypesAccepted('.xlsx');
if (dialog.run() &&
dialog.closedOk())
{
FileUpload
fileUploadControl = dialog.formRun().control(dialog.formRun().controlId('Upload'));
FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();
if (fileUploadResult != null &&
fileUploadResult.getUploadStatus())
{
stream =
fileUploadResult.openResult();
using (OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(stream))
{
int rowCount, i;
Package.Load(stream);
OfficeOpenXml.ExcelWorksheet worksheet = package.get_Workbook().get_Worksheets().get_Item(1);
OfficeOpenXml.ExcelRange range =
worksheet.Cells;
rowCount =
worksheet.Dimension.End.Row - worksheet.Dimension.Start.Row + 1;
try
{
for (i = 2; i<= rowCount; i++)
{
try
{
ttsbegin;
inventJournalTable = InventJournalTable::find(range.get_Item(i, 1).value);
findim = range.get_Item(i, 15).value;
list = Global::strSplit(findim,"|");
iterator = new ListIterator(list);
while (iterator.more())
{
packedlist +=
iterator.value();
iterator.next();
}
_bankAccount =
conPeek(packedlist,1);
_BusinessUnit=
conPeek(packedlist,2);
_customer = conPeek(packedlist,3);
_department = conPeek(packedlist,4);
_device = conPeek(packedlist,5);
_fixedAsset = conPeek(packedlist,6);
_itemGroup = conPeek(packedlist,7);
_location = conPeek(packedlist,8);
_project = conPeek(packedlist,9);
_worker = conPeek(packedlist,10);
packedlist =
conNull();
if(inventJournalTable)
{
select forupdate InventJournalTransDel where
InventJournalTransDel.JournalId == inventJournalTable.JournalId
&&
inventJournalTransDel.LineNum == range.get_Item(i, 16).value;
if(InventJournalTransDel)
{
InventJournalTransDel.delete();
}
//Below code
creates journal lines
InventTransdate =
any2Str(range.get_Item(i, 2).value);
inventJournalTrans.clear();
inventJournalTrans.initFromInventJournalTable(inventJournalTable);
inventJournalTrans.JournalId =
inventJournalTable.JournalId;
journalId = inventJournalTable.JournalId;
inventJournalTrans.JournalType = InventJournalType::Movement;
inventJournalTrans.ItemId = range.get_Item(i, 3).value;
itemNum =
range.get_Item(i, 3).value;
inventJournalTrans.initFromInventTable(InventTable::find(inventJournalTrans.ItemId));
inventDim.configId = range.get_Item(i, 4).value;
inventDim.InventSizeId = range.get_Item(i, 5).value;
inventDim.InventColorId = range.get_Item(i, 6).value;
inventDim.InventStyleId = range.get_Item(i, 7).value;
inventDim.InventSiteId = range.get_Item(i, 8).value;
inventDim.InventLocationId = range.get_Item(i, 9).value;
inventDim.wMSLocationId = range.get_Item(i, 10).value;
inventDim.inventBatchId = range.get_Item(i, 18).value;
inventDim.InventDimension1 = range.get_Item(i, 11).value;
inventJournalTrans.Qty = range.get_Item(i, 12).value;
inventJournalTrans.CostPrice = range.get_Item(i, 13).value;
inventJournalTrans.CostAmount = range.get_Item(i, 14).value;
inventJournalTrans.LineNum = range.get_Item(i, 16).value;
LineNum =
range.get_Item(i, 16).value;
inventJournalTrans.LedgerDimension = LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountId(range.get_Item(i, 17).value);
inventJournalTrans.DefaultDimension = DAL_MovementJournal::getDefaultDimensionfromValues(_bankAccount,_customer,_department,_device,_fixedAsset,_itemGroup,_location,_project,_worker,_BusinessUnit
) ;
inventJournalTrans.TransDate = str2Date(InventTransdate,321);
inventJournalTrans.InventDimId = inventDim::findOrCreate(inventDim).inventDimId;
inventJournalTrans.PriceUnit = 1;
inventJournalTrans.insert();
_bankAccount = "";
_customer = "";
_department = "";
_device = "";
_fixedAsset = "";
_itemGroup = "";
_location = "";
_project = "";
_worker = "";
_BusinessUnit = "";
}
// else
//{
// warning(strFmt
("%1",range.get_Item(i, 1).value));
//}
ttscommit;
}
catch (Exception::Error)
{
infoLogEnum = SysInfoLogEnumerator::newData(infolog.infologData());
while(infoLogEnum.moveNext())
{
infoMessageStruct = SysInfologMessageStruct::construct(infoLogEnum.currentMessage());
if(infoMessageStruct.message() != "Data inserted succesfully")
errorMsgs += "\n"+(infoMessageStruct.message())+"-";
}
DAL_MovementJournalCreate.ItemId = itemNum;
DAL_MovementJournalCreate.ExcelLineNum = i;
DAL_MovementJournalCreate.JournalLineNum
= LineNum;
DAL_MovementJournalCreate.JournalId =
journalId;
DAL_MovementJournalCreate.ExcelName = fileUploadResult.getFileName();
DAL_MovementJournalCreate.ErrorMsg = errorMsgs;//CLRInterop::getLastException().toString();
DAL_MovementJournalCreate.insert();
errorMsgs = "";
infolog.clear();
continue;
}
}
info("Data inserted succesfully");
}
catch (Exception::Error)
{
}
}
}
}
}
public static DimensionDefault
getDefaultDimensionfromValues(DimensionValue _bankAccount,
DimensionValue _customer,
DimensionValue _department,
DimensionValue _device,
DimensionValue _fixedAsset,
DimensionValue _itemGroup,
DimensionValue _location,
DimensionValue _project,
DimensionValue _worker,
DimensionValue _BusinessUnit)
{
DimensionDefault defaultDimension,result;
container conAttr,conValue;
str dimValue;
int counttrans,j;
DimensionAttribute dimensionAttribute;
DimensionAttributeValue
dimensionAttributeValue;
DimensionAttributeValueSetStorage valueSetStorage = new DimensionAttributeValueSetStorage();
conAttr
= ["BankAccount","BusinessUnit","Customer","Department","Device","FixedAsset","ItemGroup","Locations","Project","Worker"];
conValue = [_bankAccount,
_BusinessUnit,_customer,_department,_device,_fixedAsset,
_itemGroup,_location,_project,_worker];
for (j = 1; j <= conLen(conAttr);
j++)
{
dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,j));
if (dimensionAttribute.RecId
== 0)
{
continue;
}
dimValue = conPeek(conValue,j);
{
dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);
valueSetStorage.addItem(dimensionAttributeValue);
}
}
result = valueSetStorage.save();
return result;
}
ReplyDeleteHi, This is a great article. Loved your efforts on it buddy. Thanks for sharing this with us. Microsoft Certified: Dynamics 365: Finance and Operations Apps Developer Associate