Friday, August 22, 2014

To loop though all the form parts in a form in AX 2012

loop though all the form parts in a form in AX 2012

we can call below method from form data source active method:

public void updateLineInfoParts()
{
    PartList    partList;
    int            partListCount;
    FormRun     factBox;
    Object      factBoxObject;

    partList = new PartList(element);

    for (partListCount = 1; partListCount <= partList.partCount(); partListCount++)
    {
        factBox = partList.getPartById(partListCount);
        switch(factBox.name())
        {
            case  formStr("FormPart 1")
                 ,formStr("FormPart 2")
                 ,formStr("FormPart 3"):
                factBoxObject = factBox;
                factBoxObject.methodonFactbox();
                break;
        }
    }
}

Sunday, April 6, 2014

Loop through infolog in AX 2012 through code

In 2 methods we can loop through infolog messages and read the data about them

1)

The infolog class has a "copy" and a "cut" method that return the contents of the infolog, in container form. The elements in the container are containers themselves, each one corresponding to a line in the infolog. So you get the container, loop over it, and for each iteration you can grab the sub-container and extract the details like the exception type, action class, helptext, etc.
 
static void InfologParse(Args _args)
{
    container   infologCon, infoline;
    Exception   exception;
    int         i;
    str         message;
    str         helpURL;
    ClassName   actionClassName;
    container   actionClassOptions;
    ;

    // Put test data in infolog
    setPrefix("First Prefix");
    error("test error");
    warning("test warning");
    
    setPrefix("One more level");
    info("infolog!");
    
    // Get the infolog data and clear the messages (cut)
    infologCon = infolog.cut();

    for(i=1; i<=conLen(infologCon); i++)
    {
        infoline = conPeek(infologCon, i);

        exception = conPeek(infoline, 1);
        message = conPeek(infoline, 2);
        helpURL = conLen(infoline) > 2 ? conPeek(infoline, 3) : '';
        if(conLen(infoline) > 3 && conPeek(infoline, 4))
        {
            actionClassName    = classId2Name(conPeek(infoline, 4));
            actionClassOptions = conPeek(infoline, 5);
        }

        info(strFmt("Type: %1; Prefix: %2; Message: %3",
            exception,
            // replace the \t by / so we can see all prefixes
            strReplace(message, '\t', '/'),
            // copy the message from the last occurance of the \t to the end
            subStr(message,
                strScan(message, '\t',
                strLen(message),
                -strLen(message)) + 1,
                strLen(message))));
    }
}
 
2)
 
SysInfologEnumerator enumerator;
SysInfologMessageStruct msgStruct;
Exception exception;

enumerator = SysInfologEnumerator::newData(infolog.cut());
while (enumerator.moveNext())
{
    msgStruct = new SysInfologMessageStruct(enumerator.currentMessage());
    exception = enumerator.currentException();
    info(strFmt("Type: %1; Prefix: %2; Message: %3",
        exception,
        msgStruct.preFixTextElement(1),
        msgStruct.message()));
} 
 
infolog.num([exception _exception]) --> this method returns number of infolog lines 
of the exception type. If no exception type is passed it number the total number of 
infolog lines

Thursday, February 6, 2014

Different ways of applying query ranges on static queries in AX 2012


watch the below screens how AND and OR changes to different query ranges applied on static queries




Tuesday, January 28, 2014

Find any item's On-hand quantity in a warehouse on a particular date through code in AX 2012



    InventDimParm           inventDimParm;
    InventDim                   inventDim;

    TransDate                   CountOnDate = today();

    inventDim.InventLocationId = // assign the counting warehouse
    inventDimParm.initFromInventDim(inventDim);

    return InventSumDatePhysicalDim::onHandQty(CountOnDate, "ItemId", inventDim, inventDimParm);

That's all!

Create a Retail store through code in AX 2012



    OMOperatingUnit     omOperatingUnit;
    DirOrganization     dirOrganization;
    DirParty            dirParty;
    Name                storeName =  'AjayStore';
    Name                storeNameAlias =  'AjayStore';
    LanguageId          languageId = 'EN-US';

    dirOrganization.clear();
    dirOrganization.Name                  = storeName;
    dirOrganization.NameAlias             = storeNameAlias;
    dirOrganization.LanguageId            = languageId;
    dirOrganization.insert();

    dirParty = new DirParty(dirOrganization);

    omOperatingUnit.initFromDirParty(dirParty);
    omOperatingUnit.Name                  = storeName;
    omOperatingUnit.OMOperatingUnitNumber =         NumberSeq::newGetNum(OMOperatingUnit::getNumberSequenceReference()).num();
    omOperatingUnit.OMOperatingUnitType   = OMOperatingUnitType::RetailChannel;
    omOperatingUnit.LanguageId            = languageId;
    omOperatingUnit.insert();

    retailStoreTable.initValue();
    retailStoreTable.OMOperatingUnitID    = omOperatingUnit.RecId;
    retailStoreTable.DefaultCustAccount   = // Default account num;
    retailStoreTable.inventLocation       = // Default invent location;
    retailStoreTable.StoreNumber          = NumberSeq::newGetNum(RetailParameters::numRefStoreId()).num();
    retailStoreTable.Currency             = //currency;
    retailStoreTable.taxGroup             = // sales tax group;

    if (retailStoreTable.validateWrite())
    {
        retailStoreTable.insert();
    }

That's all!

Find a particular Item's product attribute by attibute name and find it's value


Take an item "RawCake". Suppose you have defined in it's product attribute it's an 'eatable' item with its value check marked yes; Now you wanted to know the value of this Product's item attribute value for 'eatable'. Here is that small piece of code that helps in finding it's value;

    InventTable                    InventTableLoc;
    EcoResProductAttributeValue ecoResProductAttributeValue;
    EcoResAttribute            ecoResAttribute;
    EcoResValue                 ecoResValue;

    #define.EatableItem("eatable")

    select RecId from InventTableLoc where InventTableLoc.itemid =='RawCake'
        join RecId from ecoResProductAttributeValue
        where ecoResProductAttributeValue.Product == InventTableLoc.Product
            join RecId, Name from ecoResAttribute
            where ecoResProductAttributeValue.Attribute == ecoResAttribute.RecId
                && ecoResAttribute.Name like #EatableItem
                join ecoResValue
                where ecoResValue.RecId == ecoResProductAttributeValue.Value;

    if (ecoResAttribute.RecId != 0)
    {
        return ecoResValue.value();
    }

Find Sales price from Trade agreement on a Sales Quotation through code

PriceDisc_Price       priceDisc_Price;
SalesPrice                salesPriceAgreement, salesPriceItem;
SalesQuotationLine  salesQuotationLine;  

priceDisc_Price = SalesPurchLine::priceDisc_PriceCache(salesQuotationLine);
salesPriceItem = InventTableModule::find(salesQuotationLine.ItemId, ModuleInventPurchSales::Sales).Price;
salesPriceAgreement = priceDisc_Price.price();

// Check if a trade agreement exists
 if (salesQuotationLine.SalesPurchLine::priceAgreementExists(salesQuotationLine.inventDim()))
 {
         // Your code   
         salesQuotationLine.SalesPrice = salesPriceAgreement;
  }
  else
        salesQuotationLine.SalesPrice = salesPriceItem ;

That's all!