Thursday, 11 August 2022

Workflow validation before submit x++ D365 CoC

 [ExtensionOf(classStr(CustFreeInvoiceWorkflow))]

final class XX_CustFreeInvoiceWorkflow_Extension

{

    public static void main(Args _args)

    {

        CustFreeInvoiceWorkflow custFreeInvoiceWorkflow = CustFreeInvoiceWorkflow::construct();

        CustInvoiceTable        custInvoiceTable;

        FormDataSource          custInvoiceTableDataSource;


        if (_args)

        {

            custInvoiceTable = _args.record();

            custInvoiceTableDataSource = FormDataUtil::getFormDataSource(custInvoiceTable);


            if(custInvoiceTable)

            {

                CustInvoiceLine     custInvoiceLine;

                MainAccountNum      mainAccountNum;

                MainAccount         mainAccount;

                

                while select custInvoiceLine where custInvoiceLine.ParentRecId == custInvoiceTable.RecId

                {

                    if(MainAccount::findByMainAccountId(LedgerDimensionFacade::getDisplayValueForLedgerDimension(custInvoiceLine.LedgerDimension)).XX_IsMandatory == true)

                    {

                        boolean         validated, contractCat, customers, projects, division, site;


                        contractCat =   XX_CustInvoiceLineDimension::getValidated(custInvoiceLine.DefaultDimension, "ContractCategory");

                        customers   =   XX_CustInvoiceLineDimension::getValidated(custInvoiceLine.DefaultDimension, "Customers");

                        projects    =   XX_CustInvoiceLineDimension::getValidated(custInvoiceLine.DefaultDimension, "Projects");

                        division    =   XX_CustInvoiceLineDimension::getValidated(custInvoiceLine.DefaultDimension, "Division");

                        site        =   XX_CustInvoiceLineDimension::getValidated(custInvoiceLine.DefaultDimension, "Site");


                        if(contractCat || customers || projects || division || site)

                        {

                            throw error("[ContractCategory, Customers, Projects, Division, Site] dimensions are mandatory in line to submit");

                        }

                    }

                }

            }

        }

        next main(_args);

    }


}

Dimension value by name x++

 public static boolean getValidated(RecId defaultDimension, Name dimName)

    {

        DimensionAttributeValueSetStorage   dimStorage;

        boolean                             hasValue;

        dimStorage  =       DimensionAttributeValueSetStorage::find(defaultDimension);

        hasValue    =       dimStorage.getDisplayValueByDimensionAttribute(DimensionAttribute::findByName(dimName).RecId)? false : True;

        return hasValue;

    }

Thursday, 4 August 2022

Unpack dimension x++

 public str unpackDimension(DimensionDefault _transDimension)

    {

        DimensionAttributeValueSetStorage           dimStorage;

        str                                         segmentName, segmentDescription;

        str                                         segmentValue = "";

        str                                         retProj;//retDept;

        boolean                                     isNullforSales;

        int                                         hierarchyCount, hierarchyIndex;

        DefaultDimensionView                        defaultDimensionView;

        RecId                                       defaultDimension;

        int                                         i;

        container                                   conAttribute;

        container                                   conValues;

        DimensionDefault                            result;

 

        defaultDimension    = _transDimension;

        dimStorage          = DimensionAttributeValueSetStorage::find(defaultDimension);

        for (i= 1 ; i<= dimStorage.elements() ; i++)

        {

            segmentName         = DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name;

            segmentValue        = dimStorage.getDisplayValueByIndex(i);

            if(segmentName)

            {

                select defaultDimensionView

            where defaultDimensionView.DefaultDimension == defaultDimension

            && defaultDimensionView.Name == segmentName;

 

                segmentDescription = defaultDimensionView.dimensionDiscription();

            }

            if(segmentName != "Projects")

            {

                continue;

            }

            else

            {

                retProj =   segmentValue;

            }

        }

        return retProj;

    }

Sunday, 3 April 2022

Unpack ledger dimension x++ D365

 class FCC_TestDim

{

    /// <summary>

    /// Runs the class with the specified arguments.

    /// </summary>

    /// <param name = "_args">The specified arguments.</param>

    public static void main(Args _args)

    {

 

        // DimensionAttributeValueCombination stores the combinations of dimension values

// Any tables that uses dimension combinations for main account and dimensions

// Has a reference to this table’s recid

DimensionAttributeValueCombination dimAttrValueComb;

        //GeneralJournalAccountEntry is one such tables that refrences DimensionAttributeValueCombination

        GeneralJournalAccountEntry gjAccEntry;

        // Class Dimension storage is used to store and manipulate the values of combination

        DimensionStorage dimensionStorage;

        // Class DimensionStorageSegment will get specfic segments based on hierarchies

        DimensionStorageSegment segment;

        int segmentCount, segmentIndex;

        int hierarchyCount, hierarchyIndex;

        str segmentName, segmentDescription,segmentValue;

        LedgerJournalTrans ledgerJournalTrans;

        ;

        select ledgerJournalTrans where ledgerJournalTrans.JournalNum == "00457";

 

        dimAttrValueComb = DimensionAttributeValueCombination::find(ledgerJournalTrans.OffsetLedgerDimension);

 

        // Get dimension storage

        dimensionStorage = DimensionStorage::findById(ledgerJournalTrans.OffsetLedgerDimension);

        if (dimensionStorage == null)

        {

            throw error("@SYS83964");

        }

 

        // Get hierarchy count

        hierarchyCount = dimensionStorage.hierarchyCount();

        //Loop through hierarchies to get individual segments

        for(hierarchyIndex = 1; hierarchyIndex <= hierarchyCount; hierarchyIndex++)

        {

 

            //Get segment count for hierarchy

            segmentCount = dimensionStorage.segmentCountForHierarchy(hierarchyIndex);

 

            //Loop through segments and display required values

            for (segmentIndex = 1; segmentIndex <= segmentCount; segmentIndex++)

            {

                // Get segment

                segment = dimensionStorage.getSegmentForHierarchy(hierarchyIndex, segmentIndex);

 

                // Get the segment information

                if (segment.parmDimensionAttributeValueId() != 0)

                {

                    // Get segment name

                    segmentName = DimensionAttribute::find(DimensionAttributeValue::find(segment.parmDimensionAttributeValueId()).DimensionAttribute).Name;

                    //Get segment value (id of the dimension)

                    segmentValue = segment.parmDisplayValue();

                    segmentDescription = segment.getName();

                    if(segmentName =="BusinessUnit")            //Give the Dimension Name to that Dimension Value

                    {

                        info(strFmt("%1Value -- %2Name",segmentValue,segmentDescription));

                        //return segmentDescription;

                    }

                }

            }

        }

       // return "";

    }


}