Tuesday, December 16, 2014

Form datasource find record method

Form datasource find record method

Form datasource find record method to find and set that record as current. So, the user can easily identify the record.

We need to pass the table record as parameter to this findrecord method.

Syntax:

public boolean findRecord(Common record)

For example:


Here, I have added some code in the button click method to find the record in the form datasource. So, that result record will be focused as current record.

void clicked()
{
    Hari_ExamResult examResult;
   
    select firstOnly1 * from examResult
    where examResult.StudentName == 'Hari';
   
    if(examResult)
    {
        Hari_ExamResult_ds.findRecord(examResult);
    }
    super();
}

Result:



Thanks,
Hari

Monday, December 15, 2014

Create a new record in the opened form

Create a new record in the opened form


We can insert the record by using the below code. This is the normal procedure, creates new table buffer, assign values and call insert method.

void clicked()
{
    Hari_ExamResult  examResults;
    examResults.StudentName = "Zyx";
    examResults.Standard = "Sixth";
    examResults.ExamResult = "Pass";
    examResults.RollNo = 1004;
    examResults.insert();
    super();
}

Our scenario is how to add a new record when form opens. If you use the above code in this scenario we should call the below code after the insert method before the super method call.

    Hari_ExamResult_ds.research();
    Hari_ExamResult_ds.refresh();

Note: Here, form datasource reread method won’t work, because when we call the reread method, it won’t call the form datasource executequery method. But, if we call the research method, it will call the form datasource executequery method.

The best practice for this scenario is calling the form datasource create method.

void clicked()
{
    Hari_ExamResult_ds.create();
    Hari_ExamResult.StudentName = "Zyx";
    Hari_ExamResult.Standard = "Sixth";
    Hari_ExamResult.ExamResult = "Pass";
    Hari_ExamResult.RollNo = 1004;
    Hari_ExamResult.insert();
    Hari_ExamResult_ds.reread();
    Hari_ExamResult_ds.refresh();
    super();

}

Please use the form datasource create method to create the records when form opens.

Filtering records

Filtering records

Filter the records is the important part to display the data. By using the filter we can display the necessary data and avoid showing the unnecessary data based on the requirement.

In AX 2012, there is a default filter functionality. To use default filter select the form grid and press the ctrl+g shortcut to show/hide the filter options in the grid and then you can enter the values and press the enter key to filter the record.




We can achieve this functionality by using the x++ code.

For example, we need to filter the student results by pass, fail and all option.
I have added one combo box and grid in the form. Based on the combo box selection, I am filtering the student exam results.


Step 1:

Add the unbound combo box with the enum type. So, we can filter the records based on this combo box selected value.

Step 2:

Declare the query build datasource and query build data range variables in the form declarations section.
public class FormRun extends ObjectRun
{
    QueryBuildDataSource    qbds;
    QueryBuildRange         qbr;
}

Step 3:

Add the filter code in the combo box selection change event.
public int selectionChange()
{
    int ret;
    ret = super();

    qbds.clearRange(fieldNum(Hari_ExamResult, ExamResult)); //Clear the filter field range
    if(ExamResultCombo.valueStr() != '') //Check the combo box has value or not
    {
        qbr = qbds.addRange(fieldNum(Hari_ExamResult, ExamResult)); //Set the filter field
        qbr.value(ExamResultCombo.valueStr()); //Set the filter value
    }
    Hari_ExamResult_ds.executeQuery(); //Execute the form datasource query to apply the filter

    return ret;
}




Some other useful functions like
qbds.clearRanges(); //To clear all ranges

qbr = qbds.findRange(fieldNum(Hari_ExamResult, ExamResult)); //To find the field range already exists or not



Once you understand this code, you can apply this code for different scenarios.

Please try the below filtering records functionality to improve your knowledge in the filtering records.
  1. Equal
  2. Not equal
  3. Like
  4. Like after
  5. Between
  6. Or
  7. And
  8. Empty
  9. Not empty
  10. Get all datasource names
  11. Get all datasource ranges
  12. Clear all ranges
  13. Clear particular datasource range
  14. Less than
  15. Greater than
  16. Less than or equal to
  17. Greater than or equal to
  18. Range is exist or not
  19. Filter is exist or not
  20. Find or create range
  21. Find or create filter
  22. Query build data source. To string
  23. Find group by field
  24. Find field

Sorting records



Sorting records

Sorting is the most important thing when display the data to the user. The user mostly prefers sorting functionality in the grid forms. AX 2012 has inbuilt functionality to sort the records in the grid by simply clicking the header of the grid column. Multiple sorting is not supported in the AX 2012. For example sort the fields by class and student name. Only single field sorting functionality is working in AX 2012.
As per the below example, we need to sort the record by student name. So, we should add some code in the form datasource init methd like below.


public void init()

{

    super();
    this.query().dataSourceNo(1).addSortField(fieldNum(Hari_ExamResult, StudentName), SortOrder::Ascending);

}


We can also clear the sorting functionality by calling the form datasource sortClear method like below.

void clicked()

{

    Hari_ExamResult_ds.query().dataSourceNo(1).sortClear();

    Hari_ExamResult_ds.executeQuery();

    super();

}


We should call the form datasource execute query to reflect the changes.

We can change the sorting field by using the below code.
First, clear the existing sorting field then add new sorting field.

void clicked()

{

    Hari_ExamResult_ds.query().dataSourceNo(1).sortClear();

    Hari_ExamResult_ds.query().dataSourceNo(1).addSortField(fieldNum(Hari_ExamResult, Standard), SortOrder::Ascending);

    Hari_ExamResult_ds.executeQuery();

    super();

}

 

We can use this code logic to achieve some sorting related business requirement