Leader Board

Get day, month and year from date in axapta x++

In Dynamics AX, sometimes we need to get the day, month or year from a date and we don't know how to do this... but we can do this easily...


//Get day from date
dayOfMth(systemdateget())

//Get month from date
mthOfYr(systemdateget())

//Get year from date
year(systemdateget())"

Passing Parameters to a Report in Dynamics AX « Rehan Aqeel's Blog

Passing Parameters to a Report in Dynamics AX « Rehan Aqeel's Blog:


Creating reports in AX is generally ‘a walk in the park’ even for the new developers not fully acquaint with the syntax of X++. However, it sometimes becomes challenging to understand how the parameters could be passed to a report when you want to initiate that report from a form.

Let’s look at an example where you may want to create a ‘Purchase Requisition Report’ that the users could generate to see the status of each purchase requisition and the associated workflow entries for that Purchase Requisition.
I will not go into the details of how to develop this report, but you should know that such a report do not exist in the out-of-the-box functionality within AX 2009.
So let’s say that you have developed this report and now when you generate this report you can pass on the required ‘Purchase Requisition ID’ in the ‘Parameter Prompt’ of the report to view the particular PR details accordingly.
However, now in the next step you want to call this report from the ‘Purchase Requisition Form’ (i.e PurchReqTable). To do this, you should first create a ‘Button’ within the ButtonGroup on the form.
Now right-click on the ‘Methods’ node within the newly created button and choose ‘Override Method –> clicked’
Within the clicked function, you should write the following code;
void clicked()
{
Args args = new args();
ReportRun reportRun;
;
args.parm(PurchReqTable.PurchReqId);
args.name(reportstr(PurchRequisitionDetails));
reportRun = classFactory.reportRunClass(args);
reportRun.init();
reportrun.run();
//hello
super();
}
The above code calls the args() system class. This class is used to pass arguments to a class-constructor. You can pass information such as name, caller, and parameters to a new class. This code passes the relevant Purchase Requisition ID (which is currently selected by the user on the form) to the report ‘PurchRequisitionDetails’ in the above code.
Now, you need to go to the report (which in this case is PurchRequisitionDetails). Right-Click on the methods node of the Report and click on ‘Override Method –> init’
In this init method, you need to enter the following code to allow the report to read the parameters from the form.
public void init()
{
;
try
{
if(element.args().parm())
{
this.query().dataSourceTable(tablenum(PurchReqTable))
.addRange(fieldnum(PurchReqTable, PurchReqId)).value(element.args().parm());
this.query().userUpdate(false);
this.query().interactive(false);
super();
}
}
catch(exception::Error)
{
info(“Error in init method”);
}
}
Once you have got the above code correctly entered, you should now be able to generate your report directly from the form.

Joining Data Sources on Forms

Joining Data Sources on Forms

Data sources can be joined together in the following ways:

  • Display data from more than one table, as if it is a single data source.

    For example, fields from more than one table might be displayed in a single grid.

    With this type of join, the two data sources, in many respects, function as a single data source. For example, if you use an inner join and if a user is modifying or adding data to a record in a grid, neither table will be updated until the whole "record" (grid row) is exited rather than a table being updated as the user moves from one data source to another within the row.

  • Display data from more than one table where there is a parent/child relationship between the data sources. The two data sources are kept visually separate.

    For example, order records might be displayed as the parent data source in one grid and order lines as the child data source in another grid.

TipTip

For more information about joining data sources, see the tutorial_Form_Join form.


  1. Add both data sources to the Data Sources node in the form.

  2. Set the JoinSource property on the secondary data source to the name of the main data source.

  3. Set the LinkType property on the secondary data source to InnerJoin, OuterJoin, ExistJoin, or NotExistJoin.

    The LinkType property settings are described later in this topic.

  4. Create the form design, and then display the relevant fields from the form data sources. Typically, you would choose a grid control for your joined data sources.

NoteNote

You also need to write code to update data in shared fields. For example, if there is a relationship between the tables on Table1.Field1 and Table2.Field2, only one of these fields will be displayed in the form (because the fields are identical). When a change is made to Table1.Field1 on the form, however, Table2.Field2 is not automatically updated. You must add code to the form—for example, on the modified method for Table.Field1.


Advantages of Joining Data Sources

