Wednesday, June 14, 2023

Custom class to handle financial Dimensions using x++

 Custom class to handle financial Dimensions using x++ 


/// <summary>
/// Custom class to handle dimensions
/// </summary>
class DimensionHelper
{
    /// To Get the default dimension value by using default dimension id
    public static DimensionValue getDefaultDimensionValue(str dimName, 
                                                        DimensionDefault defaultDimension)
    {
        DimensionAttributeValueSetStorage   dimStorage;
        Counter                             i;
        str                                 returnValue;

        dimStorage = DimensionAttributeValueSetStorage::find(_defaultDimension);

        for (i=1 ; i<= dimStorage.elements() ; i++)
        {
            if (DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == _dimName)
            {
                returnValue = dimStorage.getDisplayValueByIndex(i);
                break;
            }
        }
        return returnValue;
    }

    /// Static method to get default dimension
    public static DimensionDefault getDefaultDim(container conAttr, container conValue)
    {
        DimensionAttributeValueSetStorage   valueSetStorage = new DimensionAttributeValueSetStorage();

        int                     i;
        DimensionAttribute      dimensionAttribute;
        DimensionAttributeValue dimensionAttributeValue;
        str                     dimValue;

        if((conLen(conAttr) <= 0) || (conLen(conValue) <= 0))
        {
            throw error("@SYS313886");
        }

        for (i = 1; i <= conLen(conAttr); i++)
        {
            dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,i));
            
            if (dimensionAttribute.RecId == 0)
            {
                continue;
            }
            
            dimValue = conPeek(conValue,i);
            
            if (dimValue != "")
            {
                dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(
                                                                    dimensionAttribute, dimValue, false, true);
                valueSetStorage.addItem(dimensionAttributeValue);
            }
        }

        return valueSetStorage.save();
    }

    /// Static method to get default dimension
    public static DimensionDynamicAccount createLedgerDimFromMainAccount(container conAttr, container conValue, 
                                                                            Name mainAccount, LedgerJournalACType accountType) 
    {
        DimensionDefault        dimensionDefault;
        DimensionDynamicAccount dimensionDynamicAccount;
        LedgerDimensionBase     ledgerDimBase;
        LedgerDimensionAccount  ledgerDim;

        dimensionDefault        = DimensionHelper::getDefaultDim(conAttr, conValue);
        dimensionDynamicAccount = LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber(mainAccount, accountType);
        ledgerDim               = LedgerDimensionFacade::serviceCreateLedgerDimension(dimensionDynamicAccount);

        ledgerDimBase = LedgerDimensionFacade::serviceCreateLedgerDimForDefaultDim(dimensionDefault, ledgerDim);

        return ledgerDimBase;
    }

    /// Generate ledger dimension 
    public DimensionDynamicAccount generateLedgerDimension(container    conData, MainAccountNum  mainAccount)
    {
        int                                 hierarchyCount;
        int                                 hierarchyIdx;
        LedgerRecId                         ledgerRecId;
        MainAccount                         mainAccount;
        RefRecId                            recordvalue;
        DimensionAttribute                  dimensionAttribute;
        DimensionAttributeValue             dimensionAttributeValue;
        DimensionSetSegmentName             DimensionSet;
        DimensionStorage                    dimStorage;
        LedgerAccountContract               LedgerAccountContract = new LedgerAccountContract();
        dimensionAttributeValueCombination  dimensionAttributeValueCombination;
        DimensionAttributeValueContract     ValueContract;
        List                                valueContracts;
        
        valueContracts          = new List(Types::Class);
        mainAccount             = MainAccount::findByMainAccountId(_mainAccount);
        recordvalue             = DimensionHierarchy::getAccountStructure(mainAccount.RecId,Ledger::current());
        hierarchyCount          = DimensionHierarchy::getLevelCount(recordvalue);
        DimensionSet            = DimensionHierarchyLevel::getDimensionHierarchyLevelNames(recordvalue);

        for(hierarchyIdx = 1; hierarchyIdx <= hierarchyCount; hierarchyIdx++)
        {
            if (hierarchyIdx == 1)
            {
                continue;
            }

            dimensionAttribute = DimensionAttribute::findByLocalizedName(DimensionSet[hierarchyIdx], 
                                                                            false, SystemParameters::getSystemLanguageId());
            if (dimensionAttribute)
            {
                dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,
                                                                                            conPeek(_conData,hierarchyIdx));
                if (dimensionAttributeValue)
                {
                    ValueContract = new DimensionAttributeValueContract();
                    ValueContract.parmName(dimensionAttribute.Name) ;
                    ValueContract.parmValue(dimensionAttributeValue.CachedDisplayValue);
                    valueContracts.addEnd(ValueContract);
                }
            }
        }

        LedgerAccountContract.parmMainAccount(_mainAccount);
        LedgerAccountContract.parmValues(valueContracts);

        dimStorage                          = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);
        dimensionAttributeValueCombination  = DimensionAttributeValueCombination::find(dimStorage.save());
        ledgerRecId                         = dimensionAttributeValueCombination.RecId;

        return ledgerRecId;
    }
}


