Thursday, March 14, 2013

Changing Fields Default Values. Auto populating with what we want.

Hi Everyone,

This will be the last post for tonight :)

Ok, when we need to change default behavior for a control (a field), for instance in AR Invoice and Memo screen I would like a Subaccount and a Task to get defaulted from a Project, entered in the header area. Illustrated below:


To achieve it, I will use FieldDefaulting event for Subaccount and Project Task fields in the grid area.
So what I did is added an event for both fields, it had created me a code then I added necessary lines to it.

For the Subaccount:

protected override void ARTran_SubID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
{
  base.ARTran_SubID_FieldDefaulting(sender, e);
  var row = (ARTran)e.Row;
  var SubID = row.SubID;
  
  PMProject pm = PXSelect<PMProject,
             Where<PMProject.baseType, Equal<PMProject.ProjectBaseType>, And<PMProject.nonProject, Equal<False>, And<PMProject.contractID, Equal<Current<ARInvoice.projectID>>, And<PMProject.isTemplate, NotEqual<True>>>>>>.SelectSingleBound(this, new object [] { e.Row });
  
  if (pm != null)
    {
      //row.SubID = pm.DefaultSubID;  
      e.NewValue = pm.DefaultSubID;
    }
}


For the Task:

protected void ARTran_TaskID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
{
  
  var row = (ARTran)e.Row;
  var TaskID = row.TaskID;
  string DefTask = "00";

  PMProject pm = PXSelect<PMProject,
             Where<PMProject.baseType, Equal<PMProject.ProjectBaseType>, And<PMProject.nonProject, Equal<False>, And<PMProject.contractID, Equal<Current<ARInvoice.projectID>>, And<PMProject.isTemplate, NotEqual<True>>>>>>.SelectSingleBound(this, new object [] { e.Row });
  
  if (pm != null)
    { 
      e.NewValue = DefTask;
    }

}

Please take note, I am updating task with 00 value, which is default task in my case.
e.NewValue structure is the correct way of settling new Default value for the control.

As a result:


It is defaulted to what I want on a New Line click :)

All the best,

Sergey.

Wednesday, March 13, 2013

Removig Artificial Intelligence from GL Journal Entry Screen.

Hi Guys,

GL Journal Entry screen has one controversial feature - auto balancing of Debit to Credit for the line entered in the grid.

Its like when you enter Debit 100$ then you move to the next line, system auto defaults Credit to be 100$. Well its nice to have but...would be great to be able to switch it off...when needed.

So after some browsing throught the code, just realised, the calculation is hidden into a Method PopulateSubDescr

So, if you do not like DR to be == to CR :) for each line, then just override that method in your customization code with this statment. Well what I did - just removed AI piece of code. So just leave this:


protected override void PopulateSubDescr(PXCache sender, GLTran Row, bool ExternalCall)
        {
            GLTran prevTran = null;
            foreach (GLTran tran in GLTranModuleBatNbr.Select())
            {
                if (Row == tran)
                {
                    break;
                }
                prevTran = tran;
            }
            if (prevTran != null)
            {
                if (prevTran.SubID != null)
                {
                    Sub sub = (Sub)PXSelectorAttribute.Select<GLTran.subID>(sender, prevTran);
                    if (sub != null)
                    {
                        sender.SetValueExt<GLTran.subID>(Row, sub.SubCD);
                        PXUIFieldAttribute.SetError<GLTran.subID>(sender, Row, null);
                    }
                }
                Row.TranDesc = prevTran.TranDesc;
                Row.RefNbr = prevTran.RefNbr;
            }
            else
            {
                Row.TranDesc = BatchModule.Current.Description;
            }
            Account account = (Account)PXSelectorAttribute.Select<GLTran.accountID>(sender, Row);
            if (account != null && account.CashSubID != null)
            {
                Row.SubID = account.CashSubID;
            }
            if (account != null && (bool)account.NoSubDetail && glsetup.Current.DefaultSubID != null)
            {
                Row.SubID = glsetup.Current.DefaultSubID;
            }
            
        }

Have a nice Day!
Sergey. :)