Thursday, May 11, 2023

Extension button confirm purchase order on D365FO

[ExtensionOf(formControlStr(PurchTable, buttonConfirm))]

final class PurchTable_buttonConfirm_Extension

{

    public void clicked()

    {

        FormControl callerButton = any2Object(this) as FormControl;

        FormRun formRun = callerButton.formRun();

        FormDataSource purchTable_ds = formRun.dataSource(tablestr(PurchTable));

        PurchTable purchTable = purchTable_ds.cursor();

              

        info(strFmt('Before next PurchId %1', purchTable.PurchId ));


        next clicked();

        

        info(strFmt('After next PurchId %1', purchTable.PurchId ));

    }

}

Monday, September 26, 2022

Can not delete Main Account in D365FO. Main account XXXXXX is used 10 times as a default account on the Item groups(InventItemGroupForm) table in the Main account field.

When You try to delete Main Account and got error log as below: 



Main account Z116101 is used 10 times as a default account on the Item groups(InventItemGroupForm) table in the Main account field.

A financial dimension value is based on the Z116101 record and has been used on a transaction. You cannot delete the Z116101 record

But in Item Group setup form (or in Inventory posting) the Main account is not exist (not used by any Item group).


Solution for my case, 

 I had customized table : InventItemGroupForm, then show (visible) field: ItemGroupId.



Item Group Id: STG02A, is not shown on form. 

To delete the data in table InventItemGroupForm, create Item group Id with form InventItemGroup, use value: STG02A.

After you created item group id, all records related to Item group Id is deleted, then you can delete the Item group Id and delete the Main account related. 

 

Monday, August 29, 2022

Compare event handler when close child form with record value from caller form for D365FO

 class CustOpenTrans_form_Exthandler

{

    [PreHandlerFor(formStr(CustOpenTrans), formMethodStr(CustOpenTrans, close))]

    public static void CustOpenTrans_Post_close(XppPrePostArgs args)

    {        

        FormRun sender = args.getThis();

        CustTransOpen custTransOpen = sender.dataSource(formdatasourcestr(CustOpenTrans, CustTransOpen)).cursor();

        LedgerJournalTrans ljt = sender.args().record();

        info('Event: CustOpenTrans_Post_close');

        info(strFmt('Ledgertrans voucher %1',ljt.Voucher));

    }


    [FormControlEventHandler(formControlStr(CustOpenTrans, Save), FormControlEventType::Clicked)]

    public static void Save_OnClicked(FormControl sender, FormControlEventArgs e)

    {

        LedgerJournalTrans ljt = sender.formRun().args().record();        

        info('Event: Save_OnClicked');

        info(strFmt('Ledgertrans voucher %1',ljt.Voucher));

        

    }


    [FormEventHandler(formStr(CustOpenTrans), FormEventType::Closing)]

    public static void CustOpenTrans_OnClosing(xFormRun sender, FormEventArgs e)

    {

        LedgerJournalTrans ljt = sender.args().record();      

        info('Event: CustOpenTrans_OnClosing');

        info(strFmt('Ledgertrans voucher %1',ljt.Voucher));

    }

}

Sunday, August 28, 2022

Get caller datasource, click button closeOK in form CustOpenTrans, get caller record of form LedgerJournalTrans for D365FO

 class CustOpenTrans_form_Exthandler

{


    [PostHandlerFor(formStr(CustOpenTrans), formMethodStr(CustOpenTrans, closeOk))]

    public static void CustOpenTrans_Post_closeOk(XppPrePostArgs args)

    {

        FormRun sender = args.getThis();

        CustTransOpen custTransOpen = sender.dataSource(formdatasourcestr(CustOpenTrans, CustTransOpen)).cursor();


        LedgerJournalTrans ljt = sender.args().record();

        info(strFmt('Voucher: %1', ljt.Voucher ));

    }


}

Monday, August 15, 2022

Extension on modified form field for D365FO

 class PriceDiscAdmTable_ExtHandler

{

