Table Main Methods
D365 F&O provides various methods that allow developers to interact with these tables effectively. These methods help in performing CRUD operations (Create, Read, Update, Delete) and other data-related tasks. Here's an explanation of some of the commonly used table methods.
How to create Methods in table.
Open your table, go to Methods, right click on it, you can see the Override option, select override option then you can see the all standerd methods.
initValue(): This method is used to initialize the fields of a table with default values. It is called when a new record is created.
EX: This method will fill the StarteDate Field with Today's Date & Time.
public void initValue()
{
super();
this.StartDate = DateTimeUtil::utcNow();
}
insert(): This method is used to insert a new record into the table. Mostly we use this method in RunnableClass(JOB).
EX: It will insert the Value (India) in Country Field at that time only.
FMAddressTable fmAddressTable:
ttsBegin;
fmAddressTable.Country = 'India';
fmAddressTable.insert();
ttsCommit;
update(): This method is used to update an existing record in the table. Mostly we use this method in RunnableClass(JOB).
EX: it update the Country field with UK, where city = London
FMAddressTable fmAddressTable;
ttsBegin;
select forUpdate fmAddressTable
where fmAddressTable.Country == 'UK';
fmAddressTable.City = 'London';
fmAddressTable.Update();
ttsCommit;
delete(): This method is used to delete a record from the table. Mostly we use this method in RunnableClass(JOB).
EX: It will delete the Records where Country == 'UK'.
ttsBegin;
FMAddressTable fmAddressTable;
while select forUpdate fmAddressTable
where fmAddressTable.Country == 'UK'
{
fmAddressTable.delete();
}
ttsCommit;
validateField(): This method is called when a field in the table is inserting or modifing and allows you to perform validation logic on the field. Used to validate the contents of a field before saving the record.
EX: when you select the EndDate it must be above from the StartDate, Otherwise it won't allow to fill the EndDate.
public boolean validateField(FieldId _fieldIdToCheck)
{
boolean ret;
ret = super(_fieldIdToCheck);
if (ret)
{
switch (_fieldIdToCheck)
{
case fieldnum(FMRental, EndDate):
if(this.EndDate < this.StartDate)
{
ret = checkFailed("End date must be after start date");
}
break;
}
}
}
validateWrite(): This method is called before a record is inserted, updated, or deleted. It allows developers to implement additional validation logic before the write operation is performed.
EX:this method shows the warning when you click the save, if the Address in not empty it will save the data or else it will throw the warning.
public boolean validateWrite()
{
boolean ret;
if (FMAddressTable.Address != "")
ret = super();
else
warning(" Please fill the address value");
return ret;
}
{
boolean ret;
if (FMAddressTable.Address != "")
ret = super();
else
warning(" Please fill the address value");
return ret;
}
find(): This method is used to search for records based on specified criteria. It returns a record buffer that can be used to access the data of the found record.
EX: To retrieve a record from the database. Once you have the record, you can read, update, or delete it. we use the PrimaryKey in Find method.
public static FMRental find(FMRentalId _rentalId, boolean _forupdate = false)
{
FMRental result;
result.selectForUpdate(_forupdate);
select firstonly result
where result.RentalId == _rentalId;
return result;
}
Exist: Checks if a record exists in the table based on specified criteria.
EX: with this we can check the perticuler Field availble in the table or not.
static boolean exist(RentalId _rentalId)
{
return _rentalId && ( select firstonly RecId from FMRental
index hint RentalIdx
where FMRental.RentalId == _rentalId).RecId !=0;
ModifiedField: It is used to check if a specific field on a data source has been modified by the user or through code during the current session.
EX:when you select the itemId in IBD_invent it will fill the ItemNameDisplay field by fetch the data from InventTable.
public void modifiedField(FieldId _fieldId)
{
InventTable inventTable;
super(_fieldId);
switch (_fieldId)
{
case fieldNum(IBD_Invent,itemid):
this.ItemNameDisplay = inventTable::find(this.itemid).NameAlias;
break;
}
}
No comments:
Post a Comment