Reputation: 1311
I have a custom DAC to save different Branch prices of an item.
When i try to save the inventory item, I'm getting "Specified cast is not valid" error:
I'm not sure whether one of the Branch attribute is at fault though.
Here is my DAC
using System;
using PX.Data;
using PX.Objects.IN;
using PX.Objects.GL;
namespace SalePriceByBranch
{
[Serializable]
public class INItemPriceByBranch: IBqlTable
{
[PXDBInt(IsKey = true)]
[PXUIField(DisplayName = "Inventory ID")]
[PXParent(typeof(Select<InventoryItem, Where<InventoryItem.inventoryID, Equal<Current<INItemPriceByBranch.inventoryID>>>>))]
[PXDBDefault(typeof(InventoryItem.inventoryID))]
public Int32? InventoryID { get; set; }
public class inventoryID : IBqlField{}
[PXDBInt(IsKey=true)]
[PXUIField(DisplayName = "Branch")]
[PXDefault(0)]
[Branch()]
public Int32? BranchID{ get; set; }
public class branchID : IBqlField{}
[PXDBDecimal(4)]
[PXUIField(DisplayName = "Unit Price")]
[PXDefault(TypeCode.Decimal,"0.0")]
public Decimal? UnitPrice { get; set; }
public class unitPrice : IBqlField{}
}
}
I reckon the error on BranchID is a red herring. Here is the error trace :
Any thoughts ? Thanks for the replies !
Upvotes: 0
Views: 1205
Reputation: 8278
The Branch attribute already defines its type as PXDBInt/PXInt (see below). When using built in attributes such as [Branch], [Inventory], [Customer]... I avoid redefining the type as it sometimes throws runtime errors.
[PXDBInt()]
[PXInt]
[PXUIField(DisplayName = "Branch", FieldClass = _FieldClass)]
[PXRestrictor(typeof(Where<Branch.active, Equal<True>>), Messages.BranchInactive)]
public class BranchAttribute : AcctSubAttribute, IPXFieldSelectingSubscriber
{
[...]
}
Instead of using PXDBInt, you can declare BranchID as a key field in the Branch attribute:
[Branch(null, IsKey = true)]
Another possibility is that value 0 in [PXDefault(0)] doesn't resolve to BranchID 0 in the system. This should be easy to lookup in the database, for SalesDemo DB branches start at 16. Branch attribute has more logic in it than a standard custom field, it could have validation logic that prevents invalid Branch number.
If you need to initialise Branch attribute consider doing it in Branch attribute instead of PXDefault:
[Branch(typeof(APRegister.branchID))]
Branch attribute also contains a PXUIField with 'Branch' display name so you don't have to redefine it if you want the field to be labeled 'Branch'.
Last thing, DAC field backing class are declared as abstract by convention in Acumatica.
All these changes applied to your DAC field:
[Branch(null, IsKey=true)]
public abstract class branchID : IBqlField { }
public Int32? BranchID { get; set; }
Upvotes: 1