Showing posts with label Class Extension. Show all posts
Showing posts with label Class Extension. Show all posts

Form Data Source Field Method Override D365FO


Form Data Source Field Method Override D365FO



In this demo, I will show to create an extension class of form data source Field class and override method through COC.

Here is the annotation code to create COC



[ExtensionOf(formDataFieldStr(FormName, DataSourceName,DataFieldName))]


In this demo, we are using the HcmWorker form and creating Extension of DirPerson Data source field Personal Title..

[ExtensionOf(formDataFieldStr(HcmWorker, DirPerson,PersonalTitle))]

In actual code, you can find the method in the Hcmworker form 


 [DataField]
        class PersonalTitle
        {
            public Common lookupReference(FormReferenceControl _formReferenceControl)
            {
                return DirUtility::buildAffixReferenceControlLookup(_formReferenceControl, DirNameAffixType::PersonalPrefix);
            }

        }


Here is the code through which we create extensions.

[ExtensionOf(formDataFieldStr(HcmWorker, DirPerson,PersonalTitle))]
final class Demo_Extension
{
}


Here is the code through this we override the method.


[ExtensionOf(formDataFieldStr(HcmWorker, DirPerson,PersonalTitle))]
final class Demo_Extension
{
    public Common lookupReference(FormReferenceControl _formReferenceControl)
    {
        next lookupReference(_formReferenceControl);
        return DirUtility::buildAffixReferenceControlLookup(_formReferenceControl, DirNameAffixType::PersonalPrefix);
    }

}

Now perform build in sync on your module. enjoy


Form Data Source Method override COC D365FO


Form Data Source Method override COC D365FO


Here is the sample how can you override the form data-source event.

In this demo we have created the extension of HcmWorker form data-source (HcmWorker).


Here is the original method  of HcmWorker data-source 

    public boolean validateWrite()
        {
            boolean ret;

            ret = super();

            if (ret)
            {
                partyForm.validateWrite();
            }

            return ret;
        }

Here is the  code to create form data-source extension class 

[ExtensionOf(formdatasourcestr(HcmWorker, HcmWorker))]
final class Demo_Extension
{

}


Here is the code to override the data-source method through COC.

[ExtensionOf(formdatasourcestr(HcmWorker, HcmWorker))]
final class Demo_Extension
{
    public boolean validateWrite()
    {
        boolean ret;

        ret  =  next validateWrite();

        if (ret)
        {
                    
           /// do your code here
        }

        return ret;
    }

}


In last perform Build & Sync.

Important 
Feature available from Build 8.1 PU-20


Default parameters can be wrapped In extension classes COC


Default parameters can be wrapped In extension classes




In Build-8.1 you can override the default parameter by using class extension COC.


Code reference 

There is the method in HCMWORKER table isEmployee in which you can see the default value set with _validFrom & _validto.


  public HcmIsEmployee isEmployee(
        utcdatetime   _validFrom = DateTimeUtil::utcNow(),
        utcdatetime   _validTo = _validFrom
    )
    {
        HcmIsEmployee   hcmIsEmployee = NoYes::No;
        HcmEmployment   hcmEmployment;
        unchecked(Uncheck::XDS)
        {
            if (prmisDefault(_validFrom) && prmisDefault(_validTo))
            {
                select firstonly RecId from hcmEmployment
                where hcmEmployment.Worker == this.RecId
                &&    hcmEmployment.EmploymentType == HcmEmploymentType::Employee;
            }
            else if (_validFrom == _validTo)
            {
                select firstonly ValidTimeState(_validFrom) RecId from hcmEmployment
                where hcmEmployment.Worker == this.RecId
                &&    hcmEmployment.EmploymentType == HcmEmploymentType::Employee;
            }
            else
            {
                select firstonly ValidTimeState(_validFrom, _validTo) RecId from hcmEmployment
                where hcmEmployment.Worker == this.RecId
                &&    hcmEmployment.EmploymentType == HcmEmploymentType::Employee;
            }

            if (hcmEmployment.RecId != 0)
            {
                hcmIsEmployee = NoYes::Yes;
            }

            return hcmIsEmployee;
        }
    }


So we created extension class of Hcmworker with name of Demo_Extension and override the method using COC.



[ExtensionOf(tableStr(HcmWorker))]
final class Demo_Extension
{

    public HcmIsEmployee isEmployee(
        utcdatetime   _validFrom,
        utcdatetime   _validTo 
    )
    {
        HcmIsEmployee   hcmIsEmployee = NoYes::No;
        HcmEmployment   hcmEmployment;
      
        next isEmployee(_validFrom,_validTo);

               unchecked(Uncheck::XDS)
        {
           if (_validFrom == _validTo)
            {
                select firstonly ValidTimeState(_validFrom) RecId from hcmEmployment
                where hcmEmployment.Worker == this.RecId
                &&    hcmEmployment.EmploymentType == HcmEmploymentType::Employee;
            }
            else
            {
                select firstonly ValidTimeState(_validFrom, _validTo) RecId from hcmEmployment
                where hcmEmployment.Worker == this.RecId
                &&    hcmEmployment.EmploymentType == HcmEmploymentType::Employee;
            }

            if (hcmEmployment.RecId != 0)
            {
                hcmIsEmployee = NoYes::Yes;
            }

            return hcmIsEmployee;
        }
    }

}

Now perform build & Sync on your module..

Class Extension D365FO



Class Extension D365FO


In this blog I will show you class extension in D365 and add new method in the existing class without any over-layer..


There are two way to create Extension of the existing class...


  1. Static Class 
  2. ExtensionOf



Static class 

Create a new public static class use the name pattern ClassName + _Extension

You can use the following code for reference....
public static class SLD_PaymFee_Extension
{
  
        public static void paymfeeFind(PaymFee _this)
        {
            /// You can write your code here

            _this.findFees(); //Use _this to call methods from the base class;
        }


}


ExtensionOf

Create new class add the annotation of ExtensionOf and enter the class name... In this type of extension add final keyword. 

[ExtensionOf(classStr(PaymFee))]
final class SLD_PaymFee_Extension
{
  
        public static void paymfeeFind(PaymFee _this)
        {
            /// You can write your code here

            _this.findFees(); //Use _this to call methods from the base class;
        }


}


For verification you can use the below code

class SLD_DemoClass
{

     
    public static void main(Args _args)
    {
        PaymFee obj =new PaymFee();
        obj.paymfeeFind();
    }

}


Private, Protected and Public attribute access in Class Extension


Private, Protected, and Public attribute access in Class Extension


lots of developers faced the issue on the daily basis during development to access private, protected, and public methods.

As everyone knows that public attribute is available to access with the class instance.
but protected and private attributes are a little difficult to access.

In this demo, we show how to access all 3 access modifiers using the class extension.


Step-1 Create a class SLD_DemoClass.
Step-2 Declare 3 variables like below and one public method to set values in these variables.






Step-3 Now create an Extension of this class with SLD_DemoClass_Extension.
Step-4 Create a getter setter to access the variable.



for public and protected methods it's easy to access just to write simple getter setter method and can access these public methods using class instance.

Step-5 To Access the private attributes in-class extension, We need to use the C# reflection library.

Step-6 Now write the below code to access the private attribute on run-time.





following is the screenshot of the complete code of the extension class.




Step-7 Now create a run-able class and action menu item to execute this code.





Step-8 perform build & Sync and login on D365FO for verification.



You can check in the screenshot all 3  protected, private, and public attributes values in the info message.


Please feel free to contact me if you are facing any issues during the implementation of this blog.




Transfer Order Shipment Validation D365FO


Transfer Order Shipment Validation in Extension COC


Yesterday, I received the requirement to put some custom validation during transfer order shipment.

As everyone knows that in D365FO over-layering is not allowed anymore. So I have performed this task in extension.

In extension, You have two options to perform this task.


1st-Way

Create the form extension and replace the shipment menu item with my custom menu item and after performing my successful custom validation call original menu items.

2nd-Way

Create extension class of  InventTransferUpdShip and override the check method using COC pattern and perform our custom validation.

I decided to go with COC and the following are the code snippet I have to write 



[ExtensionOf(classstr(InventTransferUpdShip))]
final class SLD_InventTransferUpdShip_Extension
{

    boolean validate()
    {
        
        boolean ok = next validate();

        
             ok=ok && this.validateTransferOrderShip(ok);
        
        return ok;
    }


  public boolean validateTransferOrderShip(boolean flag)

    {
        // write your validation here... 

     return flag;
  }

}

Product Receipt validation in Extension using COC D365FO

Product Receipt validation in Extension using COC D365FO



Yesterday, I received the requirement to put some custom validation while performing product receipts.

As everyone knows that in D365FO over-layering is not allowed anymore. So I have to perform this task in extension.

In extension, I have two ways to perform this task.

Create the form extension and replace the product receipt menu item with my custom menu item and after performing my successful custom validation call original menu items.

Create extension class of  PurchPackingSlipJournalCreate and override the check method using COC pattern and perform our custom validation.

So I go with COC and the following are the code snippet I have to write 



    [ExtensionOf(classstr(PurchPackingSlipJournalCreate))]

    final class SLD_PurchPackingSlipJournalCreate_Extension
    {
        protected boolean check()
        {
            boolean flag=    next check();
            flag= flag && validatePackingSlip(flag);

            return flag;
        }



        public boolean validatePackingSlip(boolean flag)
        {

            if(flag && this.purchParmUpdate.DocumentStatus==DocumentStatus::PackingSlip)
            {
                // write your validation here... you can access buffer of these table purchParmUpdate,purchParmTable
                flag= checkFailed(strFmt("Custom validation causes the product receipt generation failed, %1, %2",this.purchParmTable.ParmId, this.purchParmTable.DocumentDate));

            }
            return flag;
        }


    }





Virtual Fields Vs Computed Fields

  Virtual Field: A virtual field in D365FO is a field that doesn't have a direct representation in the database. It's a field that y...