Wednesday, June 26, 2013

How to print export Dimensions

static void DAXPrintEmplDim(Args _args)
{

    HcmWorker worker;
    HcmEmployment employment;
    #AviFiles
    SysOperationProgress    progressBar = new SysOperationProgress();
    // Method to retuen dimension name
    str getDefaultDimensionValue(RecId defaultDimension, Name dimName)
    {
        str 255 ret;
        DimensionAttributeValueSetStorage dimStorage;
        dimStorage = DimensionAttributeValueSetStorage::find(defaultDimension);
        ret = dimStorage.getDisplayValueByDimensionAttribute(DimensionAttribute::findByName(dimName).RecId);
        return ret;
    }  
    // Initialising progress bar
    progressBar.setCaption("Printing in progress...");
    progressBar.setAnimation(#AviTransfer);

    while select worker
    {
        progressBar.setText(strfmt("Employee %1", worker.name()));
        employment = HcmEmployment::getActiveEmploymentsByWorker(worker.RecId);
        if(employment.DefaultDimension)
        {
            info(strFmt("%1, %2, Department Value: %3", worker.PersonnelNumber, worker.name(), getDefaultDimensionValue(employment.DefaultDimension, "Department")));
        }
    }
}

How to create employee ID auto numbering system

public static str getNewPersonnelNumber()
{
    int maxLenght = 4;
    str tempEmployeeID;
    str newNumber;
    int newIndex;
    str companyIdx;
    HCMWorker localWorker;
    str ret;
    str 2 companyIdFilter;

    //Pickup the first digit as company name
    companyIdx = String::trimToLength(curext(), 1);
    companyIdx = strUpr(companyIdx);
    companyIdFilter = companyIdx + '*';
    select firstOnly localWorker
        order by localWorker.PersonnelNumber desc
        where localWorker.PersonnelNumber like companyIdFilter;
    tempEmployeeID = localWorker.PersonnelNumber;
    newNumber = String::replaceAll(tempEmployeeID, companyIdx, "");
    newIndex = str2int(newNumber);
    newIndex = newIndex + 1;
    newNumber = String::fillString(maxLenght - strLen(int2str(newIndex)), '0') + int2str(newIndex);
    newNumber = companyIdx + newNumber;
    ret  = newNumber;

    return ret;
}

Tuesday, June 25, 2013

Dynamics AX 2012 Import HR Postions X++ code

static void ImportPosition(Args _args)
{
SysExcelApplication     application;
SysExcelWorkbooks       workbooks;
SysExcelWorkbook        workbook;
SysExcelWorksheets      worksheets;
SysExcelWorksheet       worksheet;
SysExcelCells           cells;
COMVariantType          type;
OMOperatingUnit         OMOperatingUnit;
int                     row=1;

Name                                name;
FileName                            filename;
HcmPosition                         HcmPosition;
HcmPositionDetail                   HcmPositionDetail;
HcmPositionWorkerAssignment         HcmPositionWorkerAssignment;
HcmPositionDuration                 HcmPositionDuration;
str                                 job;
OMOperatingUnitNumber               department;

;
application = SysExcelApplication::construct();
workbooks   = application.workbooks();

filename = "C:\\import\\HRPosition.xlsx";
ttsBegin;

try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
do
{
row++;

    HcmPosition.PositionId = cells.item(row, 1).value().bStr();
    HcmPosition.insert();
    department = cells.item(row, 2).value().bStr();
    job = cells.item(row, 3).value().bStr();
    select firstOnly OMOperatingUnit where OMOperatingUnit.OMOperatingUnitNumber == department;
    if(OMOperatingUnit)
    {
        HcmPositionDetail.Department    = OMOperatingUnit.RecId;
        HcmPositionDetail.Job           = HcmJob::findByJob(job).RecId;
        HcmPositionDetail.Position      = HcmPosition.RecId;

        HcmPositionDetail.Title         = HcmTitle::findByTitle(HcmJobDetail::findByJob(HcmPositionDetail.Job).Description).RecId;
        HcmPositionDetail.Description   = cells.item(row, 4).value().bStr();
        HcmPositionDetail.ValidFrom     = DateTimeUtil::newDateTime(cells.item(row, 5).value().date(),timeNow());
        HcmPositionDetail.ValidTo       = DateTimeUtil::maxValue();
        HcmPositionDetail.insert();
        HcmPositionWorkerAssignment.ValidFrom   = DateTimeUtil::newDateTime(cells.item(row, 5).value().date(),timeNow());
        HcmPositionWorkerAssignment.ValidTo     = DateTimeUtil::maxValue();
        HcmPositionWorkerAssignment.Position    = HcmPosition.RecId;
        HcmPositionWorkerAssignment.Worker      = HcmWorker::findByPersonnelNumber(cells.item(row, 6).value().bStr()).RecId;
        HcmPositionWorkerAssignment.insert();
        HcmPositionDuration.Position    = HcmPosition.RecId;
        HcmPositionDuration.ValidFrom   = HcmPositionWorkerAssignment.ValidFrom;
        HcmPositionDuration.ValidTo     = DateTimeUtil::maxValue();
        HcmPositionDuration.insert();

        type = cells.item(row+1, 1).value().variantType();
    print row;
    }
    }
while (type != COMVariantType::VT_EMPTY);
application.quit();
ttsCommit;
}

Dynamics AX 2012 Code to meagre MainAccount Dimesion and Default Dimesion

Below is the to Code to meagre  MainAccount Dimesion and Default Dimesion   to get Ledger Dimension Account which is used in segmented while creating Journal voucher in LedgerJournalTrans

static void CreateLedgerDimension(Args _args)
{
    LedgerDimensionAccount  ledgerDimension,defaultDimension;
    LedgerDimensionAccount  empledgerdimension;
    LedgerDimensionAccount  mainAccDimension;
    HcmPositionDefaultDimension (empPositionDimension
    HcmPositionWorkerAssignment HcmPositionWorkerAssignment;
   
mainAccDimension = PayHeadTable::find('BasicRate').DebitAccount;

HcmPositionWorkerAssignment =   HcmPositionWorkerAssignment::getActivePositionWorkerAssignment(HcmWorker::findByPersonnelNumber('PID-0937').RecId);
       
select empposdimension where empposdimension.Position == HcmPositionWorkerAssignment.Position;
       
if(empPositionDimension)
empledgerdimension  =   empPositionDimension.DefaultDimension;

    defaultDimension = DimensionDefaultingService::serviceCreateLedgerDimension(mainAccDimension, empledgerdimension);


     info(strFmt("Before: %1", DimensionAttributeValueCombination::find(defaultDimension).DisplayValue));
 

}

Dynamics AX 2012 inserting data in Resource Dimension and fetching Default Dimension

DimensionDefault getDimension(ResourceTable _Resource)
{

DimensionAttribute DimensionAttribute;
DimensionFinancialTag  DimensionFinancialTag;

Struct struct = new Struct();

container defaultDimensionCon;
DimensionDefault workerDimensionDefault;

;
DimensionAttribute =  DimensionAttribute::findByName('Resource');

DimensionFinancialTag = DimensionFinancialTag::findByFinancialTagCategoryAndValue(DimensionAttribute.financialTagCategory(),_Resource.value,true);
        if(!DimensionFinancialTag)
        {
            DimensionFinancialTag.clear();
            DimensionFinancialTag.Description=_Resource.description;
            DimensionFinancialTag.Value=_HcmWorker.PersonnelNumber;
            DimensionFinancialTag.FinancialTagCategory=DimensionAttribute.financialTagCategory();
            DimensionFinancialTag.insert();


struct.add('Resource', _HcmWorker.PersonnelNumber);

defaultDimensionCon += struct.fields();

defaultDimensionCon += struct.fieldName(1);

defaultDimensionCon += struct.valueIndex(1);

workerDimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(defaultDimensionCon);

return workerDimensionDefault;
    }
    else
        return 0;
}