修改字段 Purchtable createorder x++ 时出现编辑错误

Got edit error onmodified field Purchtable createorder x++

我每次使用此代码时都会收到错误 "you cannot edit the record in the table Purchtable"。 我无法继续执行订单。我的任务是获取每个站点的默认地址。

[ExtensionOf(formdatasourcestr(PurchCreateOrder, PurchTable))]
final class PurchCreateOrderGetDefAdress_Extension
{
[FormDataFieldEventHandler(formDataFieldStr(PurchCreateOrder, PurchTable, OrderAccount), FormDataFieldEventType::Modified)]
public static void OrderAccount_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
{
    Formdatasource                              purchtable_ds = sender.datasource();
    PurchTable                                  purchtable = purchtable_ds.cursor();
    InventSite                                  inv;
    InventLocation                              invent;

    str iid =  UserInfoSz::findDefaultSite(curUserId()).InventSiteId;
    purchtable.inventsiteid = iid;
    purchTable.setAddressFromInventSiteId(iid);

    select firstonly invent where invent.inventsiteid == iid && invent.WMSLocationIdDefaultReceipt == "recv";
    if(invent.RecId !=0)
    {
        purchtable.inventlocationid = invent.InventLocationId;
    }
    else
    {
        select firstonly invent where invent.inventsiteid == iid;
        purchtable.inventlocationid = invent.InventLocationId;
    }
}

}

我认为 purchTable.setAddressFromInventSiteId(iid); 方法会导致您出现该问题。

试试这个代码:

[ExtensionOf(formdatasourcestr(PurchCreateOrder, PurchTable))]
final class PurchCreateOrderGetDefAdress_Extension
{
    [FormDataFieldEventHandler(formDataFieldStr(PurchCreateOrder, PurchTable, OrderAccount), FormDataFieldEventType::Modified)]
    public static void OrderAccount_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
    {
        Formdatasource                              purchtable_ds = sender.datasource();
        PurchTable                                  purchtable = purchtable_ds.cursor();
        InventSite                                  inv;
        InventLocation                              invent;

        str iid =  UserInfoSz::findDefaultSite(curUserId()).InventSiteId;
        ttsbegin;
        If(purchtable.recid)
        {
            purchtable.selectforupdate(true);    
            purchtable.inventsiteid = iid;
            purchTable.setAddressFromInventSiteId(iid);

            select firstonly invent where invent.inventsiteid == iid && invent.WMSLocationIdDefaultReceipt == "recv";
            if(invent.RecId !=0)
            {
                purchtable.inventlocationid = invent.InventLocationId;
            }
            else
            {
                select firstonly invent where invent.inventsiteid == iid;
                purchtable.inventlocationid = invent.InventLocationId;
            }
        }
        ttscommit;
    }
}