Showing posts with label overload. Show all posts
Showing posts with label overload. Show all posts

Thursday, February 10, 2022

Overload a method for a new button in Form extension

 This can be used as a copy-paste pattern, when you need to change a standard method for new form controls added as a form extensions.

At the form initialization step we can overload any form control methods with registerOverrideMethod.

[ExtensionOf(formStr(<FormName>))]
public final class my<FormName>_MyNewButton_Extension
{
   public void init()
    {
        next init();
		// MyNewButton button added in an extension to <FormName>
        FormButtonControl myButton = this.design().controlName(formControlStr(<FormName>, myNewButton));
		// Here we can overload its standard clicked() method in run-time		
        myButton.registerOverrideMethod(methodStr(FormButtonControl, clicked), formMethodStr(<FormName>, myNewButtonClicked), this);
    }

    public void myNewButtonClicked(FormButtonControl _sender)
    {
		<run some logic>
        _sender.clicked();
    }
}

Check this article for more complicated scenario https://alexvoy.blogspot.com/2018/09/lookup-and-modified-methods-for.html

Wednesday, September 12, 2018

Lookup and Modified methods for FormReferenceGroup fields in D365

Let's say we need to keep in Purchase line a manufacturer code for a particular product. So each Product/Manufacturer code combination is unique.





Three tables are referenced via RecId fields.









So once Manufacturer code field is placed in the form, we end up with a FormReferenceGroup.





We can easily override its lookup method by subscribing to the relevant event on it.

 [FormControlEventHandler(formControlStr(PurchTable, myEcoResManufacturerProduct_myEcoResManufacturerProductRecId), FormControlEventType::Lookup)]
    public static void myEcoResManufacturerProduct_myEcoResManufacturerProductRecId_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        PurchLine                           purchLine = sender.formRun().dataSource(formDataSourceStr(PurchTable, PurchLine)).cursor() as PurchLine;
        FormControlCancelableSuperEventArgs cancelableArgs = e as FormControlCancelableSuperEventArgs;
        
        myEcoResManufacturerProduct::lookupByItem(sender, purchLine.itemId);
        
        cancelableArgs.CancelSuperCall();
    }

public client static Common lookupByItem(FormReferenceControl _formReferenceControl, ItemId _itemId)
    {
        SysReferenceTableLookup sysReferenceTableLookup;
        Query                   query;
        QueryBuildDataSource    myEcoResMan;

        sysReferenceTableLookup = SysReferenceTableLookup::newParameters(tableNum(myEcoResManufacturerProduct), _formReferenceControl);
        sysReferenceTableLookup.addLookupfield(fieldNum(myEcoResManufacturerProduct, EcoResManufacturerRecId));
        sysReferenceTableLookup.addLookupfield(fieldNum(myEcoResManufacturerProduct, EcoResManufacturerPartNbr));

        query = new Query();
        myEcoResMan = query.addDataSource(tableNum(myEcoResManufacturerProduct));
        myEcoResMan.addRange(fieldNum(myEcoResManufacturerProduct, EcoResProductRecId)).value(SysQuery::value(InventTable::find(_itemId).Product));

        sysReferenceTableLookup.parmQuery(query);

        return sysReferenceTableLookup.performFormLookup() as myEcoResManufacturerProduct;
    }






But what if the user wants to create new values in appropriate tables if them do not exist yet?




We can catch the modified event in order to create new values before failing the validation.
However, given that its content may be changed, it is impossible to get access to its fields at design time.

