Standard Form data source methods in D365 F&O - X++ Code
Table of Content:
form data source methods in D365 F&0 - X++ Code
The following sections explain the standard form data source methods in the finance and operations apps development platform.
init()
The init()
method is a standard method of a form data source, which is called when a form is opened after the implementation of the form init()
method. This method helps create a data source query based on the data source properties.
Example
You have a custom form with a table named TrainingMaster. The table has an enum type field named TrainingType that has two values: Online and Classroom. The form should be opened with those records, where TrainingType is Online.
The code pattern should be as follows:
[DataSource] class TrainingMaster { public void init() { super(); this.query().dataSourceName("TrainingMaster") .addRange(fieldNum(TrainingMaster,TrainingType)) .value(enum2Str(TrainingType::Online)); } }
active()
The active()
method is a standard method of a form data source that's called when a user navigates to a new record.
Example
You have a custom form with a table named TrainingMaster. The table has a field named Venue and an enum type field named TrainingType, which has two values of Online and Classroom. While you're navigating through the records, the Venue field will be uneditable if TrainingType is Online. Otherwise, the Venue field will be editable.
The code pattern should be as follows:
[DataSource] class TrainingMaster { public int active() { int ret; ret = super(); TrainingMaster_ds.object(fieldNum(TrainingMaster, Venue)).allowEdit(TrainingMaster.TrainingType == TrainingType::Classroom); return ret; } }
validateWrite()
The validateWrite()
method is a standard method of a form data source, which is called when a user attempts to save a record to determine whether data is valid and ready to be written.
Example
You have a custom form with a table named TrainingMaster. The table has a date field named TrainingDate. No backdated training data should be allowed.
The following code pattern can be a way to achieve this result:
[DataSource] class TrainingMaster { public boolean validateWrite() { boolean ret; ret = super() && (TrainingMaster.TrainingDate >= today()); return ret; } }
Note
You don’t want to let the validateWrite()
method fail without a message regarding why. A best practice would be to throw an error with a specific message rather than using the Boolean return of the if
statement.
validateDelete()
The validateDelete()
method is a standard method of a form data source, which is called when a user attempts to delete a record, to confirm the deletion.
Example
You have a custom form with a table named TrainingMaster. The table has a date field named TrainingDate. No backdated training data should be deleted.
The following code pattern can be a way to achieve this result:
[DataSource] class TrainingMaster { public boolean validateDelete() { boolean ret; ret = super() && (TrainingMaster.TrainingDate >= today()); return ret; } }
Note
You should throw an error with a message rather than let the validateDelete()
method silently fail.
executeQuery()
The executeQuery()
method is a standard method of a form data source that runs the data source query and displays the retrieved records. You can apply a filter on the data source that uses a query by overriding executeQuery()
. The main difference between this method and the init()
method is that the init()
method runs once during the opening of the form, whereas the executeQuery()
method runs whenever the form is refreshed.
Example
You have a custom form with a table named TrainingMaster. The table has an enum type field named TrainingType that has two values: Online and Classroom. The data in the form should be filtered with the Trainingtype of Online only.
[DataSource] class TrainingMaster { public void executeQuery() { QueryBuildDataSource queryBuildDataSource; queryBuildDataSource = this.query().dataSourceTable(tablenum(TrainingMaster)); queryBuildDataSource.clearRanges(); queryBuildDataSource.addRange(fieldnum(TrainingMaster, TrainingType)).value(enum2Str(TrainingType::Online)); super(); } }
initValue()
The initValue()
method is a standard method of a form data source. While you're inserting a new record, initValue()
can help you initialize field values. This method is called when a new record is created.
Example
You have a custom form with a table named TrainingMaster. The table has a date field named TrainingDate. During the creation of a new record, the TrainingDate field should be initialized with today’s date.
The coding pattern can be as follows:
[DataSource] class TrainingMaster { public void initValue() { super(); TrainingMaster.TrainingDate = today(); } }
write()
The write()
method is a standard method of a form data source. This method is called when a record is modified and when you've attempted to save it by selecting the Save button or navigating out of the record.
Example
You have a custom form with a table named TrainingMaster. The table has a real field called Price that indicates the total price of the trainer. It also contains an integer field called NoofDays that indicates the duration of the training. Additionally, the table has a field named TrainerID, which creates a foreign key with the TrainerTable. The TrainerTable has a real field called Rate, which captures the daily rate of the trainers.
After you've saved the record in the TrainingMaster table, the Price field of the TrainerMaster table should be updated with the multiplication of NoofDays and Rate of the TrainerTable.
The coding pattern is as follows:
[DataSource] class TrainingMaster { public void write() { TrainingMaster.Price = TrainerTable::find(TrainingMaster.TrainerID).Rate * TrainingMaster.NoofDays; super(); } }
delete()
The delete()
method is a standard method of a form data source. This method is called when a record is deleted.
Example
You have a custom form with a table named TrainingMaster. The table has a field named TrainerID, which has a foreign key relation with the TrainerTable. TrainerTable has a field named TrainingCount. The requirement is to reduce the TrainingCount number by one if a record is deleted from the TrainingMaster table.
The coding pattern can be as follows:
public void delete() { TrainerTable trainerTable; ttsbegin; trainerTable = TrainerTable::find(TrainingMaster.TrainerID, true); trainerTable.TrainingCount = trainerTable.TrainingCount - 1; trainerTable.update(); ttscommit; super(); }