Skip to main content
Solved

SOOrder extension: not able to save updates of custom columns to the database

  • December 23, 2024
  • 19 replies
  • 176 views

Forum|alt.badge.img+2

I added some custom columns to SOOrder in a customization project (under Database scripts) and they’ve been created in my database correctly in the SOOrder table.  I’m also able to view/modify these columns in the PXCachExtension with my C# code.  However, when I run a SQL query in SOOrder on these new columns, they always return as NULL.

Question: How do I save updates to these new fields in the database?

I’ve tried the following without luck:

                    Base.Document.Update(Base.Document.Current);
                    Base.Document.Cache.Update(Base.Document.Current);
 

Best answer by darylbowman

Are you sure your field is defined with PXDBFieldType instead of PXFieldType?

19 replies

Forum|alt.badge.img+7
  • Captain II
  • December 23, 2024

Can you share the code where you’re declaring and then calling GetExtension? And then the code you’re using to assign the data values to your custom fields?


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • December 23, 2024

Can you share the code where you’re declaring and then calling GetExtension? And then the code you’re using to assign the data values to your custom fields?

            SOOrder order = Base.Document.Current;
            SOOrderExt ext = order.GetExtension<SOOrderExt>();

            if (true)  {

                    ext.UsrFraudId = 5588924208;  //new field
                    Base.Document.Update(Base.Document.Current);
                    Base.Document.Cache.Update(Base.Document.Current);
                    Base.Document.Update(order);
            }


Forum|alt.badge.img+7
  • Captain II
  • December 23, 2024

So when you’re updating one way to think about it is this (because this helped me later on to figure out how to update other records):

SOOrder order = Base.Document.Current;

Now I have an object called ‘order’ that contains the contents of a record.

SOOrderExt ext = order.GetExtension<SOOrderExt>();

Now I have another object that allows me to access the extension’s fields/logic/etc.

If you examine ‘order’ and dig into the methods and properties, you’ll actually see the ext fields. It’s that ‘order’ doesn’t know how to process those fields, which is why you access them through your ext object.

When you call .Update, you need to pass it an object that contains the DAC that you’re wanting to update. By that I mean when you call Base.Document.Update, you want to pass it an object with the structure of SOOrder and that object should have the updated values.

When you call ext.UsrFraudId = 232423434 you’re updating the extension fields and you’re also updating the ‘order’ object.

When you call Base.Document.Update you want to pass it the updated ‘order’ object. The update routine will look in that cache (Base) for data with the same DAC (SOOrder) with the key field values found in ‘order’. If it finds it then it will apply the updates from ‘order’ to the record. And because the ‘order’ object does know of the fields in the extension, it will update the database with those values.

Finally, in order to save or persist the data to the database, you have to call the Save method of the graph.

Base.Save.Press()

That’s a long way to say that you’re missing the call to .Save but it also tells you that you don’t need the two lines after your field assignment - they are both extraneous:

Base.Document.Update(Base.Document.Current);
Base.Document.Cache.Update(Base.Document.Current);


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • December 23, 2024

So when you’re updating one way to think about it is this (because this helped me later on to figure out how to update other records):

SOOrder order = Base.Document.Current;

Now I have an object called ‘order’ that contains the contents of a record.

SOOrderExt ext = order.GetExtension<SOOrderExt>();

Now I have another object that allows me to access the extension’s fields/logic/etc.

If you examine ‘order’ and dig into the methods and properties, you’ll actually see the ext fields. It’s that ‘order’ doesn’t know how to process those fields, which is why you access them through your ext object.

When you call .Update, you need to pass it an object that contains the DAC that you’re wanting to update. By that I mean when you call Base.Document.Update, you want to pass it an object with the structure of SOOrder and that object should have the updated values.

When you call ext.UsrFraudId = 232423434 you’re updating the extension fields and you’re also updating the ‘order’ object.

When you call Base.Document.Update you want to pass it the updated ‘order’ object. The update routine will look in that cache (Base) for data with the same DAC (SOOrder) with the key field values found in ‘order’. If it finds it then it will apply the updates from ‘order’ to the record. And because the ‘order’ object does know of the fields in the extension, it will update the database with those values.

