So looks like seemingly easy things in Acumatica are terribly complicated to implement. All I wanna do is to copy last row of my grid as a new one. I would like the user to persist the changes himself, so my code would put it just in cache. This is my action so far:
public PXAction<SOOrder> copyLastRow;
[PXUIField(DisplayName = "Copy Last Row", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
[PXLookupButton]
public virtual IEnumerable CopyLastRow(PXAdapter adapter)
{
SOLine line = Base.Transactions.Select().LastOrDefault();
int? lineNbr = 0;
foreach(SOLine line2 in Base.Transactions.Select())
if (line2.LineNbr > lineNbr)
lineNbr = line2.LineNbr;
line.LineNbr = lineNbr + 1;
Base.Transactions.Cache.Insert(line);
return adapter.Get();
}
So maybe I am not getting something or completely wrong in my code, but I'm getting errors no matter what I do. The grid is not getting refreshed with my row and I keep getting all sorts of errors, such as "This record cannot be saved" or "another process updated this record", etc. Also, any ideas on how to generate a new lineNbr without clunky logic I have? Much appreciated if anyone can help.
If you just want to take the line and copy all values you can use cache copy and null the lineid. Then on insert of the copied line it will get the next linenbr automatically... adding to your sample code in your question...
This method should also be upgrade friendly in the event new fields or customization are added to SOLine they will continue to be copied without having to selectively include all fields to be copied (using CreateCopy).
The code should be like this:
It is my guess that Line Number will get automatically generated ! It is not in sequence anyway.
I suggest you create a new SOline row and transfer the enterable fields from the selected row (Last row) in the same and Insert. Should work.