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!