Container Receipts can only be updated from Container Receipt Entry Issue

Hello everyone,
I’ve been working away on a customization for updating Container Receipts, for Forklift operators.

The remit was to make is simple, allow the ability to scan multiple lots onto the forklift at once and update the destination Bin on receipt then receive the lines.

I initially started with using the Container Receipt form, and after a quite a while of working through the issues, and deploying it to a tablet, I’ve discovered the performance is pretty woeful.

I’ve now started looking at how I could use a UD form and add the customization to that hopefully speeding things up. Everything appeared to be working ok for my first cut (at least loading the data), but I decided I would see if I could use the OnChangeDtlReceived method pass in the selected data and perform the update.

Clearly I’m doing something wrong here as I keep getting a “Record Not available error”.

Interestingly when I was testing out using the Receive Container Method, I got the message “Container Receipts can only be updated from Container Receipt Entry”, it’s got me thinking that the Record Not available error is a the Epicor way in the OnChangeDtlReceived method telling you can’t do this from another form.

// **************************************************
// Custom code for UD11Form
// Created: 28/08/2020 7:10:21 PM
// **************************************************

//extern alias Ice_Contracts_BO_DynamicQuery;

using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Ice.Adapters;
using Ice.BO;
using Ice.Lib;
using Ice.Lib.Customization;
using Ice.Lib.ExtendedProps;
using Ice.Lib.Framework;
using Ice.Lib.Searches;
using Ice.UI;
using Ice.UI.FormFunctions;

//Added for filtering grids
using System.Reflection;
using Erp.Adapters;
using Infragistics.Win.UltraWinGrid;

public class Script {
    // ** Wizard Insert Location - Do Not Remove 'Begin/End Wizard Added Module Level Variables' Comments! **
    // Begin Wizard Added Module Level Variables **

    // End Wizard Added Module Level Variables **

    // Add Custom Module Level Variables Here **
    string version = "1.3"; //change value of version every time.

    BAQDataView baqViewToBeReceiptedView;
    EpiDataView edvLotNumCustomDV;

    string LotNum;
    int RcvLotCount;
    string RcvPONum;
    string strContainerID;

    public void InitializeCustomCode () {
        // ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
        // Begin Wizard Added Variable Initialization

        // End Wizard Added Variable Initialization

        // Begin Wizard Added Custom Method Calls

        this.epiTxtBxLotNum.Validated += new System.EventHandler (this.epiTxtBxLotNum_Validated);
        this.epiBtnReceive.Click += new System.EventHandler (this.epiBtnReceive_Click);
        // End Wizard Added Custom Method Calls
        LotNumPublisherView ();
        CreateToBeReceiptedView ();

    }

    public void DestroyCustomCode () {
        // ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
        // Begin Wizard Added Object Disposal

        this.epiTxtBxLotNum.Validated -= new System.EventHandler (this.epiTxtBxLotNum_Validated);
        this.epiBtnReceive.Click -= new System.EventHandler (this.epiBtnReceive_Click);
        // End Wizard Added Object Disposal

        // Begin Custom Code Disposal

        // End Custom Code Disposal
    }

    private void UD11Form_Load (object sender, EventArgs args) {
        // Add Event Handler Code
        EpiDataView edvCallContextClientData = ((EpiDataView) (this.oTrans.EpiDataViews["CallContextClientData"]));
        System.Data.DataRow edvCallContextClientDataRow = edvCallContextClientData.CurrentDataRow;

        if ((edvCallContextClientDataRow != null)) {
            UD11Form.Text = UD11Form.Text + " - " + version;
        }

    }

    public void CreateToBeReceiptedView () {
        baqViewToBeReceiptedView = new BAQDataView ("TIL_UBAQ_Receipt");
        oTrans.Add ("ToBeReceiptedBAQ", baqViewToBeReceiptedView);

        var binding = "this.epiTxtBxLotNum.Text";

        // This publishes a field on the form. It's global so if you have more than one BAQ needing the same published field refactor this
        oTrans.PublishColumnChange (binding, Guid.NewGuid ().ToString ());
        var publish = oTrans.GetPublisher (binding);

        // Subscribes the baq field to the published form field
        baqViewToBeReceiptedView.SubscribeToPublisher (publish.PublishName, "MyBaqTable_SomeField");

    }

    public void SetParentView () {
        baqViewToBeReceiptedView.SetParentView (edvLotNumCustomDV, "PONum", "RcvDtl_PONum", "=");
    }

    private void RefreshToBeReceiptedView () {
        MethodInfo mi = baqViewToBeReceiptedView.GetType ().GetMethod ("invokeExecute", BindingFlags.Instance | BindingFlags.NonPublic);
        mi.Invoke (baqViewToBeReceiptedView, new object[] { true });
    }

    public void LotNumPublisherView () {
        DataTable dt = new DataTable ();
        dt.Columns.Add (new DataColumn ("LotNum", typeof (string)));
        dt.Columns.Add (new DataColumn ("PONum", typeof (string)));
        dt.Columns.Add (new DataColumn ("SysRowID", typeof (Guid)));
        var r = dt.NewRow ();
        dt.Rows.Add (r);
        EpiDataView edvLotNumCustomDV = new EpiDataView ();
        edvLotNumCustomDV.dataView = dt.DefaultView;
        oTrans.Add ("edvLotNumCustomDV", edvLotNumCustomDV);
    }

