Sage 100

 View Only
  • 1.  Clearing PO_ReceiptReturnMatRqByPO record via scripting

    Posted 05-09-2022 12:25
    HI All,
      Have a client that is setting up scripts to generate PO receipt of invoice entries. They find that if they wind up with a failed record and then delete the that the PO record remains in PO_ReceiptReturnMatRqByPO which prevents entering the record again.   So, one of two questions.   How can the delete ROI be doing in a way that clearer is from PO_ReceiptReturnMatRqByPO or how can they find and delete the record from this table via scripting?

    ------------------------------
    Bob Osborn
    Consultant
    ACI Consulting
    ------------------------------


  • 2.  RE: Clearing PO_ReceiptReturnMatRqByPO record via scripting

    Posted 05-10-2022 01:45
    Let me start off by asking how do they delete the ROI Entry once it's in a bad state? IOW are they using the scripting Delete() method or another way? Also are they generating ROI Entries via a button script on a Sage screen or via BOI?

    ------------------------------
    Alnoor Cassim

    Email: alnoor@asifocus.com
    Ph:
    ------------------------------



  • 3.  RE: Clearing PO_ReceiptReturnMatRqByPO record via scripting

    Posted 05-10-2022 11:03
    HI Alnoon,
       They are developing an app for AP entry.  In my limited understanding, I was thinking the same thing. as far as the delete method goes.  I think I need to ask them for the code they are using but this is what they told me:

    Hi Bob,

    I'm having the following issue.

    Trying to create a receipt of invoice - creating the record with setting the keys, setting values for the rest of the fields, including PO and the lines. In the end writing the record.

     Now, let's say for example I'm trying to set the comment field with more than 30 characters. It fails. until here all good.

     In out flow we want in this case to delete the record from Sage (because once you entered the keys the record is created and is empty), let the user know of the problem, he fixes it and try to create again the fixed receipt of invoice (we have the same flow with GL based invoices).



    ------------------------------
    Bob Osborn
    Consultant
    ACI Consulting
    ------------------------------



  • 4.  RE: Clearing PO_ReceiptReturnMatRqByPO record via scripting

    Posted 05-10-2022 11:08
    Looks like someone started this discussion on Sage City too:
    link

    ------------------------------
    Kevin Moyes
    Technical Systems Analyst
    Munjal White Consulting Co.
    ------------------------------



  • 5.  RE: Clearing PO_ReceiptReturnMatRqByPO record via scripting

    Posted 05-10-2022 12:35
    Here is what he sent to me: 
    This is a simplification of what my code does:
    bool objectDropped=false;
    using (dynamic receiptInvoice = SessionUtility.NewObject(holder, "PO_Receipt_bus"))
                {
                    try
                    {
                             //Setting the Keys
                             .....
                             receiptInvoice.nSetKey();
    
    
                             //Setting other fields
                             ............
                             receiptInvoice.DropObject();
                             objectDropped=true;
    
                    }
                    catch (Exception e)
                    {
                            //Setting the keys of the record (Same keys as in try)
                            .........
                            receiptInvoice.nSetKey();
                            receiptInvoice.nDelete()
                        
                            if (!objectDropped)
                            {
                                  receiptInvoice.DropObject();
                            }
                      }
                }
    ​


    ------------------------------
    Bob Osborn
    Consultant
    ACI Consulting
    ------------------------------



  • 6.  RE: Clearing PO_ReceiptReturnMatRqByPO record via scripting

    Posted 05-10-2022 12:48
    What is the retVal / LastErrorMsg on the nDelete()?

    ------------------------------
    Kevin Moyes
    Technical Systems Analyst
    Munjal White Consulting Co.
    ------------------------------



  • 7.  RE: Clearing PO_ReceiptReturnMatRqByPO record via scripting

    Posted 05-10-2022 13:16
    What Kevin said. He is not checking for a return value so he doesn't know if an error occurred. He then needs to check receiptInvoice.sLastErrorMsg to see if the Receipt object returns an error message. His app code is too basic. You could recommend he or via your customer, take the Sage U Anytime Learning course on Object Interface.

    ------------------------------
    Alnoor Cassim

    Email: alnoor@asifocus.com
    Ph:
    ------------------------------



  • 8.  RE: Clearing PO_ReceiptReturnMatRqByPO record via scripting

    Posted 05-13-2022 10:04
    Here the complete section of code.   
     internal static bool ExportPOBasedInvoice(SessionHolder holder, ApTransaction apTransaction, ExportResponse response, string batch, string customer)
            {
                int batchEnabled;
                bool objectDropped = false;
                SessionUtility.SetUIClass(holder.Session, "PO_ReceiptOfInvoice_Ui");
                using (dynamic receiptInvoice = SessionUtility.NewObject(holder, "PO_Receipt_bus"))
                {
                    batchEnabled = receiptInvoice.nBatchEnabled;
                    bool recordCreated = false;
                    string receiptNumber = "";
                    try
                    {
                        SetReceiptHeader(holder, apTransaction, receiptInvoice, ref receiptNumber, ref recordCreated, batch, taxEnabled);
                        string MASVer = "";
                        holder.Session.nGetParameter("SYS", "Version$", ref MASVer);
                        bool sageSupportMultiplePO = !MASVer.StartsWith("6.1") && !MASVer.StartsWith("6.0");
                        if (sageSupportMultiplePO)
                        {
                            ApplyPOs(receiptInvoice, apTransaction);
                        }
                        else
                        {
                            SessionUtility.SetPOToInvoice(receiptInvoice,apTransaction.purchaseOrders[0]);
                        }
                        SessionUtility.WriteRecord(receiptInvoice);
    
                        response.response.content = "OK";
                        response.responseCode = FinSysResponseCode.OK;
                        receiptInvoice.DropObject();
                        objectDropped = true;
    
                        if (DoPosting(batchEnabled, apTransaction))
                        {
                            HandlePosting(holder, apTransaction, "PO_ReceiptRegister");
                        }
                    }
                    catch (Sage100Exception e)
                    {
                        Logger.Error("Export of invoice failed", e);
                        try
                        {
                            HandleBrokenReceiptRecord(recordCreated, receiptNumber, receiptInvoice);
                        }
                        catch (Exception e2)
                        {
                            Logger.Error("There was a problem deleting the broken record", e2);
                        }
                        if (!objectDropped)
                        {
                            receiptInvoice.DropObject();
                        }
                        response.responseCode = FinSysResponseCode.Error;
                    }
                }
                return batchEnabled == 1;
            }
    
    
    private static void SetReceiptHeader(SessionHolder holder, ApTransaction apTransaction, dynamic receiptInvoice, ref string receiptNumber, ref bool recordCreated, string batch, bool taxEnabled)
            {
                recordCreated = SetReceiptKeys(receiptInvoice, ref receiptNumber, batch);
                Dictionary<string, object> values = new Dictionary<string, object>()
                {
                    { "PurchaseOrderNo$", apTransaction.purchaseOrders[0]},
                    { "InvoiceNo$", apTransaction.transactionId },
                };
                SessionUtility.SetValues(receiptInvoice, values);
            }
    
    
    private static bool SetReceiptKeys(dynamic receiptInvoice, ref string receiptNumber, string batch)
            {
                receiptNumber = SessionUtility.GetNextReceiptNo(receiptInvoice);
                Dictionary<string, string> keysValues = new Dictionary<string, string>()
                {
                    { "ReceiptType$", "I"},
                    { "ReceiptNo$", receiptNumber},
                };
    
                int batchEnabled = receiptInvoice.nBatchEnabled;
                if (batchEnabled == 1)
                {
                    SessionUtility.SetKeysWithBatch(receiptInvoice, keysValues, batch);
                }
                else
                {
                    SessionUtility.SetKeys(receiptInvoice, keysValues);
                }
                return true;
            }
    
    
    private static void HandleBrokenReceiptRecord(bool recordCreated, string receiptNumber, dynamic receipts)
            {
                if (recordCreated)
                {
                    Dictionary<string, string> keysValues = new Dictionary<string, string>()
                    {
                        { "ReceiptType$", "I"},
                        { "ReceiptNo$", receiptNumber},
                    };
                    SessionUtility.HandleBrokenRecord(receipts, keysValues);
                }
            }
    
    
    
    public static void SetValues(dynamic businessObject, Dictionary<string, object> values, bool write = false)
            {
                foreach (string column in values.Keys)
                {
                    ExecuteMethodIntegerReturn(businessObject, (FunctionIntegerReturn)(() => { return businessObject.nSetValue(column, values[column]); }),
                        "setting column " + column + " with value " + values[column]);
                }
                if (write)
                {
                    WriteRecord(businessObject);
                }
            }
    
    public static void WriteRecord(dynamic record)
            {
                ExecuteMethodIntegerReturn(record, (FunctionIntegerReturn)(() => { return record.nWrite(); }), "writing a new record to file");
            }
    
    private static void ApplyPOs(dynamic receiptInvoice, ApTransaction apTransaction)
            {
                string firstPO = apTransaction.purchaseOrders[0];
                SessionUtility.ApplyPrimaryPOToReceipt(receiptInvoice, firstPO);
    
                foreach (string poID in apTransaction.purchaseOrders)
                {
                    if (poID.Equals(firstPO))
                    {
                        continue;
                    }
                    SessionUtility.ApplyPOToReceipt(receiptInvoice, poID);
                }
                SessionUtility.ApplyPurchaseOrderCopyLines(receiptInvoice);
            }
    
    internal static void SetPOToInvoice(dynamic receiptInvoice, string poID)
            {
                ExecuteMethodNoReturn(receiptInvoice, (FunctionVoidReturn)(() => { receiptInvoice.nCopyFromPurchaseOrder(poID); }), "copying purchase order's header data");
                ExecuteMethodNoReturn(receiptInvoice, (FunctionVoidReturn)(() => { receiptInvoice.nCopyPurchaseOrderLines(0); }), "copying purchase order's lines to invoice");
            }
    ​


    ------------------------------
    Bob Osborn
    Consultant
    ACI Consulting
    ------------------------------