It is also possible to display data from other data sources by using the display or edit methods. Following are the advantages to joining data sources:

  • Performance is increased if you join data sources, compared to using the display or edit methods. Selects, inserts, and updates can occur simultaneously in more than one table because a single query is used.

  • Navigation is synchronized in both data sources—when the user selects a new record, both data sources are updated at the same time.

  • Notification methods, such as active, are run for both data sources. Methods that operate on the data source, such as next, executeQuery, and so on, are redirected to the data source that owns the join, the main data source. The init method is run for both and/or all data sources.

Display data from more than one table where there is a parent/child relationship between the data sources, and the two data sources are kept visually separate.

  1. Add both data sources to the Data Sources node in the form.

  2. Set the JoinSource property on the child data source to the parent data source.

  3. Set the LinkType property on the child data source to Active, Passive, or Delayed.

    The LinkType property controls how the child data source is updated in relation to changes in the master data source.

  4. Create the form design, and then display the relevant fields from the form data sources. Typically, you would choose two grid controls—one to display the parent data source and one to display the child data source.

The following table describes the different settings for the LinkType property.

Use Passive, Delayed, and Active when you join data sources that have a parent/child relationship, and InnerJoin, OuterJoin, ExistJoin, and NotExistJoin should be used when joining data sources to appear as one data source.

Setting

How the join is performed

Passive

Linked child data sources are not updated automatically. Updates of the child data source must be programmed on the active method of the master data source.

Delayed

A pause is inserted before linked child data sources are updated. This enables faster navigation in the parent data source because the records from child data sources are not updated immediately. For example, the user could be scrolling past several orders without immediately seeing each order lines.

Active

The child data source is updated immediately when a new record in the parent data source is selected. Continuous updates consume lots of resources.

InnerJoin

Selects records from the main table that have matching records in the joined table and vice versa.

There is one record for each match. Records without related records in the other data source are eliminated from the result.

OuterJoin

Selects records from the main table whether they have matching records in the joined table.

ExistJoin

Selects a record from the main table for each matching record in the joined table.

The differences between InnerJoin and ExistJoin are as follows:

  • When the join type is ExistJoin, the search ends after the first match has been found.

  • When the join type is InnerJoin, all matching records are searched for.

NotExistJoin

Select records from the main table that do not have a match in the joined table.

Using the display Method Modifier

Using the display Method Modifier

The display method modifier is used to indicate that a method’s return value is to be displayed on a form or a report.

If you want users to edit the value in the control, use an edit method.

Use the display modifier on methods in the following:

  • Table methods

  • Form methods

  • Form data source methods

  • Report methods

  • Report design methods

Write your display methods on a table. You can use the same code in several forms or reports.

display methods are called each time the form is redrawn. They should not contain complex and time-consuming calculations.

NoteNote

display methods can result in unintended information disclosure. For more information, see Security on Display and Edit Methods.

display methods are not activated if they are on a hidden tabbed page.

display methods can be cached. For more information about caching display methods, seeCaching display Methods.


To create a display method, place the display keyword immediately in front of the method’s return type. For example:

display Amount amount()

display methods must have a return type. The return value is typically a calculated value (for example, a sum). For an example, see How to: Create Form Controls.

There should no parameters in a display method unless it is on a form data source where you must include the data source as a parameter. For example:

display InventQty accumulated(InventBudgetInvent Budget)

NoteNote

You must use display on a form data source method when the return value is to be shown on a grid control.


To use a display method on a form or report control, the control and the return type of the method must have identical types. For example, if you have a RealEdit control on your form, the display method you are using must return a value of type real.

Add the display method to a form control.

  1. Set the DataSource property for the control to the data source that contains the method.

    If you do not set the DataSource property, the system assumes that the method has been defined on the form.

  2. Set the DataMethod property to the name of the method.

You might also want to set the ExtendedDataType or ArrayIndex properties:

  • If the ExtendedDataType property is set, formatting, Help text, and so on are inherited from the type specified here.

  • If the display method returns an array, set ArrayIndex to 0 to indicate that all array elements are to be shown in the control. If, for example, you set it to 2, only array element number two is shown.

Add a display method to a report control.

  1. Set the Table property for the control to the table that contains the method.

    If you do not set the Table property, the system assumes that the method has been defined on the report—the report design.

  2. Set the DataMethod property to the name of the method.

Dynamics AX 2009 Development: Creating a new Number Sequence for an Existing Module, and Putting it to Use!

Dynamics AX 2009 Development: Creating a new Number Sequence for an Existing Module, and Putting it to Use!


Creating a new Number Sequence for an Existing Module, and Putting it to Use!

