Acumatica - Copy last row

2019-08-27 23:15发布

问题:

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.

回答1:

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...

SOLine line = Base.Transactions.Select().LastOrDefault();
var copy = (SOLine)Base.Transactions.Cache.CreateCopy(line);
copy.LineNbr = null;
Base.Transactions.Cache.Insert(copy);

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).



回答2:

The code should be like this:

public PXAction<SOOrder> copyLastRow;
[PXUIField(DisplayName = "Copy Last Row", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
[PXProcessButton]
public virtual IEnumerable CopyLastRow(PXAdapter adapter)
{
    SOLine line = Base.Transactions.Select().LastOrDefault();
    SOLine newLine = new SOLine();
    ... (copy all you need from line to newLine)
    Base.Transactions.Cache.Insert(newLine);           
    Base.Actions.PressSave();
    return adapter.Get();
} 


回答3:

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.



标签: acumatica