update or create finical dimension on purchline

 [ExtensionOf(tableStr(PurchLine))]

final class BI_PurchLine_Extension

{

    public void insert(

        boolean     dropInvent                     ,

        boolean     findMarkup                     ,

        boolean     updatePurchTableDistributions  ,

        boolean     _skipPurchTableUpdate          ,

        boolean     _skipInterCompanyCalcDisc      )

    {

        SalesLine salesLine;

        DimensionAttributeValueSetStorage dimStorage,dimStorage_Purch;

        DimensionAttributeValueSetStorage dimStorage_Update;

        DimensionAttribute dimAttr;

        DimensionValue categoryValue;

        PurchLine purchLine_Updated;

        DimensionAttributeValue dimValue;

        DimensionDefault newDefaultDimension;




        next insert(dropInvent                     ,

                    findMarkup                     ,

                    updatePurchTableDistributions  ,

                    _skipPurchTableUpdate          ,

                    _skipInterCompanyCalcDisc      );


        if( this.ItemRefType  == InventRefType::Sales)

        {

            select firstOnly salesLine

                where salesLine.SalesId == this.InventRefId

                && salesLine.ItemId == this.ItemId;


            // Load its DefaultDimension values

           dimStorage = DimensionAttributeValueSetStorage::find(salesLine.DefaultDimension);


            // Find the DimensionAttribute record for "Category"

            dimAttr = DimensionAttribute::findByName('Category');


            // Get the actual value stored in this sales line

            categoryValue = dimStorage.getDisplayValueByDimensionAttribute(dimAttr.RecId);


            //if(this.DefaultDimension == '0')

            //{

            //    dimStorage.addItem();

            //}

            //else

            {

                ttsbegin;

                select forupdate purchLine_Updated

                where this.RecId == purchLine_Updated.recid;


                dimStorage_Purch = DimensionAttributeValueSetStorage::find(purchLine_Updated.DefaultDimension);



                if (!dimAttr.RecId)

                {

                    warning(strFmt("Dimension %1 not found.", 'Category'));

                    return;

                }


                // 4️⃣ Find or create the dimension value record

                dimValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimAttr, categoryValue, true);


                // 5️⃣ Add (or replace) this value in the current combination

                dimStorage_Purch.addItem(dimValue);


                // 6️⃣ Save and get the new dimension default record

                newDefaultDimension = dimStorage_Purch.save();


                // 7️⃣ Assign back to the PurchTable

                purchLine_Updated.DefaultDimension = newDefaultDimension;

                purchLine_Updated.update();


                ttscommit;

            }

        }

    }

}


Comments

Popular posts from this blog

import MT_940 file create new bank statement