Showing posts with label method sequence in d365fo. Show all posts
Showing posts with label method sequence in d365fo. Show all posts

Tuesday, January 4, 2022

Table Methods in D365 FO

                  Table Methods in D365 FO


Whenever records are changed ,  inserted or deleted from a table , various default method are executed .

Create A table (CustomerTable ) with some fields :

  • CustId
  • CustomerName
  • Description
  • FromDate
  • ToDate
  • Qty
  • Status 
  • UnitPrice
  • TotalAmount

  1.    initvalue() : * If we create a new record from the table browser or a form , the table method initvalue()                         is   executed.

                    *  It is also used to set a default value for fields.


Example: 

 let's override initvalue for CustomerTable(tablename) and set default value for CustId(fieldname)


                public void initvalue()

            {

                super();

                this.CustId = "10";

            }


after adding this method , open table CustomerTable through Open table Browser and press Ctrl+N to create new record, field CustId  will have default value 10.


   2. modifiedField() : value of field is changed.

Example :  override modifiedField method for CustomerTable (tablename) and target to set Description  to null when CustId is modified 

            public void modifiedField(fieldId _fieldId)

            {

                    switch(_fieldId)

                {        

                    case fieldnum(CustomerTable, CustId):

                               this.Description = "";

                                break;

                            super(_fieldId);

                    }

                }

 modifiedField() receives field number of active field (as parameter) , switch statement is used to check which field is active.

if none of checked field are active super() calls is executed instead.



3. validateField():   * used for validation only and will return true and false.


Example :

 let's override for CustomerTable to verify the condition that CustomerName must be more than 3 characters.

 public boolean validateField (fieldId  _fieldIdToCheck)

{

boolean ret;

ret = super(_fieldIdToCheck);

if(ret)

  {

    switch(_fieldIdToCheck)

      {

      case fieldNum(CustomerTable, CustomerName):

            if(strlen(this.CustomerName)<=3)

            {

                ret = checkfailed("Customer Name should be more than 3 character");

             }

        }

       return ret;

    }


If we try to enter less than 3 character of CustomerName Field , AX will throw warning message.


4. validatewrite() :

 when a record is written to db, before the date change is committed in db.


Example :  

The ToDate always be larger than StartDate.


   public boolean validatewrite()

{

    boolean ret;

    ret = super;

    if (this.ToDate < this.FromDate)

        {

           ret = ret && checkfailed("ToDate should be greater")   ;

          }

        return ret;

 }

   

5. validateDelete():

       executed when record is deleted.


Example: 

 public boolean validateDelete()

{

    boolean ret;

     ret = super();

    info(this.Description);

    return ret;

}


6. find() :

All tables should have atleast one find method that select and return one record from table.

It uses select statement to retreive the data ,  but it requires parameter to search , if that exist  , it will be search otherwise , it skips.

It accepts one more parameter i.e. boolean update which is false by default , if true , record should be select for update.


Example :

 

static CustomerTable find(CustId _custId , boolean update = false)

{

    CustomerTable CustomerTable;

    CustomerTable.Selectforupdate(update);

            if(CustId)

                {

            select firstonly CustomerTable

            index hint PrimaryIdx

            where CustomerTable.CustId == CustId;

                }

            return CustomerTable;

}


7. Exists() 

As with find method , there should also exists method, 

same as find method , except that it just returns true if a record with unique index specified by input parameter is found.


Example :  

static boolean exist(CustId CustId)

{

  return CustId && (select RecId from CustomerTable index hint PrimaryIdx 

where CustomerTable.CustId == CustId).RecId ! = 0;

}


Enjoy 😊