We can do it during run-time by means of getting sought field form controls by their names and overloading then their Modified() methods. (see the similar trick for AX 2012 https://alexvoy.blogspot.com/2014/01/how-to-set-properties-for-reference.html)





The code you need to add.

[ExtensionOf(formStr(PurchTable))]
final class myPurchTableForm_PurchTableManuf_Extension
{
    private const  str          myFieldNameDisplayProductNumber    = 'EcoResManufacturerPartNbr';
    private const  str          myFieldNameEcoResManufacturerName  = 'EcoResManufacturerName';
    private FormStringControl   myFSCDisplayProductNumber;
    private FormStringControl   myFSCEcoResManufacturerName;
    
    // the only way to change the standard modified method for a control inside of a dynamically populated reference group
    // is to get it by its name looping all form controls of this group during run-time. then to overload it
    [FormEventHandler(formStr(PurchTable), FormEventType::Initialized)]
    public void PurchTable_OnInitialized(xFormRun sender, FormEventArgs e)
    {
        FormDesign                      formDesign = sender.design();
        FormReferenceGroupControl       formReferenceGroupControl;
        formReferenceGroupControl = formDesign.controlName(formControlStr(PurchTable, myEcoResManufacturerProduct_myEcoResManufacturerProductRecId)) as formReferenceGroupControl;
        this.registerManufacturerNameOverload(formReferenceGroupControl);
    }

    private void registerManufacturerNameOverload(FormReferenceGroupControl _formReferenceGroupControl )
    {
        int                                 i;
        Object                              childControl;
        FormStringControl                   formStringControl;

        for (i = 1; i <= _formReferenceGroupControl.controlCount(); i++) // FilterCategory is of FormReferenceGroupControl type
        {
            childControl = _formReferenceGroupControl.controlNum( i );
            formStringControl = childControl as formStringControl;
            if(formStringControl.DataFieldName() == myFieldNameEcoResManufacturerName)
            {
                myFSCEcoResManufacturerName = formStringControl;
                formStringControl.registerOverrideMethod(methodStr(formStringControl, modified), formMethodStr(PurchTable, myEcoResManufacturerName_modified_overload),
                    this);
            }
            if(formStringControl.DataFieldName() == myFieldNameDisplayProductNumber)
            {
                myFSCDisplayProductNumber = formStringControl;
                formStringControl.registerOverrideMethod(methodStr(formStringControl, modified), formMethodStr(PurchTable, myDisplayProductNumber_modified_overload),
                this);
            }
        }
    }

    /// <summary>
    /// We have to allow the user to insert any value, even though such a value is not found in the referenced table;
    /// then we will ask whether this new value must be created in the table and set this new value for the current record
    /// </summary>
    /// <param name = "_sender">EcoResManufacturerName</param>
    public void myEcoResManufacturerName_modified_overload(FormStringControl _sender)
    {
        myEcoResManufacturer myEcoResManufacturer = myEcoResManufacturer::findOrCreateByName(_sender.text());
        _sender.modified();
    }

    /// <summary>
    /// We have to allow the user to insert any value, even though such a value is not found in the referenced table;
    /// then we will ask whether this new value must be created in the table and set this new value for the current record
    /// </summary>
    /// <param name = "_sender">EcoResManufacturerName</param>
    public void myDisplayProductNumber_modified_overload(FormStringControl _sender)
    {
        Common          comm = _sender.dataSourceObject().cursor();
        PurchLine       purchLine   =  _sender.parentControl().dataSourceObject().cursor() as PurchLine;

        myEcoResManufacturerProduct myEcoResManufacturerProduct = myEcoResManufacturerProduct::findOrCreateEcoResManufacturerProduct(
                                                                                                        purchLine.itemId, 
                                                                                                        myFSCEcoResManufacturerName.text(),
                                                                                                        _sender.text());
        _sender.modified();
    }

}

Table methods

public static myEcoResManufacturer findOrCreateByName(myEcoResManufacturerName       _manufacturerName)
    {
        myEcoResManufacturer           myEcoResManufacturer = myEcoResManufacturer::findByName(_manufacturerName);
        if(!avrEcoResManufacturer)
        {
            if(Box::confirm("Do you want to creare new manufacturer?", strFmt(myEcoResManufacturer::txtNotExist(), _manufacturerName)))
            {
                try
                {
                    myEcoResManufacturer.EcoResManufacturerName = _manufacturerName;
                    if(myEcoResManufacturer.validateWrite())
                    {
                        myEcoResManufacturer.insert();
                    }
                }
                catch
                {
                    Error("Failed to create new manufacturer");
                }
            }
        }
        return myEcoResManufacturer;
    }


public static myEcoResManufacturerProduct findOrCreateEcoResManufacturerProduct(ItemId                         _itemId,
                                                                                    myEcoResManufacturerName       _manufacturerName,
                                                                                    myEcoResManufacturerPartNbr    _manufacturerPartNbr)
    {
        myEcoResManufacturerProduct    myEcoResManufacturerProduct;
        myEcoResManufacturer           myEcoResManufacturer;
        InventTable                     inventTable = InventTable::find(_itemId);
        // item and part number are given and exist
        if(inventTable.Product && _manufacturerPartNbr)
        {
            // such a manufacturer exists, so just try to find it for given combination
            myEcoResManufacturer           = myEcoResManufacturer::findOrCreateByName(_manufacturerName);
            myEcoResManufacturerProduct    = myEcoResManufacturerProduct::find(inventTable.Product, myEcoResManufacturer.RecId);
            if(!myEcoResManufacturerProduct)
            {
                if(Box::confirm("Do you want to create new part number", strFmt(myEcoResManufacturerProduct::txtNotExist(), _manufacturerPartNbr)))
                {
                    try
                    {
                        myEcoResManufacturerProduct.EcoResProductRecId         = inventTable.Product;
                        myEcoResManufacturerProduct.EcoResManufacturerRecId    = myEcoResManufacturer.RecId;
                        myEcoResManufacturerProduct.EcoResManufacturerPartNbr  = _manufacturerPartNbr;
                        myEcoResManufacturerProduct.EcoResManufacturerDefault  = NoYes::Yes;
                        if(myEcoResManufacturerProduct.validateWrite())
                        {
                            myEcoResManufacturerProduct.insert();
                        }
                    }
                    catch
                    {
                        Error("Failed to create new part number");
                    }
                }
            }
        }
        return myEcoResManufacturerProduct;
    }


Friday, February 2, 2018

Parallel Items/Product Validating or Deleting

If you need to validate or delete items/products or any other records in a BIG number, it is better to run such processing, first, in CIL, second in parallel threads.

This project is to demonstrate this approach.

The whole concept is similar to what I explained in one of my previous blogpost about Multi thread parallelism and a dispatching table for finding a minimum

myInventItemProcessBatch class populates a special table containing RecIds to be processed and thread number they belong to.



Based on the user selection, it creates appropriate number of batch tasks that can run independently with their progress percentage.




Feel free to elaborate this project by adding new types of processing or new table to process. Also it is probably a good idea to add a new column to the table to separate different instances myInventItemProcessBatch simultaneously running in the same environment.


myInventItemProcessBatch 

private static server int64 populateItems2Process(str 20 _what2find, int _batchThreads)
{
    myInventItemProcessTable    myInventItemProcessTable;
    InventTable                 inventTable;
    int                         firstThread = 1;
    Counter                     countr;
    // flush all previously created items from the table
    delete_from myInventItemProcessTable;
    // insert all needed items in one shot. this part can be refactored to use Query instead
    insert_recordset myInventItemProcessTable (threadNum, ItemRecId, ItemId)
    select firstThread, RecId, ItemId from InventTable
    where
        inventTable.itemId like _what2find;
    // now group them in threads by simply enumerating them from 1 to N
    countr=1;
    ttsBegin;
    while select forUpdate myInventItemProcessTable
    {
        myInventItemProcessTable.threadNum = countr;
        myInventItemProcessTable.update();

        countr++;

        if(countr > _batchThreads)
        {
            countr=1;
        }
    }
    ttsCommit;
    // return the total number of items to process
    select count(RecId) from myInventItemProcessTable;

    return myInventItemProcessTable.RecId;
}

public void run()
{
    // get all required items by their RecIds in the table and group them in threads
    int64 totalRecords = myInventItemProcessBatch::populateItems2Process(what2find, batchThreads);
    if(totalRecords)
    {
        info(strFmt("Found %1 items like '%2' to %3", totalRecords, what2find, processType));
        // create number of batch tasks to parallel processing
        this.scheduleBatchJobs();
    }
    else
    {
        warning(strFmt("There are no items like '%1'", what2find));
    }
}
myInventItemProcessTask process()

... 
select count(RecId) from inventTable
        exists join myInventItemProcessTable
        where
            myInventItemProcessTable.ItemRecId == inventTable.RecId &&
            myInventItemProcessTable.threadNum == threadNum;
    // total number of lines to be processed
    totalLines = inventTable.reciD;
    // to enjoy our bored user during a few next hours
    // this progress just updates percentage in Batch task form
    progressServer = RunbaseProgress::newServerProgress(1, newGuid(), -1, DateTimeUtil::minValue());
    progressServer.setTotal(totalLines);

    while select inventTable
        exists join myInventItemProcessTable
        where
            myInventItemProcessTable.ItemRecId == inventTable.RecId &&
            myInventItemProcessTable.threadNum == threadNum
    {
        progressServer.incCount();

        try
        {
            // RUN YUR LOGIC HERE //////////////////////
...