Keep Daxing!!

Add additional total discounts to the sales total form using x++

Add additional total discounts to the sales total form using x++.

/// <summary>
/// extension class to apply total discounts on sales total forms
/// </summary>
[ExtensionOf(classstr(TradeTotals))]
public final class TradeTotals_Extension
{
    /// <summary>
    /// adds additional total discounts to the sales total form
    /// </summary>
    /// <returns>total discount</returns>
    public DiscAmount totalEndDisc()
    {
        endDisc = next totalEndDisc();

        if (orderTable.tableID == tableNum(SalesTable))
        {
            SalesTable salestableloc = orderTable;

            if (salestableloc.TotalDiscountAmount && salestableloc.SalesStatus != SalesStatus::Invoiced && balance)
            {
                return endDisc + ((balance / 100) * salestableloc.TotalDiscPercentage);

               // return endDisc + salestableloc.TotalDiscountAmount;
            }
        }
        return endDisc;
    }

    //public TaxAmountCur  totalTaxAmount()
    //{
    //    TaxAmountCur tax;
    //    tax = next totalTaxAmount();
	
    //    if (orderTable.tableID == tableNum(SalesTable))
    //    {
    //        SalesTable salestableloc = orderTable;
    //        if (salestableloc.TotalDiscountAmount && salestableloc.SalesStatus != SalesStatus::Invoiced)
    //        {
    //            return tax - salestableloc.TotalDiscountAmount ;
    //        }
    //    }
    //     return tax;
    //}

    public TaxAmountCur getInclTaxAmount_IN(Common _buffer)
    {
        TaxAmountCur  taxAmount;

        taxAmount = next getInclTaxAmount_IN(_buffer);

        if (orderTable.tableID == tableNum(SalesTable))
        {
            SalesTable salestableloc = orderTable;

            if (salestableloc.TotalDiscountAmount && salestableloc.SalesStatus != SalesStatus::Invoiced && balance)
            {
                return taxAmount - ((balance / 100) * salestableloc.TotalDiscPercentage);
            }
        }
        return taxAmount;
    }
}


/// <summary>
/// Retrieves the line amount excluding tax and adding total discount to line.
/// </summary>
[ExtensionOf(classStr(TaxSales))]
final class TaxSales_Extension
{
    /// <summary>
    /// Retrieves the line amount excluding tax (base amount) from the <c>SalesFormLetter</c> instance.
    /// </summary>
    /// <param name = "_lineAmount">Line amount.</param>
    /// <returns>The line amount excluding tax.</returns>
    [Replaceable]
    protected TaxBaseCur retrieveBaseAmount(AmountCur _lineAmount)
    {
        AmountCur       lineAmount;
        SalesTable      salesTableLoc;

        lineAmount = next retrieveBaseAmount(_lineAmount);

        salesTableLoc = salesLine.Salestable();

        if (salesTableLoc.TotalDiscountAmount != 0 && salesTableLoc.SalesStatus != SalesStatus::Invoiced)
        {
            lineAmount -= ((lineAmount / 100) * salesTableLoc.TotalDiscPercentage);
        }

        return lineAmount;
    }
}

