Wednesday 9 May 2018

update default dimensions in ax 2012

update default dimensions in ax 2012


class ProjDimUpload
{
    

}

..............................................................

public void importProjDim()

{

    RecId               recIdDefaultDimension,Dimension3,itemid;

    //table

    ForecastSales               forecastSales,forecastSalesLoc;

    CustTable                   custTable;

    DefaultDimensionView       defaultDimensionView;

    InventTable               inventTable;

 
    container   financialDimensionFromExcel;

    container   conDimensionName;

    ;

    while select forecastSales

    {

        select forupdate forecastSalesLoc where forecastSalesLoc.CustAccountId == forecastSales.CustAccountId

            && forecastSalesLoc.ItemId == forecastSales.ItemId

            && forecastSalesLoc.RecId == forecastSales.RecId;


        conDimensionName = this.TIDgetDimensionName();

        financialDimensionFromExcel += "";

        financialDimensionFromExcel += "";

        financialDimensionFromExcel += forecastSalesLoc.CustAccountId;

        financialDimensionFromExcel += "";

        financialDimensionFromExcel += "";

        financialDimensionFromExcel += forecastSalesLoc.ItemId;

        financialDimensionFromExcel += "";

        financialDimensionFromExcel += "";


        custTable = CustTable::find(forecastSalesLoc.CustAccountId);

        
        recIdDefaultDimension       = this.TIDcreateDefaultDimension(conDimensionName,financialDimensionFromExcel);


      Dimension3 = DimensionDefaultingService::serviceMergeDefaultDimensions(recIdDefaultDimension, custTable.DefaultDimension);



        Dimension3 = DimensionDefaultingService::serviceMergeDefaultDimensions(itemid, custTable.DefaultDimension);



        forecastSalesLoc.DefaultDimension =Dimension3;



        //forecastSalesLoc.DefaultDimension = recIdDefaultDimension;

        forecastSalesLoc.update();

    }

}
.............................................................................

Public DimensionDefault TIDcreateDefaultDimension(container _attr, container _value, boolean _createIfNotFound = true)
{
    DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();
    DimensionDefault                    result;
    int                                 i;
    DimensionAttribute                  dimensionAttribute;
    DimensionAttributeValue             dimensionAttributeValue;
    //_attr is dimension name in table DimensionAttribute
    container               conAttr =   _attr;
    container               conValue = _value;
    str                     dimValue;

    for (i = 1; i <= conLen(conAttr); i++)
    {
        dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,i));

        if (dimensionAttribute.RecId == 0)
        {
            continue;
        }

        dimValue = conPeek(conValue,i);

        if (dimValue != "")
        {
            // _createIfNotFound is "true". A dimensionAttributeValue record will be created if not found.
            dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,_createIfNotFound);

            // Add the dimensionAttibuteValue to the default dimension
            valueSetStorage.addItem(dimensionAttributeValue);
        }
    }
    result = valueSetStorage.save();
    return result;
}


..............................

public  container TIDgetDimensionName()
{
    DimensionAttribute              dimAttr;
    DimensionAttributeSetItem       dimAttrSetItem;
    DimensionEnumeration            dimensionSetId;
    DimensionAttributeValue         dimAttributeValue;
    container                       DimensionName;

    dimensionSetId      = DimensionCache::getDimensionAttributeSetForLedger();

    while select dimAttr order by Name
                where dimAttr.Type != DimensionAttributeType::MainAccount
            join RecId from dimAttrSetItem
                where dimAttrSetItem.DimensionAttribute     == dimAttr.RecId &&
                      dimAttrSetItem.DimensionAttributeSet  == dimensionSetId
            {
                dimensionName += dimAttr.Name;

            }

    return DimensionName;
}



/////////////////////////////////////////

public static void main(Args args)
{
   ProjDimUpload projDimUpload = new ProjDimUpload();
    projDimUpload.importProjDim();
}

Wednesday 2 May 2018

create and post invoice journal ax 2012

create and post invoice journal ax 2012

