Just like I mentioned in my previous post regarding the upload of WBS Data and the challenges developers face similarly we sometimes get the requirement of writing the code for uploading the WBS Quotation Data from MS Excel in to D365 Fin Ops Masters.
The Entire Code Construct is same for this as it was there in my last post except the Data entity name which will be used in this scenario will be different.
Data Entity which we have to use is "ProjQuotationWBSEntity".
Let's see how its done in the code given below : -
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
class WBSProjQuotationExcelUpload
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
MemoryStream memoryStream = new MemoryStream();
WBSProjQuotationExcelUpload importWBS = WBSProjQuotationExcelUpload::construct();
ProjProjectWBSDraftEntity projectwbsentity;
ProjWBSActivityEstimatesEntity activityEstimateEntity;
ProjPlanVersion projPlanVersion,projPlanVersionProcess,projPlanVersioncount;
FormRun formRun;
projPlanVersion = _args.record();
formRun = _args.caller();
if (Box::okCancel("Do you want to Upload Excel ?", DialogButton::Cancel) == DialogButton::Ok)
{
select count(RecId) from projPlanVersioncount
where projPlanVersioncount.HierarchyId == projPlanVersion.HierarchyId;
if (projPlanVersioncount.RecId != 1)
{
formRun.deleteRecords();
}
ttsbegin;
update_recordset projPlanVersionProcess
setting ProcessedEstimation = NoYes::No
where projPlanVersionProcess.HierarchyId == projPlanVersion.HierarchyId;
ttscommit;
importWBS.import();
}
}
public static WBSProjQuotationExcelUpload construct()
{
return new WBSProjQuotationExcelUpload();
}
public void import()
{
System.IO.Stream stream;
ExcelSpreadsheetName sheet;
FileUploadBuild fileUpload;
DialogGroup dlgUploadGroup;
FileUploadBuild fileUploadBuild;
FormBuildControl formBuildControl;
ProjQuotationWbsEntity wbsEntity;
Dialog dialog = new Dialog('Import Project Quotation WBS from excel');
dlgUploadGroup = dialog.addGroup("Upload WBS Quotation Group");
formBuildControl = dialog.formBuildDesign().control(dlgUploadGroup.name());
fileUploadBuild = formBuildControl.addControlEx(classStr(FileUpload), 'UploadWBSQuotation');
fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
fileUploadBuild.fileTypesAccepted('.xlsx');
if(dialog.run() && dialog.closedOk())
{
FileUpload fileUploadControl = dialog.formRun().control(dialog.formRun().controlId('UploadWBSQuotation'));
FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();
if (fileUploadResult != null && fileUploadResult.getUploadStatus())
{
stream = fileUploadResult.openResult();
using (ExcelPackage Package = new ExcelPackage(stream))
{
int rowCount, i;
Package.Load(stream);
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;
ttsbegin;
for (i = 2; i<= rowCount; i++)
{
wbsEntity.QuotationId = range.get_Item(i, 1).Value;
wbsEntity.WBSId = range.get_Item(i, 2).Value;
wbsEntity.Reference = range.get_Item(i, 3).Value;
wbsEntity.TaskID = range.get_Item(i, 4).Value;
wbsEntity.Task = range.get_Item(i, 5).Value;
wbsEntity.Note = range.get_Item(i, 6).Value;
wbsEntity.Category = ProjParameters::find().EmplCategory;
wbsEntity.Quantity = range.get_Item(i, 7).Value;
wbsEntity.ExpensePercent = range.get_Item(i, 8).Value;
wbsEntity.MarkupPercent = range.get_Item(i, 9).Value;
wbsEntity.StartDate = str2DateDMY(range.get_Item(i, 10).Value);
wbsEntity.EndDate = str2DateDMY(range.get_Item(i, 11).Value);
wbsEntity.insert();
}
ttscommit;
info(strFmt('%1 records processed', rowCount-1));
}
}
}
}
}
Happy Coding.
Please type in the comments section if you have any queries.
No comments:
Post a Comment