Solved

Custom int field throws "Specified cast is not valid"

  • 11 August 2023
  • 5 replies
  • 739 views

Userlevel 4
Badge

I have a custom field that is throwing the following error:

Error: An error occurred during processing of the field PD Inches/SQIN: Specified cast is not valid..


I have the following 

  #region UsrPDinches
[PXDBInt]
[PXUIField(DisplayName="PD Inches/SQIN")]

public int? UsrPDinches { get; set; }
public abstract class usrPDinches : PX.Data.BQL.BqlInt.Field<usrPDinches> { }
#endregion

What could be causing this issue?

Another questtion,

This field is on a production order. I can’t find how to disable the field when the production order status is completed. How do I do that?

icon

Best answer by davidnavasardyan09 12 August 2023, 16:47

View original

5 replies

Userlevel 7
Badge +11

Hi @rcreasy 

  1. Double-check your code to ensure that you are not assigning field values to other data types (e.g., string or decimal).

  2. This can be accomplished in the graph extension using the Row Selected event.

eg:
 PXUIFieldAttribute.SetVisible<DACName.UsrField>(cache, e.Row, Base.Document.Current.Status!=SOOrderStatus.Completed);

Userlevel 7
Badge +10

Hi @rcreasy ,

 

The error might be occurring due to a mismatch between the data type expected by the field and the data being stored in it. 

 

Regards,

Sweta

 

Badge +11

I always prefer the DAC attributes over events. You can add this to your DAC definition:

[PXUIEnabled(typeof(Where<SOOrder.status.IsNotEqual<SOOrderStatus.completed>>))]

 

Userlevel 4
Badge

My cast error was because the DB table had the incorrect type on the column.

Here is my theory - 
I created the field with the wrong type, then went back and changed it.
I think the DB was not updated on my change. So, the DB retained the first, and incorrect, type for the field.

Userlevel 5
Badge +1

Hi @rcreasy 

The error "Specified cast is not valid" is typically thrown when the application attempts to read a value from the database that doesn't match the data type specified in the DAC field. This often happens due to changes in the field's data type after data has been entered.

Here's what you can do to troubleshoot this:

  1. Check Database Value: Directly check the value stored in the database for the UsrPDinches field. Ensure that all values in the column are integers. If there's a non-integer value (like a decimal or string), that would be the root cause.

  2. History: If you changed the data type of this custom field after it was initially created (for instance, if it was originally a PXDBDecimal and then you changed it to PXDBInt), old data might still be stored in the database in the original format, causing a casting error when trying to retrieve it. You'll need to either clear out or convert old data to the correct type.

  3. Database Schema: Also, ensure that the database schema correctly reflects this field as an integer column.

 

Disabling Field Based on Production Order Status

To disable the field based on the production order status, you'll need to use a RowSelected event on the production order DAC to set the field's Enabled property to false when the order is completed.

Here's an example of how you can achieve this:

Assuming your production order DAC is named ProductionOrder:

 

protected virtual void _(Events.RowSelected<ProductionOrder> e)
{
if (e.Row == null)
return;

ProductionOrder order = e.Row;

// Assuming 'Completed' status is represented as "C"
if (order.Status == "C")
{
PXUIFieldAttribute.SetEnabled<ProductionOrder.usrPDinches>(e.Cache, order, false);
}
}

 

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved