web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to use datasource from Form to Class?

(0) ShareShare
ReportReport
Posted on by 272

I have a form where I need to populate my grid. (I'm using TMP table)

I made a class containing all the methods I need to populate the grid. I thought about placing all my methods in a class because I'll be re-using some of the methods in report generation. 

Basically, the logic is when I click my generate button in the form it will call the methods to populate the grid where it inserts to the tmp table and sets the tmpdata. 

public void generate() //code in class
{
  ttsBegin;
  .
  .
  .
  TmpTable.insert();

  TmpTable.setTmpData(tmpTable);

  ttsCommit;
}

void clicked() //code in form
{
   Class  class = Class:construct();

   class.generate();

   TmpTable_ds.research();
   TmpTable_ds.refresh();
}

I already made it work when all of my methods are in my form, but now I moved everything to a class.

As I'm calling my methods in the class, the grid is not populating. So I think that the data source is not being properly called. How can I use my data source from my form into my class?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,166 Moderator on at

    It will not matter where the code is executed, as long as you have the real tmpTable buffer at hand on your form, so that you can pass it to the data source.

    In your code above I don't see where you pass the tmpTable buffer to your form data source. Instead you seem to have a local variable of your TmpTable in your class. But how would the form see that? Actually that TmpTable is accessible only inside your generate method and it's deleted right after generate method is executed. Temporary table data is only available in the original declared table buffer. That explains why it doesn't work.

    Your class method should return the tmpTable buffer, so that you could pass it to your form data source.

    Something like this

    public TmpTable generate()
    {
        TmpTable tmpTable;
        tmpTable.Field1 = "FOO";
        tmpTable.insert();
    
        return tmpTable;
    }
    
    void clicked() //code in form
    {
       Class  class = Class:construct();
    
       TmpTable.setTmpData(class.generate());
    
       TmpTable_ds.refresh();
       TmpTable_ds.research();
    }
  • Suggested answer
    Rustem Galiamov Profile Picture
    8,072 on at

    Hi Miguel Zuniga!

    We've created something similar. As example, the method on a class:

    static void dsRefresh(Common                _record,
                          UNC_dsRefreshAction   _action,                    
                          NoYes                 _savePosition = NoYes::Yes, 
                          NoYes                 _forceActive  = NoYes::Yes  
                          )
    {
        FormDataSource  formDataSource   = _record.datasource();
        FormDataSource  parentDataSource = formDataSource;
        Common          record;
        boolean         savePosition     = _savePosition && _action != UNC_dsRefreshAction::RereadRefresh;
        ;
    
    
        if (formDataSource)
        {
            while (parentDataSource.joinSource() && parentDataSource.linkType() == FormLinkType::InnerJoin)
            {
                parentDataSource = formGetParentDatasource(parentDataSource);
            }
            if (savePosition)
            {
                record = parentDataSource.cursor().data();
            }
            switch (_action)
            {
                case UNC_dsRefreshAction::RereadRefresh:
                    parentDataSource.reread();
                    parentDataSource.refresh();
                    break;
                case UNC_dsRefreshAction::Research:
                    parentDataSource.research();
                    break;
                case UNC_dsRefreshAction::ExecuteQuery:
                    parentDataSource.executeQuery();
                    break;
            }
            if (savePosition)
            {
                parentDataSource.findRecord(record);
            }
            if (_forceActive)
            {
                parentDataSource.active();
            }
        }
    }


    Hope this will helpful.

  • Miguel Zuniga Profile Picture
    272 on at


    Hi Nikolaos, thanks answering.

    I did what you said and unfortunately the grid is not being populated.

    I tried debugging and here what I saw:

    In returning the table buffer from the class it gets all the records, in my case I have 210 records.

    return tmpTable; //it gets the 210 records

    But when I'm in the form I did this to check:

    TmpTable tmpTable; //declared in class declaration
    
    void clicked()
    
    {
    
    tmpTable = class.generate(); //I checked the tmpTable value in the debugger and it got 1 record only and it seems that it's empty 
    
    TmpTable.setTmpData(tmpTable);
    TmpTable_ds.refresh();
    
    TmpTable_ds.research();
    
    }


  • Suggested answer
    nmaenpaa Profile Picture
    101,166 Moderator on at

    You can't see all lines in the debugger, you only see the first line of the table buffer.

    Let's forget your button for a while. Instead please put the population logic in the init method of your form, and don't call refresh/research. Does that work? If yes, you can start figuring out how to populate the grid in the clicked method.

  • Miguel Zuniga Profile Picture
    272 on at

    Hi Nikolaos,

    Placed it in the init method form. Unfortunately grid is still empty. :(

    Also confirmed that all 210 records are passed to the form (checked record count of the table buffer)

  • nmaenpaa Profile Picture
    101,166 Moderator on at

    Then you still have something wrong. Does your grid have the TmpTable set as a data source?

  • Miguel Zuniga Profile Picture
    272 on at

    Yes it does.

    Do I have to use my datasource instance? cause previously when my code is all in the form I am using my datasource instance itself. I didn't create an instance of my tmp table because it said that it is already used in an outer scope

    When I thought of that I tried to equate it to the datasource instance itself..

    tmpTable = class.generate(); 

    but then I got the error: "the assignment is invalid because the variable is read only". 

  • Suggested answer
    nmaenpaa Profile Picture
    101,166 Moderator on at

    I'm not sure what you mean exactly, and your code doesn't show where "tmpTable" variable is declared. You used it already in the previous posts and didn't mention about errors.

    What if you try simply this:

    TmpTable.setTmpData(class.generate());


  • Miguel Zuniga Profile Picture
    272 on at

    What I meant is that I didn't need to declared the "tmpTable" because I am already using it as a datasource so I can call it directly.

    Sadly, grid is still empty. :(

  • Rustem Galiamov Profile Picture
    8,072 on at

    Which type of temp table did you use?

    InMemory ot TempDB ?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 16

#2
GiacomoRovai Profile Picture

GiacomoRovai 4

#3
Douglas Noel Profile Picture

Douglas Noel 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans