Skip to main content

Notifications

Announcements

No record found.

Get parent datasource without solid relationships using FormDataObject

Sohaib Cheema Profile Picture Sohaib Cheema 46,610 User Group Leader

Recently went through this, and found no documentataion and/or blogpost anywhere, so thought to blogpost it; In case if anyone faces in future.

Imagine you want to implement some event on a child datasource where there are no strong relationships between two tables. Let’s take the example of InventDim. InventDim can save dimensions for any business process (be it sales lines, purchase lines, project lines etc.) At the same time, InventDim does not saves any ItemId with it because an InvetDim record can be associated with different tables. Same InventDimId can be associated with SalesLine and PurchLine etc. In such case if you want to get Parent record standing on child dataSource, it may not be very straight forward. Let assume, InventDim has join with ProdJournalTable. Now can you tell what is RecId of ProdJournalTable while you standing at InventDim..? No. Vice-versa you can. For example, you can tell what rec-id of inventDim is, which is located in ProdJournalTable. It is because InventDim is much normalized.

I went through similar situation last week when I was thinking to implement OnValidating for a field of InventDim where it was joined with parent table ProdJournalProd. There is no simple way to get parent DataSource using FormDataObject. If this was object type FormControl, there is no problem in getting all datasources of calling form. But not a simple way for the case of FormDataObject when the realtionships between tables are not very meaningful. In such cases, you cannot trust the realstionships between tables.

Here is the way how you can get parent DataSource Record while you are standing on child DataSource fields, It should do everything on its own. Also please note that in such case if you would try to use queryRun.get(tablenum(NameOfParentDataSourceTable)); it will not return a record, so the approach that you use in RDP classes usually, cannot be used in such cases. If you will try that, you will get no record and can get only name of Parent DS.

Here is my situation in picture.

Untitled.png

And what we want, is to get record of parent DataSource (ProdJournalProd) while being on Child DS using extensions and FormDataObject 

[FormDataFieldEventHandler(formDataFieldStr(ProdJournalTransProd, InventDim, inventBatchId), FormDataFieldEventType::Validating)]
    public static void inventBatchId_OnValidating(FormDataObject sender, FormDataFieldEventArgs e)
    {
        Query			currentQuery;
		QueryRun		queryRun;
        ProdJournalProd	currentProdJournalProd;
        int				dsCount,dsPlace;
        QueryBuildDatasource qbdsTest;

		FormDataSource	InventDim_ds			= sender.datasource();
        InventDim		currentInventDim		= InventDim_ds.cursor();
        
        //info(currentInventDim.inventBatchId);//Here is your current record

        FormDataSource      parentDatasource;
        FieldId             itemFieldId;
        InventTable         inventTableLocal;
        Common              parentBuffer;

        parentDatasource = formGetParentDatasource(InventDim_ds);
        if (parentDatasource)
        {

            itemFieldId = fieldName2id(parentDatasource.table(),fieldStr(InventTable,ItemId));
            if (itemFieldId)
            {
                inventTableLocal = InventTable::find(parentDatasource.cursor().(itemFieldId));
            }
            else if (parentDatasource.table())
            {
                parentBuffer = parentDatasource.cursor();
                if (parentBuffer.TableId && tableHasInstanceMethod(new DictTable(parentBuffer.TableId),identifierStr(ItemId))) // method Name
                {
                    inventTableLocal = InventTable::find(parentBuffer.itemId());
                }
            }

            //info(inventTableLocal.ItemId);//Here you have parent record

        }
		   
        


    }

Comments

*This post is locked for comments