Finally, in order to save or persist the data to the database, you have to call the Save method of the graph.

Base.Save.Press()

That’s a long way to say that you’re missing the call to .Save but it also tells you that you don’t need the two lines after your field assignment - they are both extraneous:

Base.Document.Update(Base.Document.Current);
Base.Document.Cache.Update(Base.Document.Current);

Thanks but I should have told you that my code was in a PXOverride of the PostPersist() function so calling Base.Save.Press() in that function creates an infinite loop (and doesn’t save any changes to the SOOrder table either).  Is there a way to save the extension fields without calling Base.Save.Press()?


darylbowman
Captain II
Forum|alt.badge.img+15
  • December 23, 2024

Where is PostPersist() found?


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • December 23, 2024

Where is PostPersist() found?

PXGraphExtension<SOOrderEntry>


darylbowman
Captain II
Forum|alt.badge.img+15
  • December 23, 2024

In what file and what version of Acumatica?


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • December 23, 2024

In what file and what version of Acumatica?

The Pre/Post Persist functions are new for version 24R1 of Acumatica. When save is pressed it calls PrePersist(), and if it returns true it will do the main persist functions, then after those transactions are commited, it calls PostPersist(). Calling an addition save in that function is starting the process all over again.


Forum|alt.badge.img+7
  • Captain II
  • December 23, 2024

Is there a way to save the extension fields without calling Base.Save.Press()?

If you really want to update the record with just that one field you can make a call using PXDatabase. That essentially allows you to write a SQL statement.

Can you not determine that field value at the time that the order is being saved? Or is the state of the order preventing you from updating the header? Maybe understanding when you need to update this field will help to determine what event that should happen in.

There’s very little documentation that I’ve found around PostPersist other than the 24R1 Release Notes. I see it only used in one place in the source code.


darylbowman
Captain II
Forum|alt.badge.img+15
  • December 23, 2024

If the cache is being updated and save is being called after, you shouldn’t need to call save yourself.


darylbowman
Captain II
Forum|alt.badge.img+15
  • Answer
  • December 23, 2024

Are you sure your field is defined with PXDBFieldType instead of PXFieldType?


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • December 23, 2024

If the cache is being updated and save is being called after, you shouldn’t need to call save yourself.

Correct, it’s not needed.  So now I’ve removed the call to Base.Save.Press() that I just added.  The problem is that the new custom columns aren’t being updated in the SQL database.


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • December 23, 2024

Are you sure your field is defined with PXDBFieldType instead of PXFieldType?

Neither.  It’s defined as a PXUIField.  Maybe that’s the problem?


Forum|alt.badge.img+7
  • Captain II
  • December 23, 2024

Can you post your extension DAC that contains your custom field?


darylbowman
Captain II
Forum|alt.badge.img+15
  • December 23, 2024

It seems to me that PostPersist would be used to make changes to other graphs, not the current graph, since, as you're discovering, there's no way to save. In this case, I think you'd override either PrePersist or Persist and make changes before the save has happened.


darylbowman
Captain II
Forum|alt.badge.img+15
  • December 24, 2024

I’m just a bit curious what the resolution here was. By the marked solution, I’m assuming you weren’t using persisted field types. Do fields get persisted in PostPersist() or did you need to move it?


Forum|alt.badge.img+2
  • Author
  • Semi-Pro I
  • December 24, 2024

I’m just a bit curious what the resolution here was. By the marked solution, I’m assuming you weren’t using persisted field types. Do fields get persisted in PostPersist() or did you need to move it?

Yes they do.  The problem is that PostPersist is called even when the Save button isn’t used.  For instance, if the order gets put on hold (button is pressed), PostPersist is called and then gets called again when the Save button is pressed.  I only want to call the functionality that sets the custom column when the Save button is pressed.  Is there a way to check this in PostPersist?


darylbowman
Captain II
Forum|alt.badge.img+15
  • December 24, 2024

Some workflow actions call persist before they fire.


darylbowman
Captain II
Forum|alt.badge.img+15
  • December 24, 2024

Check out this post