Skip to main content
Solved

Add a DB row upon loading a screen


My background is C#, T-SQL, and ORMs like EF.  I’m continually having issues wrapping my head around the Acumatica paradigm.  Something as simple as conditionally adding a row to the DB with the user’s ID (if one isn’t present) to a custom master table (UserPriceComparison) escapes me.
 

And then, I need to re-save the row whenever the user changes the form - and also refresh the details view.

The reason for all of the above is that the complex pivot and aggregation logic I need to create is not performant anywhere except inside of a T-SQL view, which uses the values in the master table to do thing that I can’t explain here.

While my instinct is to ask the question, I can predict that the first thing that will happen is that someone will ask to see my code.  Pretend I don’t have any.  This should be, what, 10 lines of code at most?

 private void CreateFilterWhenMissing()  // Call on load?  Or later.
 {
     var u = PXAccess.GetUserID();
     // Get the existing row for the current user
     EmpPriceComparisonUser row = SelectFrom<EmpPriceComparisonUser>.Where<EmpPriceComparisonUser.userId.IsEqual<@P.AsGuid>>.View.Select(this, u);
     if (row != null)
     {
         return;
     }
     row = new EmpPriceComparisonUser();
     row.UserId = u;
     Caches[typeof(EmpPriceComparisonUser)].Insert(row);
 }

Best answer by Django

I think you’re just missing one line from your pseudo code. The call to .Insert or .Update (and even .Delete) doesn’t to anything to the database. It’s only acting upon the records within the cache that you’re working with.

Your last step is to tell the graph object to persist the cache to the database and most of the time that is with a call like myGraph.Actions.PressSave() or sometimes just myGraph.Save() myGraph.Persist()  (thank you Daryl!). That’s when the records within the cache will be examined and compared to what’s in the database. Records that exist in the cache but on in the database will be inserted into the database, existing records will be updated, and records that have been deleted will be removed from the database.

Hopefully that helps.

With regards to adding a record to the table when the user accesses the screen, I think RowSelected would work. It’s called often and thus your code will be called often so make your code as efficient as possible.

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

2 replies

Forum|alt.badge.img+6
  • Captain II
  • 554 replies
  • Answer
  • May 21, 2024

I think you’re just missing one line from your pseudo code. The call to .Insert or .Update (and even .Delete) doesn’t to anything to the database. It’s only acting upon the records within the cache that you’re working with.

Your last step is to tell the graph object to persist the cache to the database and most of the time that is with a call like myGraph.Actions.PressSave() or sometimes just myGraph.Save() myGraph.Persist()  (thank you Daryl!). That’s when the records within the cache will be examined and compared to what’s in the database. Records that exist in the cache but on in the database will be inserted into the database, existing records will be updated, and records that have been deleted will be removed from the database.

Hopefully that helps.

With regards to adding a record to the table when the user accesses the screen, I think RowSelected would work. It’s called often and thus your code will be called often so make your code as efficient as possible.


darylbowman
Captain II
Forum|alt.badge.img+13
Django wrote:

...or sometimes just myGraph.Save()

myGraph.Persist()


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