Thursday, March 17, 2016

Customer electronic and postal address

Below code will fetch the customer's electronic and postal addresses.



    LogisticsElectronicAddress      electronicAddress;
    CustTable                               custTable;
    ;
    
    custTable   = CustTable::find('CUST-000011', false);
    
    electronicAddress   = DirParty::primaryElectronicAddress(custTable.Party, LogisticsElectronicAddressMethodType::Phone);

    print electronicAddress.Locator;
    print custTable.postalAddress().Address;
    pause;

Code for unit conversion of quantity

Hi guys,

Below is the sample code to convert quantity from one unit to another.


    RecId      fromUnit, toUnit;
    Qty         fromQty, toQty;
    ;
    fromQty   = 100;    
    fromUnit   = UnitOfMeasure::findBySymbol('KG').RecId;
    toUnit       = UnitOfMeasure::findBySymbol('g').RecId;    
    toQty       = UnitOfMeasureConverter::convert(fromQty, fromUnit, toUnit, false);
    
    info(strFmt("%1",toQty));

Totals for Purchase order, Sales order and Sales quotation.

There are some forms in Ax 2012 which displays the totals for the selected records such as purchase order, sales order, sales quotation, etc. These totals will be calculated based on the line details of those orders. The following piece of code can be used to get the totals for the orders at the header level.


Totals for purchase order:
    
    PurchtotalsForm     purchTotals;
    PurchTable             purchTable;    
    real                         a, b, c, d;
    ;
    purchTable      = purchTable::find('PO-000000132', false);
    purchTotals     = PurchtotalsForm::newPurchTotalsForm(purchTable, PurchUpdate::All);
    purchTotals.calctotals();
    purchTotals.doPack();
    
    a = purchTotals.invoiceAmountValue();
    b = purchTotals.invoiceRoundOffValue();
    c = purchTotals.sumMarkUpValue();
    d = purchTotals.qtyValue();
    
    info(strFmt("%1, %2, %3, %4", a, b, c, d));


Totals for sales order:

    container                       salesTotals;
    SalesTable                     salesTable;
    Amount                         a, b, c, d;
    ;
    salesTable          = SalesTable::find('SO-000000114', false);
    salesTotals         = SalesTotals::displayFieldsServer(salesTable, SalesUpdate::All);

    a = conPeek(salesTotals, TradeTotals::posEndDisc());
    b = conPeek(salesTotals, TradeTotals::posMarkup());
    c = conPeek(salesTotals, TradeTotals::posRoundOff());
    d = conPeek(salesTotals, TradeTotals::posTotalAmount()); 

    info(strFmt("%1, %2, %3, %4", a, b, c, d));


Totals for sales quotation:

    container                       displayFields;
    SalesQuotationTotals     quotationTotals;
    SalesQuotationTable      quotationTable;
    Amount                         a, b, c, d;
    ;
    quotationTable      = SalesQuotationTable::find('SQ-000031', false);
    quotationTotals     = SalesQuotationTotals::construct(quotationTable, SalesUpdate::All);
    displayFields        = quotationTotals.displayFieldsCurrency('SAR');

    a = conPeek(displayFields, TradeTotals::posBalance());
    b = conPeek(displayFields, TradeTotals::posMarkup());
    c = conPeek(displayFields, TradeTotals::posRoundOff());
    d = conPeek(displayFields, TradeTotals::posTotalAmount());

    info(strFmt("%1, %2, %3, %4", a, b, c, d));


Code to convert amount as per currency exchange rate

Below code can be used to apply exchange rate for the amount. From currency and To currency must be mentioned along the amount to be converted.



CurrencyExchangeHelper     currencyExchangeHelper;
AmountCur                           amountValue = 100;
AmountMst                         amountMST;
CurrencyCode                       fromCurrency = 'SAR', toCurrency = 'USD';
 
currencyExchangeHelper = CurrencyExchangeHelper::newExchangeDate(Ledger::current(), systemDateGet());
amountMST                    = currencyExchangeHelper.calculateCurrencyToCurrency(fromCurrency, toCurrency, amountValue, false);  

info(strfmt("%1", amountMST));

Code to get Main account number and description from LedgerDimension

Hi guys,


Below code can be used to fetch the Main account number and its description from LedgerDimension field. 


DimensionAttributeValueCombination     dimAttrValueComb;
DimensionStorage                                 dimensionStorage;
DimensionDynamicAccount                  _ledgerDimension;       
DimensionStorageSegment                    segment;
str                                                         segmentName, segmentDescription;
SysDim                                                 segmentValue;

 ;

_ledgerDimension      = 5637146328;

dimAttrValueComb      = DimensionAttributeValueCombination::find(_ledgerDimension);

dimensionStorage       = DimensionStorage::findById(_ledgerDimension);
segment                     = dimensionStorage.getSegmentForHierarchy(1, 1);
if (segment.parmDimensionAttributeValueId() != 0)
{
        segmentName         = DimensionAttribute::find(DimensionAttributeValue::find(segment.parmDimensionAttributeValueId()).DimensionAttribute).Name;
        if (segmentName == 'MainAccount')
        {
            segmentValue        = segment.parmDisplayValue();
            segmentDescription  = segment.getName();
        }        
}
    info( strFmt("%1,   %2", segmentValue, segmentDescription));