0

Every time Im using this code I get a error "you cannot edit the record in the table Purchtable". I cant proceed the order with it. My task is to get the default address for each site.

[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;
    }
}

}
Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
Tweene
  • 257
  • 4
  • 16
  • Probably a conflict with the logic in form `PurchCreateOrder`, method `orderAccountModified`. Try extending that method instead. – FH-Inway Mar 03 '20 at 15:04

1 Answers1

2

I think the method purchTable.setAddressFromInventSiteId(iid); causes you that problem.

Try this code:

[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;
    }
}
Jonathan Bravetti
  • 2,228
  • 2
  • 15
  • 29
  • No still not good. I tried to debug it and it looks like there is no recid for purchtable. {recid = 0} – Tweene Mar 03 '20 at 14:08
  • Update the post, now you will not have the error. Anyway try to find why purchtable it's empty to make it work correctly – Jonathan Bravetti Mar 03 '20 at 18:12
  • It works fine now. Thank you. There was a problem from older solutions. Could you help me with one more thing ? I want to catch if purchTable.setAddressFromInventSiteId(iid); isset correcly because if I dont have a address connected to inventsite it lets the default site address on form from companyinfo I quess. – Tweene Mar 11 '20 at 08:03
  • Okay. I got used what I needed from this method. Now its good – Tweene Mar 11 '20 at 10:56