Lightweight Persist To Database
Hello everybody,
today I want to describe following use case. Quite often it is needed to persist to database one or another DAC class, which is filled by some data.
As usually I see people do this via hard coding of DAC class inside of the Graph. But today I want to share with you a way of persisting DAC class without hardcoding it as a view.
In order to accomplish this, you can use following graph:
public class ImportEntitiesInsertion : PXGraph<ImportEntitiesInsertion> { public string AddView(Type dacType) { var viewName = "_DYNAMIC_" + dacType.GetLongName(); if (!this.Views.ContainsKey(viewName)) { var command = BqlCommand.CreateInstance(typeof(Select<>), dacType); var newView = new PXView(this, true, command); Views.Add(viewName, newView); Views.Caches.Add(dacType); } return viewName; } }
After that, in some other place of the code, you can use this graph like this:
var graphForInsertion = PXGraph.CreateInstance<ImportEntitiesInsertion>(); var dacType = typeof(SOOrder); var viewName = graphForInsertion.AddView(dacType); for (int i = 0; i < 10; i++) { var newOrd = new SOOrder(); graphForInsertion.Views[viewName].Cache.Insert(newOrd); } graphForInsertion.Persist();
What I especially like about this approach, is that records will be persisted initially in the cache, and only after you'll call Persist, all bunch of records will be persisted to database.
Ready to take your Acumatica development to the next level? The approach shared in this article demonstrates how to dynamically persist DAC classes without hardcoding, offering flexibility and efficiency in your customization projects. If you have a unique business need or a specific customization request that could benefit from this technique—or any other Acumatica development challenge—we’d love to hear from you!
Leave us a customization request today and let’s work together to build tailored solutions that empower your business. Your vision, our expertise—let’s make it happen!