axaptadynamics-365-operations

Using InMemory table as a form datasource in Dynamics 365 f&o


I'm obtaining data from an external service and inserting it into an inMemory table (Table_movieTemp), which I use as a datasource on a form (Form_MovieSearch_ds):

[FormControlEventHandler(formControlStr(Form_MovieSearch, FormCommandButtonControl1), FormControlEventType::Clicked)]
public static void FormCommandButtonControl1_OnClicked(FormControl sender, FormControlEventArgs e)
{
    FormDataSource Form_MovieSearch_ds = formRun.dataSource();
    System.Collections.IEnumerable  data =  ClassLibrary1.Program::CallRestService();
    var enumerator = data.getEnumerator();

    while(enumerator.moveNext())
    {
        MovieRentalService.TmdbMovie item = enumerator.get_current();
        Table_movieTemp.Description = item.Description;
        Table_movieTemp.ReleaseDate = today();
        Table_movieTemp.Title = item.Title;
        Table_movieTemp.Rating = item.Rating;
        Table_movieTemp.existsAlready = Table_Movie::exist(item.Title);
        insertList.add(movieTemp);
    }
    ttsbegin;
    insertList.insertDatabase();
    ttscommit;

    while select Table_movieTemp
    {
        info(strFmt("Name: %1,", Table_movieTemp.Title));
    }

The while loop I used purely to prove the inserts were succesful. Afterwards I figure I can call the executeQuery on the form which has my temptable as datasource:

    FM_MovieSearch_ds.executeQuery();

This did not work and when I searched google I found a solution where I have to pass the TempTable buffer so that I can link it using 'setTmpTable'. So I added the following call before calling executeQuery():

    formRun.BindTable(movieTemp);

Function on my form:

public void BindTable(FM_MovieTemp _movieTempBuffer)
{
    _movieTempBuffer.setTmpData(_movieTempBuffer);
}

Now my code compiles and does not generate runtime errors either, but I still don't see any data. Could someone advice what I miss or do wrong?


Solution

  • The use of in-memory tables in forms has been around for 25 years, and you will find several uses in the standard application.

    From the CustVendAgingStatistics form:

    void calcAgingStatistics(boolean _research)
    {
        CustVendAgingStatistics custVendAgingStatistics = CustVendAgingStatistics::construct(linkedCustVendTable, graphData.loadDefName(), graphData.perInvoiceDate());    
        custVendAgingStatistics.calcStatistic();
        tmpAccountSum.setTmpData(custVendAgingStatistics.tmpAccountsum());    
        if (_research)
        {
            tmpAccountSum_ds.research();
        }
    }
    

    Another nice example is found here.

    The method:

    In your code I see the use of InsertRecordList, do not use that on in-memory temporary tables, it makes no sense. Also _movieTempBuffer.setTmpData(_movieTempBuffer) does not do anyting useful as it operates on itself.

    Also good style is not do a lot in onClicked methods and other event methods, call proper methods to do the hard work instead.