Keep Daxing!!


Adding validation for Invoice date in Pending vendor Invoice using x++ in D365

Adding validation for Invoice date in Pending vendor Invoice using x++ in D365.


[ExtensionOf(formStr(VendEditInvoice))]
final class VendEditInvoice_Extension
{
    void postInvoice()
    {
        VendInvoiceInfoSubLine  vendInvoiceInfoSubLine;
        VendInvoiceInfoLine     vendInvoiceInfoLine;
        VendPackingSlipJour     vendPackingSlipJour;
        VendInvoiceInfoTable    vendInvoiceInfoTable = this.VendInvoiceInfoTable;

        select firstonly DeliveryDate from vendPackingSlipJour order by vendPackingSlipJour.DeliveryDate desc
            join vendInvoiceInfoSubLine
                where vendInvoiceInfoSubLine.DocumentId == vendPackingSlipJour.PackingSlipId
	    join vendInvoiceInfoLine
		where vendInvoiceInfoSubLine.LineRefRecId == vendInvoiceInfoLine.RecId &&
		    vendInvoiceInfoLine.ParmId == vendInvoiceInfoTable.ParmId &&
		    vendInvoiceInfoLine.TableRefId == vendInvoiceInfoTable.TableRefId;

        if (vendPackingSlipJour.DeliveryDate > vendInvoiceInfoTable.DocumentDate)
        {
            throw Global::error('Invoice date should be greater than Receipt date');
        }
		
        next postInvoice();
    }

}


Keep daxing!!

Open journal lines form from the custom form using x++.

 Open journal lines form from the custom form using x++.


  • We need to add ledgerJournalTable in the form data source and don't give any properties.
  • Copy the JournalLines_Review button from the LedgerJournalTable form.

Write the below code in the Form.

public class Form1 extends FormRun
{
    LedgerJournalFormTable  journalFormTable;

    public void init()
    {
        journalFormTable = LedgerJournalFormTable::construct(element);

        journalFormTable.datasourceInitPost(LedgerJournalTable);

        super();
    }

    JournalForm journalForm()
    {
        return journalFormTable;
    }
}


Button clicked method. (copied from LedgerJournalTable)
    [Control("MenuFunctionButton")]
    class JournalLines_Review
    {
        /// <summary>
        /// Handles the Lines->Review button's clicked event.
        /// </summary>
        public void clicked()
        {
            journalFormTable.assertJournalNotInUse();
    
            journalFormTable.parmIsProcessingPaymentOnApprovedJounal(false);
            journalFormTable.parmIsWFApprovedModeSelected(true);
    
            super();
    
            ledgerJournalTable_ds.reread();
        }

    }

The below code I have written in jumpRef method is based on my requirement.

    [Control("String")]
    class Table1_FieldString1
    {
        public void jumpRef()
        {
            super();

            LedgerJournalTable  ledgerJournalTableNew = LedgerJournalTable::find(Table1.FieldString1);


            //ledgerJournalTable_ds.setRecord(ledgerJournalTableNew);

            ledgerJournalTable_ds.findRecord(ledgerJournalTableNew);

            //ledgerJournalTable_ds.reread();
            //ledgerJournalTable_ds.refresh();

            journalFormTable.datasourceInitPost(ledgerJournalTable_ds.cursor());

            JournalLines_Review.clicked();
        }
    }


Another Way:

Keep daxing!!





Thursday, June 8, 2023

Display path of navigation for a form.

 


Enable the legacy navigation bar in system administrator > setup > client performance options.





Keep Daxing!!