      [FormDataFieldEventHandler(formDataFieldStr(PriceDiscAdm, PriceDiscAdmTrans, relation), FormDataFieldEventType::Modified)]

    public static void relation_OnModified(FormDataObject sender, FormDataFieldEventArgs e)

    {

        FormDataSource priceDiscAdmTrans_ds = sender.datasource();

        PriceDiscAdmTrans _priceDiscAdmTrans = priceDiscAdmTrans_ds.cursor();


        if( _priceDiscAdmTrans.relation != PriceType::LineDiscPurch && 

            _priceDiscAdmTrans.relation != PriceType::PricePurch &&

            _priceDiscAdmTrans.relation != PriceType::LineDiscSales &&

            _priceDiscAdmTrans.relation != PriceType::PriceSales

          )

        {

            error('Relation: Only price or line discount is valid selection!');

            throw exception::Error;

        }

    }

}

Extension add table field method display name for D365FO

 public static class PriceDiscAdmTrans_Extension

{

    [SysClientCacheDataMethodAttribute(true)]  //This attribute will cache your display method.

    public static display Name My_ItemName(PriceDiscAdmTrans _this)

    {

        return InventTable::find(_this.ItemRelation).productDescription('en-us');

    }

}

Import file TXT using job class in D365FO

 class JOB_ImportUOM

{        

    public static void main(Args _args)

    {        

        UnitOfMeasure                       _uom;

        UnitOfMeasureConversion             _uomConvert;

        UnitOfMeasureTranslation            _uomTranslation;

        UnitOfMeasureClass                  uomClass;


        container                           rec;

        TextStreamIo                        file;

        FileUploadTemporaryStorageResult    fileUpload;


        str                                 fromUnitx,toUnitx,fromUnitNamex,toUnitNamex,classUnitx;

        int                                 sep;

        int                                 counterx,i;

        ;

        fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;

        file = TextStreamIo::constructForRead(fileUpload.openResult());


        try

        {

            if (file)

            {

                if (file.status())

                {

                    throw error("@SYS52680");

                }

                file.inFieldDelimiter('^');

                //file.inRecordDelimiter('\r\n');

            }


            while (!file.status())

            {

                rec = file.read();


                fromUnitx                          = conPeek(rec, 1);

                fromUnitNamex                      = conPeek(rec, 2);

                classUnitx                         = conPeek(rec, 3);


                _uom = UnitOfMeasure::findBySymbol(fromUnitx);


                if(!_uom)

                {


                    ttsBegin;

                    _uom.clear();

                    _uom.Symbol = fromUnitx;


                    switch(classUnitx)

                    {

                        case 'Length':

                            uomClass = UnitOfMeasureClass::Length;

                            break;


                        case 'Liquid volume':

                            uomClass = UnitOfMeasureClass::LiquidVolume;

                            break;


                        case 'Mass':

                            uomClass = UnitOfMeasureClass::Mass;

                            break;


                        case 'Quantity':

                            uomClass = UnitOfMeasureClass::Quantity;

                            break;


                        default:

                            uomClass = UnitOfMeasureClass::Quantity;

                            break;


                    }

                    _uom.UnitOfMeasureClass =   uomClass;

                    _uom.IsInventUnit = NoYes::Yes; // custom field

                    _uom.validateWrite();

                    _uom.insert();

                    ttsCommit;


                    ttsBegin;

                    _uomTranslation.clear();

                    _uomTranslation.UnitOfMeasure = _uom.RecId;

                    _uomTranslation.Description = fromUnitNamex;

                    _uomTranslation.LanguageId = 'en-us';

                    _uomTranslation.validateWrite();

                    _uomTranslation.insert();

                    ttsCommit;


                    //info(strFmt("%1 %2 %3 %4",fromUnitx,classUnitx,_uom.Symbol, _uomTranslation.Description));

                    counterx++;

                }


            }

            info('Import success!');            

        }

        //try

        catch

        {

            error("Error!");

            return;

        }

        info(strFmt("Done! %1 records is imported !",counterx));

    }

}