Skip to main content
Solved

PXDBIdentity field not updated in cache after Persist


Forum|alt.badge.img
  • Jr Varsity III
  • 23 replies

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()?

Best answer by mhaps

Finally I found the solution:

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

View original
Did this topic help you find an answer to your question?

6 replies

Naveen Boga
Captain II
Forum|alt.badge.img+19
  • Captain II
  • 3377 replies
  • April 28, 2021

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
}

 


 


Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • 23 replies
  • Answer
  • April 28, 2021

Finally I found the solution:

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


Gabriel Michaud
Captain II
Forum|alt.badge.img+10

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


Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • 23 replies
  • April 30, 2021

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


Gabriel Michaud
Captain II
Forum|alt.badge.img+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.


Forum|alt.badge.img
  • Author
  • Jr Varsity III
  • 23 replies
  • April 30, 2021

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings