Tuesday, May 31, 2022

PostAdj Journal in D365FO using x++

Create and post the Adj Journal in D365FO using x++.


private void createPostAdjJournal(ItemId            _itemid,
                                        InventLocationId  _warehouse,
                                        WMSLocationId     _location,
                                        Qty               _qty,
                                        WHSLicensePlateId  _licensePlateId,
                                        InventBatchId     _batch)
    {
        InventJournalTable              inventJournalTable;
        InventJournalTrans              inventJournalTrans;
        InventJournalNameId             inventJournalName;
        InventDim                       inventDim;
        JournalCheckPost                journalCheckPost;
        WHSLicensePlate                 whsLicensePlate;
        InventLocation                  inventLocation;

        ttsbegin;

        whsLicensePlate     = WHSLicensePlate::createLicensePlate(_licensePlateId, true, "");
        inventJournalName   =  InventJournalName::standardJournalName(InventJournalType::LossProfit);

        inventJournalTable.clear();
        inventJournalTable.initFromInventJournalName(InventJournalName::find(inventJournalName));
        inventJournalTable.insert();

        //<Creation of journal Line>
        inventJournalTrans.clear();
        inventJournalTrans.initFromInventJournalTable(inventJournalTable);
        inventJournalTrans.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());
        inventJournalTrans.ItemId    = _itemid;
        inventJournalTrans.initFromInventTable(InventTable::find(_itemid));
        inventJournalTrans.Qty       = _qty;

        inventDim.wMSLocationId      = _location;
        inventDim.InventLocationId   = 'ware house';
        inventDim.InventSiteId       = InventLocation::find('ware house').InventSiteId;
        inventDim.InventSizeId       = 'Blank';  //TODO
        inventDim.LicensePlateId     = whsLicensePlate.LicensePlateId;
        inventDim.inventBatchId      = _batch;

        //you can add here as many inventory dimensions as required
        inventJournalTrans.InventDimId = inventDim::findOrCreate(inventDim).inventDimId;
        inventJournalTrans.insert();

        //<Post Journal>
        journalCheckPost = InventJournalCheckPost::newPostJournal(inventJournalTable);

        journalCheckPost.runOperation();

        ttscommit;

        info(strFmt('Journal %1 has been posted',inventJournalTable.JournalId));
    }
Keep Daxing!!

Release the Product using Data entity in D365FO using x++

 Release the Product  using Data entity in D365FO using x++.


    public void createReleasedProduct(ItemId _itemNum, ItemName _itemName)
    {
        EcoResReleasedProductCreationV2Entity   releasedProduct;
        WHSInventTable                          whsTable;
        WHSUOMSeqGroupId                        unitSequenceId;
        InventItemGroupItem                     inventItemgroupItem;
        ItemGroupId                             itemGroupId;

        releasedProduct.ItemNumber                      = _itemNum;
        releasedProduct.ProductName                     = _itemName;
        releasedProduct.ProductNumber                   = _itemNum;
        releasedProduct.ProductSearchName               = _itemName;
        releasedProduct.VariantConfigurationTechnology  = EcoResVariantConfigurationTechnologyType::PredefinedVariants;
        releasedProduct.ProductType                     = EcoResProductType::Item;
        releasedProduct.ProductSubType                  = EcoResProductSubtype::ProductMaster;
        releasedProduct.ProductDimensionGroupName       = 'Product dimension group name';
        releasedProduct.ItemModelGroupId                = 'Item model group';
        releasedProduct.StorageDimensionGroupName       = 'Storage dimension group';
        releasedProduct.TrackingDimensionGroupName      = 'Tracking dimension group';
        releasedProduct.InventoryReservationHierarchyName = 'ReservationHierarchy';
        releasedProduct.SalesUnitSymbol                 = 'Unit';
        releasedProduct.PurchaseUnitSymbol              = 'Unit';
        releasedProduct.InventoryUnitSymbol             = 'Unit';
        releasedProduct.BOMUnitSymbol                   = 'Unit';
        // releasedProduct.ProductGroupId                  = 'ProductGroupId';

        if (releasedProduct.validateWrite())
        {
            releasedProduct.insert();
        }

        whsTable = WHSInventTable::find(_itemNum, true);

        if (!whsTable)
        {
            whsTable.ItemId         = _itemNum;
            whsTable.FilterChanged  = NoYes::Yes;
            whsTable.UOMSeqGroupId  = unitSequenceId;
            whsTable.insert();
        }
        else
        {
            whsTable.UOMSeqGroupId = unitSequenceId;
            whsTable.update();
        }

        inventItemgroupItem = InventItemgroupItem::findByItemIdLegalEntity(_itemNum);

        if (!inventItemgroupItem)
        {
            inventItemgroupItem.ItemId              = _itemNum;
            inventItemgroupItem.ItemGroupId         = itemGroupId;
            inventItemgroupItem.ItemDataAreaId      = curExt();
            inventItemgroupItem.ItemGroupDataAreaId = curExt();
            inventItemgroupItem.insert();
        }
    }