When I first learned how to create a Number Sequence in my Dynamics AX 2009 Development IV Class (80014) in February of 2010, I left with more confusion about the topic than it should have, and I dreaded coming back to work with the prospect of actually having to implement new Number Sequences. However, once I pulled together the precise steps from various books, online documents, and flat out trial-and-error, the task turned out to not be quite as bad as I had originally left the class thinking. I was however dumbfounded as to why no single source (that I was able to find) outlined the exact steps from creating a new Number Sequence to putting it to use in one clean tutorial.


NOTE: Please bear with me in this tutorial; I am going to break naming convention best practices, so that my example is clearer.


The first thing you need to do is determine which module you want/need to create the new number sequence for. For this example, I am going create a new number sequence for the “Accounts Receivable” module. Then take the following steps:


Creating a new Number Sequence for the Accounts Receivable Module


1. Create your Extended Data Type that will be used in your table as your Number Sequence field. For this example we will create a string based Extended Data Typecalled edt_ComplaintId (with a Label property = “Complaint Report”)


2. Next, since we are using the “Accounts Receivable” module, we must modify the proper NumberSeqReference_XXXX Class. Accounts Receivable, actually maps to Customer (or Cust), so we need to make a modification to theNumberSeqReference_Customer class.
We are only concerned with the loadModule() method, and will simply need to add the following code to it:


numRef.dataTypeId = typeId2ExtendedTypeId(typeid(edt_ComplaintId)); numRef.referenceHelp = "Unique key for the Complaint Report"; numRef.wizardContinuous = true;
numRef.wizardManual = NoYes::No;
numRef.wizardAllowChangeDown = NoYes::No;
numRef.wizardAllowChangeUp = NoYes::No;
numRef.wizardHighest = 999999;
this.create(numRef);
3. After compiling and saving your changes, the next step is to use the Number Sequence Wizard to set it up. Click Basic > Setup > Number sequences >Number sequences to open the form where you can manage your Number Sequences.


4. Click on the “Wizardbutton, which then should initialize the wizard.


NOTE: If you receive an error at this point telling you “The application already has the required number sequences”, this means that you either did not add the definition to an established NumberSeqReference_XXXX Class' (in this example, the NumberSeqReference_Customer Class)loadModule() method, or you have already run the Wizard and set it up.
5. Once you arrive on the Wizard Welcome screen, click the Next > button.


6. On the following screen, verify that the Module is “Accounts receivable” and theReference is “Complaint Report” (which is the label of our Extended Data Type). TheNumber Sequence code is just an auto-generated value that you can leave as is (Remember this code, so that you can find it in your list of Number Sequences, because you are going to want to make a couple changes to it). Once you have verified the above, click the Next > button.


7. The next screen just gives you an overview, click the Finish button.


8. You should then see your Number Sequence in the list.


9. You will probably want to change the Format to something such as “CR-######”


10. At this point, your Number Sequence is ready for use!




Using the Number Sequence


1. Create a Table and name it tbl_Complaint.


2. Add the edt_ComplaintId Extended Data Type as one of the fields (drag and drop the Extended Data Type directly into the fields of the Table) and name this field ComplaintId.


3. Add the following Method to the tbl_Complaint:


static client server NumberSequenceReference numRefComplaintId()
{
return NumberSeqReference::findReference(typeId2ExtendedTypeId(typeid(edt_ComplaintId)));
}
4. Create a Form called frm_Complaint.


5. Add the tbl_Complaint table to the form’s Data Sources, and name the Data Source ds_Complaint.


6. Add the following Methods to the Form:


public class FormRun extends ObjectRun
{
NumberSeqFormHandler numberSeqFormHandler;
}


NumberSeqFormHandler numberSeqFormHandler()
{
if (!numberSeqFormHandler)
{
numberSeqFormHandler = NumberSeqFormHandler::newForm(Tmt_CallReport::numRefComplaintId().NumberSequence, element, ds_Complaint.dataSource(), fieldnum(tbl_Complaint, ComplaintId));
}
return numberSeqFormHandler;
}
7. Add the following Methods to the ds_Complaint Data Source:


public void create(boolean _append = false)
{
element.numberSeqFormHandler().formMethodDataSourceCreatePre();
super(_append);
element.numberSeqFormHandler().formMethodDataSourceCreate();
}


public void delete()
{
element.numberSeqFormHandler().formMethodDataSourceDelete();
super();
}


public void write()
{
super();
element.numberSeqFormHandler().formMethodDataSourceWrite();
}
8. Your Number Sequence should now function!