Wednesday, 30 January 2019

employee dimension import in D365

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;
class INTC_EmployeeDefaultDimensionImport
{
    public static void main(Args _args)
    {
        System.IO.Stream            stream;
        ExcelSpreadsheetName        sheeet;
        FileUploadBuild             fileUpload;
        DialogGroup                 dlgUploadGroup;
        FileUploadBuild             fileUploadBuild;
        FormBuildControl            formBuildControl;
        container                   conDimValue;
        container                   conVal;
        int64                       ledgerLoc;
        str                         dimValue;
        HcmPersonnelNumberId        personnelNum;


        hcmWorker                           hcmWorker, hcmWorkerLocal;
        HcmEmployment                       hcmEmployment;
        DimensionValueName           tmpLegal_Entity, tmpOffice_Location, tmpProject_Location, tmpDepartment, tmpService_Line, tmpSub_Service_Line, tmpIndustry,
                                            tmpSub_Industry, tmpCoIndustry, tmpPartner, tmpPM_Employee, tmpProject;
        DimensionAttribute                  dimensionAttribute;
        DimensionAttributeValue             dimensionAttributeValue;
        DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();
        DimensionDefault                    result;
        container                           conAttr = [ "A_Legal_Entity","B_Office_Location","C_Project_Location","D_Department","E_Service_Line","F_Sub_Service_Line","G_Partner","H_Industry","I_Sub_Industry","J_CoIndustry","PM_Employee","Project"];

        Dialog                      dialog = new Dialog("Import employee default dimenson from Excel");

        dlgUploadGroup          = dialog.addGroup("@SYS54759");
        formBuildControl        = dialog.formBuildDesign().control(dlgUploadGroup.name());
        fileUploadBuild         = formBuildControl.addControlEx(classstr(FileUpload), "Import");

        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
        fileUploadBuild.fileTypesAccepted('.xlsx');

        if (dialog.run() && dialog.closedOk())
        {

            FileUpload fileUploadControl     = dialog.formRun().control(dialog.formRun().controlId('Import'));
            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

            if (fileUploadResult != null && fileUploadResult.getUploadStatus())
            {
                stream = fileUploadResult.openResult();
                using (ExcelPackage Package = new ExcelPackage(stream))
                {
                    int                         rowCount, i, j;
                    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;

                    for (i = 2; i<= rowCount; i++)
                    {
                        personnelNum                = range.get_Item(i, 1).value;
                        tmpLegal_Entity             = range.get_Item(i, 2).value;
                        tmpOffice_Location          = range.get_Item(i, 3).value;
                        tmpProject_Location         = range.get_Item(i, 4).value;
                        tmpDepartment               = range.get_Item(i, 5).value;
                        tmpService_Line             = range.get_Item(i, 6).value;
                        tmpSub_Service_Line         = range.get_Item(i, 7).value;
                        tmpPartner                  = range.get_Item(i, 8).value;
                        tmpIndustry                 = range.get_Item(i, 9).value;
                        tmpSub_Industry             = range.get_Item(i, 10).value;
                        tmpCoIndustry               = range.get_Item(i, 11).value;
                        tmpPM_Employee              = range.get_Item(i, 12).value;
                        tmpProject                  = range.get_Item(i, 13).value;

                        hcmworker       = hcmWorker::findByPersonnelNumber(personnelNum);

                        conVal          = [tmpLegal_Entity, tmpOffice_Location, tmpProject_Location, tmpDepartment, tmpService_Line, tmpSub_Service_Line,tmpPartner, tmpIndustry,
                                            tmpSub_Industry, tmpCoIndustry, tmpPM_Employee, tmpProject];
                     
                        if(hcmworker)
                        {
                            select forupdate hcmEmployment where hcmEmployment.Worker == hcmworker.RecId;
                            if(hcmEmployment)
                            {
                                for (j = 1; j <= conLen(conAttr); j++)
                                {
                                    dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,j));
                                    if (dimensionAttribute.RecId == 0)
                                    {
                                        continue;
                                    }
                                    dimValue = conPeek(conVal,j);
                                    if (dimValue != "")
                                    {
                                        dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);
                                        valueSetStorage.addItem(dimensionAttributeValue);
                                    }
                                    else
                                    {
                                        valueSetStorage.removeDimensionAttribute(DimensionAttribute::findByName(conPeek(conAttr,j)).RecId);
                                    }
                                }
                                result = valueSetStorage.save();
                                ttsbegin;
                                hcmEmployment.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
                                hcmEmployment.DefaultDimension      = result;
                                hcmEmployment.update();
                                ttscommit;
                                personnelNum            = '';
                                tmpLegal_Entity         = '';
                                tmpOffice_Location      = '';
                                tmpProject_Location     = '';
                                tmpDepartment           = '';
                                tmpService_Line         = '';
                                tmpSub_Service_Line     = '';
                                tmpIndustry             = '';
                                tmpSub_Industry         = '';
                                tmpCoIndustry           = '';
                                tmpPartner              = '';
                                tmpPM_Employee          = '';
                                tmpProject              = '';
                            }
                        }
                        else
                        {
                            Error("Employee ID %1 Not exists ", personnelNum);
                        }
                    }
                }
            }
            else
            {
                error("Error");
            }
        }
    }

}

No comments:

Post a Comment