public void createpostInvoiceJournal(ProjInvoiceJour    projInvoiceJour)
{
    LedgerJournalCheckPost  jourCheckPost;
    LedgerJournalTable      jourTable;

    AxLedgerJournalTable    header  = new AxLedgerJournalTable();
    AxLedgerJournalTrans    trans   = new AxLedgerJournalTrans();

    container               offsetDim;

    GeneralJournalAccountEntry  generalJournalAccountEntry;
    GeneralJournalEntry         generalJournalEntry;
    ProjParameters              projParameters;
    ProjJournalTrans            projJournalTrans;
    DimensionAttributeValueSetStorage   dimStorage;
    Str                                 worker;
    Counter                             i;

    // The AP invoice journal name, could vary.
    LedgerJournalNameId     ledgerJournalNameId;

    // This is the entry against the Vendor account.
    // It will have to pre-exist for this script to work.
    // It is probably created the first time such an entry is
    // made on a journal line.
    DimensionAttributeValueCombination  davc;

    LedgerJournalACType         accType, offsetAccType;
    BankAccountTable            bankAccountTable;

    accType         = LedgerJournalACType::Vend;
    offsetAccType   = LedgerJournalACType::Ledger;

    select projParameters;
    ledgerJournalNameId = projParameters.JournalName;
    header.parmJournalName(ledgerJournalNameId);
    header.parmJournalType(LedgerJournalType::VendInvoiceRegister);
    header.save();

    select generalJournalEntry
        join generalJournalAccountEntry where generalJournalEntry.JournalNumber == GeneralJournalEntry::findBySubledgerVoucher(projInvoiceJour.LedgerVoucher,curext()).JournalNumber
            && generalJournalAccountEntry.GeneralJournalEntry == generalJournalEntry.RecId
            && generalJournalAccountEntry.PostingType == LedgerPostingType::ProjPayrollAllocation;

    trans.parmAccountType(accType);
    trans.parmJournalNum(header.ledgerJournalTable().JournalNum);
    trans.parmInvoice(projInvoiceJour.ProjInvoiceId);

    //First is Display value, followed by Main Account and then dimensions.

    trans.parmAccountType(LedgerJournalACType::Vend);

    // Note: This only works if the DimensionValueAttributeCombindation record
    // exists from before.
    select projJournalTrans where projJournalTrans.ProjId == projInvoiceJour.ProjInvoiceProjId;
    select firstonly RecId from davc
        where davc.DisplayValue == projJournalTrans.ALE_AccountNum;// "202-000300"; //Vendor123 is the vendor account number.

    trans.parmLedgerDimension(davc.RecId);

    //There's a parm method for credit as well. generalJournalAccountEntry.
    trans.parmAmountCurDebit(generalJournalAccountEntry.AccountingCurrencyAmount);
    trans.parmOffsetAccountType(offsetAccType);
//offsetDim   = ["52121-Disp", "52121", 2, "Site", "12345", "Department", "204"];  
//52121-12345-204,"52121",2,"site",12345,'dep',204
//main account and other dim followed by - and no of dim 
    //trans.parmOffsetLedgerDimension(AxdDimensionUtil::getLedgerAccountId(offsetDim));
    trans.clearField(fieldNum(ledgerJournalTrans, offsetLedgerDimension), false);
    trans.parmOffsetLedgerDimension(generalJournalAccountEntry.LedgerDimension);

    dimStorage = DimensionAttributeValueSetStorage::find(generalJournalAccountEntry.LedgerDimension);

    for (i= 1 ; i<= dimStorage.elements() ; i++)
    {
        if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "worker")
        {
            worker = dimStorage.getDisplayValueByIndex(i);
        }
    }
    trans.parmApprover(str2int(worker));
    trans.parmApproved(NoYes::Yes);

    trans.save();

    jourTable =  header.ledgerJournalTable();

    if (jourTable.RecId > 0)
    {
        jourCheckPost = ledgerJournalCheckPost::newLedgerJournalTable(jourTable,
                                                                NoYes::Yes,
                                                                NoYes::Yes);
        // Post only if there is succesful validation.
        //if (jourCheckPost.validate())
        {
            jourCheckPost.run();
        }
    }
}