Wednesday 11 November 2020

split default dimensions in ax 2012

 

split default dimensions in ax 2012


static void splitDimAndExport(Args _args)

{

    DimensionAttributeValueSetStorage   dimStorage;

    DimensionValue                    bu,costCenter,cust,custbu,custsow,dept,fa,fda,ir,inv,proj,purpose,rda,vend,worker;

    Counter                             i;

    ProjTable                           projTable;

     

    

    // DimensionDefault is a RecId that combines all Dimension Values

    while select projTable where projTable.ProjId == '201P000134-01'

    {

        dimStorage = DimensionAttributeValueSetStorage::find(projTable.DefaultDimension);  

 

   

        for (i= 1 ; i<= dimStorage.elements() ; i++)

        {

            

            // Change the string "CostCenter" to whatever financial dimension you want

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "BusinessUnit")

            {

                bu = '';

                bu = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "CostCenter")

            {

                costCenter = '';

                costCenter = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "Customer")

            {

                cust = '';

                cust = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "Customer_BU")

            {

                custbu = '';

                custbu = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "Customer_SOW")

            {

                custsow = '';

                custsow = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "Department")

            {

                dept = '';

            

                dept = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "FixedAsset")

            {

                fa = '';

            

                fa = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "FixedDepositAccount")

            {

                fda = '';

                fda = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "InterestReceivable")

            {

                ir = '';

            

                ir = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "Investors")

            {

                inv = '';

            

                inv = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "Project")

            {

                proj = '';

            

                proj = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "Purpose")

            {

                purpose = '';

            

                purpose = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "RecurringDepositaccount")

            {

                rda = '';

            

                rda = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "Vendor")

            {

                vend = '';

            

                vend = dimStorage.getDisplayValueByIndex(i);

            }

            if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "Worker")

            {

                worker = '';

                worker = dimStorage.getDisplayValueByIndex(i);

            }

            

        }

        info(strFmt("%1 , %2 , %3 , %4 , %5 , %6 , %7 , %8 , %9 , %10 , %11 , %12, %13, %14 , %15 ",bu,costCenter,cust,custbu,custsow,dept,fa,fda,ir,inv,proj,purpose,rda,vend,worker));

    }


}

Monday 9 November 2020

get User roles using x++


User roles

static void userRoles(Args _args)

{

     UserInfo            userInfo;

    SecurityUserRole    securityUserRole;

    SecurityRole        securityRole;

    str                 st;

       

    ;

        st = ',';

    while

    select userInfo

        join    securityUserRole

        where   securityUserRole.User == userInfo.Id

    {               

          

       

        securityRole = null;

        select securityRole

        where  securityRole.RecId == securityUserRole.SecurityRole;

       

        if (securityRole)

        {

            //info(securityRole.AotName);

            //info(securityRole.Name);   

            info(strFmt("%1 , %2 , %3",userInfo.Id,securityRole.Name,securityRole.Description)); //info(securityRole.Description);                      

        }  

    }


Wednesday 14 October 2020

Customize class in D365 FO

 Customize class in D365 FO

1. create a extension of the standard class

2. use the expresions show 


[ExtensionOf(classStr(BusinessLogic1))] final class BusinessLogic1_Extension { str doSomething(int arg) { // Part 1 var s = next doSomething(arg + 4); // Part 2 return s; } }

Wednesday 10 June 2020

Location Id look up

Location Id look up

public void lookup(FormControl _formControl, str _filterStr)
{
    //LogisticsLocation::locationLookup(_formControl, CustTable::find(ProjFundingSource::findCustAccount(projInvoiceTable.ProjInvoiceProjId).CustAccount).Party);
    //super(_formControl, _filterStr);

    QueryBuildDataSource            queryBuildDataSource,queryBuildDataSource1;
    Query                           query;
    SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(LogisticsLocation), _formControl);
    query = new Query();

    queryBuildDataSource = query.addDataSource(tableNum(LogisticsLocation));

    queryBuildDataSource = queryBuildDataSource.addDataSource(tableNum(DirPartyLocation));
    queryBuildDataSource.joinMode(JoinMode::InnerJoin);
    queryBuildDataSource.relations(true);

    queryBuildDataSource.addRange(fieldNum(DirPartyLocation,Party)).value(queryValue(CustTable::find(ProjFundingSource::findCustAccount(projInvoiceTable.ProjInvoiceProjId).CustAccount).Party));
    //queryBuildDataSource.addRange(fieldNum(DirPartyLocation,IsPostalAddress)).value(SysQuery::value(NoYes::Yes));

    //if (_roleType != LogisticsLocationRoleType::None)
    //{
        queryBuildDataSource = queryBuildDataSource.addDataSource(tableNum(DirPartyLocationRole));
        queryBuildDataSource.joinMode(JoinMode::InnerJoin);
        queryBuildDataSource.relations(true);
        queryBuildDataSource.addRange(fieldNum(DirPartyLocationRole,LocationRole)).value(SysQuery::valueNot(LogisticsLocationRoleType::None));//SysQuery::value(int642str(LogisticsLocationRole::findBytype(_roleType).RecId)));
    //}



    sysTableLookup.addLookupfield(fieldNum(LogisticsLocation, LocationId),true);
    sysTableLookup.addLookupfield(fieldNum(LogisticsLocation, Description));

     sysTableLookup.addLookupMethod(tableMethodStr(LogisticsLocation,postalAddress));

    sysTableLookup.parmQuery(query);

    sysTableLookup.performFormLookup();
}

Wednesday 29 April 2020

Stack trace: Binding operation failed to allocate buffer space AX 2012

Stack trace: Binding operation failed to allocate buffer space



this.query().literals(1);
or

query().literals(1);

Monday 2 March 2020

Update worker Default Dimensions

Update worker Default Dimensions
static void updateWorkerDIm(Args _args)
{
    #File
     Dialog         dialog;
    Filename        filename;
    DialogField     dialogFilename;
    DataAreaId      companyCode;

    HcmWorker       hcmWorker;
    HcmEmployment   hcmEmployment;
    HcmPersonnelNumberId        hcmRecId;
    container           ledgerdimensionheader;
    DimensionDefault    dimensiondefaultheader;
    Struct structHdr    = new Struct();
    boolean             first = true;

    //To filter the files while selecting
    container    conFilter = ["Comma Seperated Value .txt ", "*.csv"];
    IO                  iO;
    FilenameOpen        filenameOpen;
    Container           record;

    dialog = new dialog();
    dialog.caption("Select File to update workers");
    dialogFilename = dialog.addField(extendedTypeStr(FilenameOpen));
    //To filter the files while selecting
    dialog.filenameLookupFilter(conFilter);
    dialog.run();

      if(dialog.closedOk())
    {
        filename = dialogFileName.value();
    }

    iO = new CommaTextIo(filename,#IO_Read);
    if (! iO || iO.status() != IO_Status::Ok)
    {
        throw error("@SYS19358");
    }

     try
    {
        ttsBegin;
        while (iO.status() == IO_Status::Ok)
        {
            record = iO.read();// To read file
            if (record)
            {
                if (first)  //To skip header
                {
                    first = false;
                }
                else
                {                   
                    hcmRecId = conpeek(record, 1);
                    hcmWorker = HcmWorker::findByPersonnelNumber(hcmRecId);
                    while select forUpdate hcmEmployment order by RecId asc where hcmEmployment.Worker == hcmWorker.RecId
                        //&& hcmEmployment.EmploymentType != 1
                    {
                        structHdr.remove('Worker');
                       
                        structHdr.add('Worker', hcmWorker.PersonnelNumber);
                        ledgerDimensionHeader += structHdr.fields();
                        ledgerDimensionHeader += structHdr.fieldName(1);
                        ledgerDimensionHeader += structHdr.valueIndex(1);
                        dimensionDefaultHeader = AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimensionHeader);
                       
                        hcmEmployment.DefaultDimension  = dimensionDefaultHeader;
                        hcmEmployment.validTimeStateUpdateMode(ValidTimeStateUpdate::Correction);
                        hcmEmployment.update();
                        ledgerDimensionHeader = conDel(ledgerDimensionHeader,1,30);
                    }

                }
            }
        }
        info('Imported successfully');
        ttsCommit;

    }
    catch(Exception::Error)
    {
        error('An Exception has occurred');
    }


}

Thursday 20 February 2020

Create and post General Journal

Create and post General Journal

public void run()
{
    Name                        journalName;
    AmountMST                   expenseAssesableValue;

    ProjParameters              projParameters;
    ALE_PayrollRun              payrollRun,payrollRunGroup;
    ALE_PayrollCodeMappingTable payrollCodeMappingTable;
    ALE_ProjAllocationDetails   projAllocationDetails,projAllocationDetailsUpdate;
    DimensionAttributeValueCombination  ledgerDimensionvalueCombination,davc;

    AxLedgerJournalTable        journalTable; // class
    AxLedgerJournalTrans        journalTrans; // class
    LedgerJournalTrans          ledgerJournalTrans;
    LedgerJournalTrans_Project  ledgerJournalTrans_Project;

    LedgerJournalTransTaxExtensionIN    ledgerJournalTransTaxExtensionIN;
    LedgerJournalTrans_ProjectTaxExtensionIN    ledgerJournalTrans_ProjectTaxExtensionIN;
    RefRecId                    recId;
    DimensionDefault            workerDim,mergedDim;
    DimensionDefault            projDim;
    LedgerDimensionAccount      offsetLD;
    DefaultDimensionView        dimView;
    Ledger                      ledger;

    ledgerJournalCheckPost      ledgerJournalCheckPost;// table

    select JournalNameIdRevenue from projParameters;
    ttsBegin;
    try
    {
        while select projAllocationDetails group by ProjId,EmployeeId,MonthYear where projAllocationDetails.Status == '0'
        {
            while select payrollRunGroup group by EmployeeId,MonthYear where payrollRunGroup.EmployeeId == projAllocationDetails.EmployeeId
            {
                //delete_from reveueAllocationFeeTmp;
                journalTable = new AxLedgerJournalTable();
                journalTrans = new AxLedgerJournalTrans();
                //Journal Name
                journalTable.parmJournalName(projParameters.JournalNameIdRevenue);//"Genjur");
                journalName = LedgerJournalName::find(projParameters.JournalNameIdRevenue).Name + '-' + projAllocationDetails.ProjId + ' - ' + strFmt("%1",projAllocationDetails.Projdate);
                journalTable.parmName(journalName);

                journalTable.save();

                select AccountingCurrency from ledger where ledger.Name == curext();
                while select EmployeeId,ProjId,Projdate FROM projAllocationDetailsUpdate
                    join Amount,BatchType,PayrollCode from payrollRun where projAllocationDetailsUpdate.EmployeeId == projAllocationDetails.EmployeeId
                    && projAllocationDetailsUpdate.Status == '0' && projAllocationDetailsUpdate.ProjId == projAllocationDetails.ProjId
                    && payrollRun.EmployeeId == projAllocationDetailsUpdate.EmployeeId
                    && payrollRun.MonthYear == payrollRunGroup.MonthYear
                {
                    ledgerJournalTrans.clear();
                    journalTrans.parmJournalNum(journalTable.ledgerJournalTable().JournalNum);
                    journalTrans.parmTransDate(projAllocationDetailsUpdate.Projdate);
                    journalTrans.parmCurrencyCode(ledger.AccountingCurrency);


                    journalTrans.parmAccountType(LedgerJournalACType::Project);

                    select ledgerDimensionvalueCombination where ledgerDimensionvalueCombination.DisplayValue == projAllocationDetailsUpdate.ProjId;

                    if(ledgerDimensionvalueCombination.recid != 0)
                    {
                        journalTrans.parmLedgerDimension(ledgerDimensionvalueCombination.recid);
                    }
                    else
                    {
                        recId = DimensionStorage::getDynamicAccount( projAllocationDetailsUpdate.ProjId, 3, 129);

                        davc = DimensionAttributeValueCombination::find(recId);

                        journalTrans.parmLedgerDimension(davc.RecId);

                    }
                    //ledgerDim = DimensionDefaultingService::serviceCreateLedgerDimension( projDim);
                    //journalTrans.parmLedgerDimension(ledgerDim);
                    select DefaultDimension from dimView where dimView.DisplayValue == projAllocationDetailsUpdate.EmployeeId;
                    workerDim = dimView.DefaultDimension;
                   
                    projDim = ProjTable::find(projAllocationDetailsUpdate.ProjId).DefaultDimension;
                   
                    mergedDim      = DimensionDefaultingService::serviceMergeDefaultDimensions(projDim,workerDim);
                   
                    journalTrans.parmDefaultDimension(mergedDim);

                    if(ALE_PayrollCodeMappingTable::findByPayrollCode(payrollRun.PayrollCode).ALE_PayrollCodeType == 0 )
                    {
                        journalTrans.parmAmountCurDebit(abs(payrollRun.Amount));
                    }
                    else
                    {
                        journalTrans.parmAmountCurCredit(abs(payrollRun.Amount));
                    }


                    select AccountCode,CategoryId FROM  payrollCodeMappingTable where payrollCodeMappingTable.PayrollCode == payrollRun.PayrollCode;

                    ledgerDimensionvalueCombination.clear();
                    select ledgerDimensionvalueCombination where ledgerDimensionvalueCombination.MainAccount == MainAccount::findByMainAccountId( payrollCodeMappingTable.AccountCode).RecId;
                   
                    offsetLD = DimensionDefaultingService::serviceCreateLedgerDimension( ledgerDimensionvalueCombination.RecId,projDim,workerDim);


                    journalTrans.parmOffsetLedgerDimension(offsetLD);
                    journalTrans.parmOffsetAccountType(LedgerJournalACType::Ledger );

                    journalTrans.save();

                    ledgerJournalTrans = journalTrans.ledgerJournalTrans();
                    ledgerJournalTransTaxExtensionIN.LedgerJournalTrans = ledgerJournalTrans.RecId;
                    ledgerJournalTransTaxExtensionIN.insert();
                    if(ledgerJournalTrans)
                    {

                        ledgerJournalTrans_Project.ProjTransDate = systemDateGet();
                        if(ledgerJournalTrans.AmountCurDebit > 0)
                        {
                            ledgerJournalTrans_Project.CostPrice = ledgerJournalTrans.AmountCurDebit;
                            ledgerJournalTrans_Project.Qty = 1;
                        }
                        else
                        {
                            ledgerJournalTrans_Project.CostPrice = ledgerJournalTrans.AmountCurCredit;
                            ledgerJournalTrans_Project.Qty = -1;
                        }

                        ledgerJournalTrans_Project.SalesCurrencyId = ledgerJournalTrans.CurrencyCode;


                        ledgerJournalTrans_Project.LinePropertyId = "Billable";


                        ledgerJournalTrans_Project.ProjId = projAllocationDetailsUpdate.ProjId;

                        ledgerJournalTrans_Project.RefRecId = ledgerJournalTrans.RecId;
                        ledgerJournalTrans_Project.CategoryId = payrollCodeMappingTable.CategoryId;



                        ledgerJournalTrans_Project.Worker = HcmWorker::findByPersonnelNumber(projAllocationDetailsUpdate.EmployeeId).RecId;

                        ledgerJournalTrans_Project.insert();

                        ledgerJournalTrans_ProjectTaxExtensionIN.LedgerJournalTrans_Project = ledgerJournalTrans_Project.RecId;
                        expenseAssesableValue  = ledgerJournalTrans_Project.Qty * ledgerJournalTrans_Project.SalesPrice;
                        ledgerJournalTrans_ProjectTaxExtensionIN.AssessableValue = expenseAssesableValue;

                        ledgerJournalTrans_ProjectTaxExtensionIN.insert();

                        ledgerJournalTrans_Project.clear();


                    }

                }
                if(ledgerJournalTrans.RecId)
                {
                    ledgerJournalCheckPost = ledgerJournalCheckPost::newLedgerJournalTable(journalTable.ledgerJournalTable(),NoYes::Yes);
                    ledgerJournalCheckPost.run();
                }
                info(strFmt("%1",ledgerJournalTrans.JournalNum));
            }

        }

    }
    catch(Exception::Error)
    {

        error('An Exception has occurred');


    }
    ttsCommit;
}

Monday 6 January 2020

conver amount from one currency to another currency

conver amount from one currency to another currency

 fromCurrecncy = projTable.currencyId();
        toCurrency  = Ledger::findByLegalEntity(CompanyInfo::findDataArea(curext()).RecId).AccountingCurrency;

        if(fromCurrecncy != toCurrency)
        {
            select ExchangeRate,ExchangeRateCurrencyPair from exchangeRate order by RecId desc
                join RecId,ExchangeRateDisplayFactor,ToCurrencyCode,FromCurrencyCode from exchangeRateCurrencyPair
                join RecId,Name from exchangeRateType where exchangeRate.ExchangeRateCurrencyPair == exchangeRateCurrencyPair.RecId
                    && exchangeRateType.RecId == exchangeRateCurrencyPair.ExchangeRateType
                    && exchangeRateType.Name == 'Default'
                    && exchangeRateCurrencyPair.FromCurrencyCode == fromCurrecncy
                    && exchangeRateCurrencyPair.ToCurrencyCode == toCurrency;

            conRevenueAmount = (exchangeRate.ExchangeRate * revenueAmount1)/minOne(exchangeRateCurrencyPair.ExchangeRateDisplayFactor);

            conRevAmount = strFmt("%1",conRevenueAmount);

            curExRate = strFmt("%1",exchangeRate.ExchangeRate);
        }