    private void epiTxtBxLotNum_Validated (object sender, System.EventArgs args) {
        // ** Place Event Handling Code Here **
        if (this.epiTxtBxLotNum.Text != "") {
            var myCustomDV = oTrans.Factory ("edvLotNumCustomDV");
            LotNum = this.epiTxtBxLotNum.Text;
            DynamicQueryAdapter dqa = new DynamicQueryAdapter (oTrans);
            dqa.BOConnect ();
            QueryExecutionDataSet qeds = dqa.GetQueryExecutionParametersByID ("TIL_TabReceiptByLot");
            qeds.ExecutionParameter.Clear ();
            qeds.ExecutionParameter.AddExecutionParameterRow ("LotNum", LotNum, "nvarchar", false, Guid.NewGuid (), "A");
            dqa.ExecuteByID ("TIL_TabReceiptByLot", qeds);
            if (dqa.QueryResults.Tables["Results"].Rows.Count > 0) {
                if (dqa.QueryResults.Tables["Results"].Rows[0]["RcvDtl_Received"].ToString () == "False") {
                    RcvLotCount = dqa.QueryResults.Tables["Results"].Rows.Count;

                    RcvPONum = dqa.QueryResults.Tables["Results"].Rows[0]["RcvDtl_PONum"].ToString ();
                    DataRow editRow = myCustomDV.CurrentDataRow;

                    editRow.BeginEdit ();
                    editRow["LotNum"] = LotNum;
                    editRow["PONum"] = RcvPONum;
                    editRow.EndEdit ();

                    RefreshToBeReceiptedView ();
                    SelectLotsOnGrid ();

                } else {
                    MessageBox.Show ("Lot does not exist, or already has been received.");
                    epiTextBoxC1.Select ();
                }
            } else {
                MessageBox.Show ("Lot does not exist, or already has been received. 2");

            }

        }

    }

    private void SelectLotsOnGrid () {
        try {
            if (epiUGToBeReceipted.Rows.Count > 0) {
                foreach (UltraGridRow row in epiUGToBeReceipted.Rows) {
                    if (row.Cells["RcvDtl_LotNum"].Value.ToString () == LotNum) {
                        epiUGToBeReceipted.ActiveRow = row;
                        epiUGToBeReceipted.Selected.Rows.Add (row);

                    }
                }
            }

        } catch (System.Exception ex) {
            ExceptionBox.Show (ex);
        }
    }

    private void FilterGridsByPO () {
        //filters the dataview by the po num (might have to change to a grid filter so as to preserve the content tab details

        //Check the rows in the grid, if none then don't try to filter.
        if (epiUGToBeReceipted.Rows.Count > 0)

        {

            epiUGToBeReceipted.DisplayLayout.Bands[0].ColumnFilters["RcvDtl_PONum"].FilterConditions.Clear (); //clears any filters
            epiUGToBeReceipted.DisplayLayout.Bands[0].ColumnFilters["RcvDtl_PONum"].FilterConditions.Add (FilterComparisionOperator.Equals, RcvPONum);
            epiUGToBeReceipted.DisplayLayout.Bands[0].ColumnFilters["RcvDtl_Received"].FilterConditions.Add (FilterComparisionOperator.Equals, false);
        }

        if (epiUGReceived.Rows.Count > 0) {
            epiUGReceived.DisplayLayout.Bands[0].ColumnFilters["PONum"].FilterConditions.Clear (); //clears any filters
            epiUGReceived.DisplayLayout.Bands[0].ColumnFilters["PONum"].FilterConditions.Add (FilterComparisionOperator.Equals, RcvPONum);
        }
    }

    private void epiBtnReceive_Click (object sender, System.EventArgs args) {
        // ** Place Event Handling Code Here **
        epiUGToBeReceipted.Select ();
        for (int i = epiUGToBeReceipted.Selected.Rows.Count - 1; i > -1; i--) {
            //Need to set the active row based on the selected row.
            epiUGToBeReceipted.ActiveRow = epiUGToBeReceipted.Selected.Rows[i];
            //epiUGToBeReceipted.Selected.Rows[i].Cells["RcvDtl_Received"].Value = true;

            strContainerID = epiUGToBeReceipted.Selected.Rows[i].Cells["RcvDtl_ContainerID"].Value.ToString ();
            int vendorNum = Convert.ToInt32 (epiUGToBeReceipted.Selected.Rows[i].Cells["RcvDtl_VendorNum"].Value.ToString ());
            string purPoint = epiUGToBeReceipted.Selected.Rows[i].Cells["RcvDtl_PurPoint"].Value.ToString ();
            string packSlip = epiUGToBeReceipted.Selected.Rows[i].Cells["RcvDtl_PackSlip"].Value.ToString ();
            int packLine = Convert.ToInt32 (epiUGToBeReceipted.Selected.Rows[i].Cells["RcvDtl_PackLine"].Value.ToString ());
            CallReceiptAdapterOnChangeDtlReceivedMethod (vendorNum, purPoint, packSlip, packLine, true);

        }
    }

    private void CallReceiptAdapterOnChangeDtlReceivedMethod (int vendorNum, string purPoint, string packSlip, int packLine, bool ipReceived) {
        try {
            // Declare and Initialize EpiDataView Variables
            // Declare and create an instance of the Adapter.
            ReceiptAdapter adapterReceipt = new ReceiptAdapter (this.oTrans);
            adapterReceipt.BOConnect ();

            // Declare and Initialize Variables
            // TODO: You may need to replace the default initialization with valid values as required for the BL method call.

            // Call Adapter method
            bool result = adapterReceipt.OnChangeDtlReceived (vendorNum, purPoint, packSlip, packLine, ipReceived);

            // Cleanup Adapter Reference
            adapterReceipt.Dispose ();

        } catch (System.Exception ex) {
            ExceptionBox.Show (ex);
        }
    }
}