How to filter and sort by dynamically created fields in grid?

I am creating a form in a job using

Form form;

FormRun formRun;

FormBuildDesign formBuildDesign;

FormBuildDataSource formBuildDataSource;

form = new Form();.

formBuildDataSource = form.addDataSource(“Table”);

formBuildDesign = form.addDesign(“Design”);

formBuildDesign.widthMode(1);

formBuildDesign.heightMode(1);

form.design().caption(“Caption”);

I am adding a grid to the form by

formBuildGridControl = formBuildDesign.addControl(FormControlType::Grid,“Table Grid”);

and than fields (they are named F1, F2, …) to this grid by

for(ii = 1; ii < 5; ii++)

{

formBuildGridControl.addDataField(formBuildDataSource.id(),fieldName2Id(tableName2id(“Table”), strFmt(“F%1”,ii)));

}

Form is created, grid is added and visible but when I try to sort by a field I get

Invalid sort field type.

and I do not see filter function.

What I need to do to have sorting and filtering available?

I can’t reproduce your problem.

Can you tell me whether following code works in your system?

Form                    form = new Form();    
FormBuildDesign         design = form.addDesign("Design");
FormBuildDataSource     buildDs = form.addDataSource("CustTable");
FormBuildGridControl    grid = design.addControl(FormControlType::Grid, "Grid");
Args                    args = new Args();
FormDataSource          ds;
FormRun                 formRun;    

buildDs.table(tableNum(CustTable));
    
grid.addDataField(buildDs.id(), fieldNum(CustTable, AccountNum));
    
args.object(form);    
formRun = classfactory.formRunClass(args);
formRun.run();
    
ds = formRun.dataSource(1);
ds.queryBuildDataSource().addSortField(fieldNum(CustTable, AccountNum), SortOrder::Descending);
ds.executeQuery();
    
formRun.wait();

By the way, if you post code that can be copied to AX and run without extra work, you’ll have a better chance that somebody will bother.

The fields has a type String with size Memo. With limited length everything works correctly.

Ah, that’s by design.

You should have debug static design first - if you debug too many things at once, you easily miss where the problem lies.

I hope that my code helped you at least a little; I would hate to hear that I just wasted my time. :slight_smile:

It help me but with different tasks :slight_smile: