Monday, 8 August 2016

Cool ways to use PXFormula

Hi All,

Acumatica platform continuously evolving and includes more and more new cool features to optimize your code and save time on development and testing.

Today i want to share with you some really cool features of PXFormula attribute and how to use it to significantly simplify your code.
Here I want to cover following parameters:
  • Validate - Validate<field>
  • Current - Current<TRecord.field>
  • Parent - Parent<TParent.field>
  • IsTableEmpty - IsTableEmpty<TRecord>
  • Selector - Selector<KeyField, ForeignOperand> 
Welcome in details if you need examples.

The Validate<field> formula will raise dependentField's FieldVerifying event each time the RelatedField is updated.

public class DacClass: PX.Data.IBqlTable
    public abstract class field : IBqlField { }
    public virtual int? Field { get; set; }

    public abstract class dependentField : IBqlField { } 
    public virtual int? DependentField { get; set; }   

Of course there you can do the same with graph/attribute events, but with formula it is much more stable and easier.

Current<TRecord.field> and Current2<TRecord.field> fetches the field value from the record stored in the Current property of the TRecord cache.

If the cache's Current property or the field itself contains null:
  • Current<> forces field defaulting and returns the default field value.
  • Current2<> returns null
        ARAdjust.adjgDocType, Equal<Current<ARPayment.docType>>,
        And<ARAdjust.adjgRefNbr, Equal<Current<ARPayment.refNbr>>>>,
protected virtual void ARAdjust_ClassIcon_CacheAttached(PXCache sender)

Parent<TParent.field> fetches the field value from the parent data record as defined by PXParentAttribute residing on the current DAC.

public class INTran : IBqlTable
            INRegister.docType, Equal<Current<INTran.docType>>,
    public virtual String RefNbr { ... }

    public virtual String OrigModule { ... }

IsTableEmpty<TRecord> returns true if the DB table corresponding to the specified DAC contains no records.

public class APRegister : IBqlTable
            IsTableEmpty<APSetupApproval>, Equal<True>>,
            APRegister.requestApproval, Equal<True>>,
    public virtual bool? DontApprove { get; set; }

Selector<KeyField, ForeignOperand> does following:
  1. Fetches a PXSelectorAttribute defined on the foreign key field (KeyField) of the current DAC. 
  2. Fetches the foreign data record currently referenced by the selector. 
  3. Using calculates and returns an expression on that data record as defined by ForeignOperand.
public class APVendorPrice : IBqlTable
    // Inventory attribute is an aggregate containing a PXSelectorAttribute
    // inside, which is also valid for Selector<>.
    [Inventory(DisplayName = "Inventory ID")]
    public virtual int? InventoryID

    public virtual string UOM { get; set; }

Big thanks to Acumatica Development team for this example.
Have a nice development!

1 comment:

Sol Sam said...

I was not aware that there are different ways to use PXFormula. Now that I know, I can share this new knowledge to my students taking up trust my paper review. Glad I came across your blog and learned something.