Solved

PXDBIdentity field not updated in cache after Persist

  • 28 April 2021
  • 6 replies
  • 604 views

Userlevel 2
Badge

Hello,

I have a problem getting the value of auto-incremented field of a new inserted record.

In my DAC the Id field has [PXDBIdentity(IsKey = true)]. It is the only key field.

Here’s some code.

MyDac newDac = graph.MyDac.Insert(new MyDac()) ;

int? tempId = newDac.Id;

graph.MyDac.Cache.Persist(PXDBOperation.Insert);

int? newId = newDac.Id;

tempId is negativ as expected. But newId is alway 0, I’d expect the real value of the newly created entry like in DB. NewDac is still in Cache.Inserted and in DirtyCashes.

If I call graph.Actions.PressSave() instead of Cache.Persist() newDac.Id is updated and it moves to Cache.Cached. But I have entities in other views that should not be saved at this time.

I am sure, that with other Dac classes the Id is updated after callling Persist(). Does the system check relations to other entities and and therefore may not give back the new Id in my case?

How can I get the real Id of the newly inserted record without PressSave()?

icon

Best answer by mhaps 28 April 2021, 17:30

View original

6 replies

Userlevel 7
Badge +17

Hi, @mhaps Auto increament value will have -ve value until we call the graph.Actions.PressSave().

Identity is a DB operation, until we insert the new record into the table, it will NOT generate the actual identity value.

 Since you are not invoking the “graph.Actions.PressSave()”, till that time, records will be inserted into the caches but NOT to the database hence identity value will NOT be populated. 


You can use the Database Insert from the Graph file using the below syntax, in this case, the identity value will be incremented. Please find the example below.

Syntax
PXDatabase.Insert<EmployeeTable>(new PXDataFieldAssign<EmployeeTable.EmpName>(Base.Document.Current.UsrEmployee));

DAC

 

[Serializable]
[PXCacheName("Shipping Info")]
public class KNDummy : IBqlTable
{
#region EmpID
[PXDBIdentity(IsKey = true)]
[PXUIField(DisplayName = "Line Nbr")]
public virtual int? EmpID { get; set; }
public abstract class empID : PX.Data.BQL.BqlInt.Field<empID> { }
#endregion

#region EmpName
[PXDBString(15)]
[PXUIField(DisplayName = "EmpName")]
public virtual string EmpName { get; set; }
public abstract class empName : PX.Data.BQL.BqlString.Field<empName> { }
#endregion
}

 


 

Userlevel 2
Badge

Finally I found the solution:

Inlude the code into a PXTransactionScope and the auto increment value is updated correctly after calling Persist().

Userlevel 7
Badge +10

@mhaps just out of curiosity -- why do you need to save the other entities at a separate time?

Userlevel 2
Badge

@Gabriel Michaud I save them immeditately afterwards but they need the auto incremented Id value.

Userlevel 7
Badge +10

That’s all handled by Acumatica - you don’t have to do that yourself. If your detail/child tables have the PXDBDefault attribute linked to the parent identity field it will be automatically set before the system persists the rows.

Userlevel 2
Badge

You’re right, but in my case it’s not a usual parent/child relation. My child has three different parents.

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