Skip to Content
0

Errors when adding A/P Credit Memos in a Brazil Database

Oct 19, 2016 at 09:41 PM

113

avatar image

I'm trying to essentially create a cancellation document for some A/P down Payment Invoices by creating linked A/P Credit Memos. When I actually go to add the doc, its fails with the error:

[RPC1.CSTfIPI][line: 1] , 'Linked value 03 does not exist'

Even if I manually fill the 'CST for IPI' field with a value I know is valid it still continues to throw that error. I'm rather lost at what the problem could be.

Here is the code as I'm using it:

        private void CancelInvoice(string Invoice)
        {
            SAPbobsCOM.Documents oMemo;
            SAPbobsCOM.Recordset oRSHeader;
            SAPbobsCOM.Recordset oRSLines;


            for (int i = 0; i < Invoices.Count; i++)
            {
                oMemo = Globals.oApplication.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseCreditNotes);
                oRSHeader = Globals.oApplication.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                oRSLines = Globals.oApplication.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);


                Query = "SELECT ";
                Query += "T0.DocEntry, ";
                Query += "T0.CardCode, ";
                Query += "T0.NumAtCard, ";
                Query += "T0.BPLId, ";
                Query += "T0.DocCur, ";
                Query += "T0.DocRate, ";
                Query += "T1.MainUsage ";
                Query += "FROM ODPO T0 ";
                Query += "JOIN DPO12 T1 ON T0.DocEntry = T1.DocEntry ";
                Query += "WHERE DocNum = '" + Invoice + "'";
                oRSHeader.DoQuery(Query);


                // Add Header detail.
                oMemo.CardCode = oRSHeader.Fields.Item("CardCode").Value;
                oMemo.NumAtCard = oRSHeader.Fields.Item("NumAtCard").Value;
                oMemo.DocDueDate = DateTime.Today;
                oMemo.BPL_IDAssignedToInvoice = oRSHeader.Fields.Item("BPLId").Value;
                oMemo.DocCurrency = oRSHeader.Fields.Item("DocCur").Value;
                oMemo.DocRate = oRSHeader.Fields.Item("DocRate").Value;
                oMemo.TaxExtension.MainUsage = oRSHeader.Fields.Item("MainUsage").Value;
                oMemo.Comments = "Based On A/P Down Payment " + Invoice + ".";


                // Get the DP Invoices line information.
                Query = "SELECT ";
                Query += "ItemCode, ";
                Query += "Quantity, ";
                Query += "Price, ";
                Query += "Usage, ";
                Query += "WhsCode, ";
                Query += "LineNum ";
                Query += "FROM DPO1 ";
                Query += "WHERE DocEntry = '" + oRSHeader.Fields.Item("DocEntry").Value + "'";
                oRSLines.DoQuery(Query);


                for (int j = 0; j < oRSLines.RecordCount; j++)
                {
                    //// Adds line detail and ties the Return to the GRPO.
                    oMemo.Lines.BaseEntry = Convert.ToInt32(oRSHeader.Fields.Item("DocEntry").Value);
                    oMemo.Lines.BaseLine = oRSLines.Fields.Item("LineNum").Value;
                    oMemo.Lines.BaseType = 204;
                    oMemo.Lines.Quantity = oRSLines.Fields.Item("Quantity").Value;
                    oMemo.Lines.CSTforIPI = "53";

                    oMemo.Lines.Add();

                    oRSLines.MoveNext();
                }


                if (oMemo.Add() != 0)
                {
                    throw new Exception(Globals.oApplication.Company.GetLastErrorDescription());
                }

                // Clean up the Objects for next run.
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oMemo);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oRSHeader);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oRSLines);
                oMemo = null;
                oRSHeader = null;
                oRSLines = null;
            }
        
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
DIEGO LOTHER Oct 21, 2016 at 01:02 AM
0

Hi Zach,

Based in your scenario and code, I wrote this sample code. Execute this in your database and check if it works. (I did run on SBODemoBR database, the demo database for Brazil and works fine).

Change the hard coded values by valid values in your database.

        private void CreateCancelAPDownPayment()
        {
            if (oCompany == null || !oCompany.Connected)
                this.connect();


            try
            {
                Documents oPchDowPay = oCompany.GetBusinessObject(BoObjectTypes.oPurchaseDownPayments);
                oPchDowPay.CardCode = "V10000"; //Change this
                oPchDowPay.DocDate = DateTime.Now;
                oPchDowPay.DownPaymentType = DownPaymentTypeEnum.dptInvoice;


                oPchDowPay.Lines.ItemCode = "A00002";//Change this
                oPchDowPay.Lines.Quantity = 2;
                oPchDowPay.Lines.Add();
                oPchDowPay.Lines.ItemCode = "A00004";//Change this
                oPchDowPay.Lines.Quantity = 1;


                int docEntry = 0;
                if(oPchDowPay.Add() != 0)
                {
                    throw new Exception(oCompany.GetLastErrorDescription());
                }
                else
                {
                    docEntry = Convert.ToInt32(oCompany.GetNewObjectKey());
                    CancelPurchaseDownPayments(docEntry);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void CancelPurchaseDownPayments(int docEntry)
        {
            Documents oMemo = null;
            Recordset oRSHeader = null;
            Recordset oRSLines = null;


            try
            {
                oMemo = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseCreditNotes);
                oRSHeader = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                oRSLines = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                
                string query = @"SELECT 
                                    T0.DocEntry,
                                    T0.DocNum,
                                    T0.CardCode,
                                    T0.NumAtCard,
                                    T0.BPLId,
                                    T0.DocCur,
                                    T0.DocRate,
                                    T1.MainUsage
                                FROM 
                                    ODPO T0
                                    JOIN DPO12 T1 ON T0.DocEntry = T1.DocEntry 
                                WHERE 
                                    T0.DocEntry = {0}";
                query = string.Format(query, docEntry);
                oRSHeader.DoQuery(query);


                oMemo.CardCode = oRSHeader.Fields.Item("CardCode").Value;
                oMemo.NumAtCard = oRSHeader.Fields.Item("NumAtCard").Value;
                oMemo.DocDueDate = DateTime.Today;
                oMemo.BPL_IDAssignedToInvoice = oRSHeader.Fields.Item("BPLId").Value;
                oMemo.DocCurrency = oRSHeader.Fields.Item("DocCur").Value;
                oMemo.DocRate = oRSHeader.Fields.Item("DocRate").Value;
                //I commented this line, because the main usage in my sample code returns 0
                //oMemo.TaxExtension.MainUsage = oRSHeader.Fields.Item("MainUsage").Value;
                oMemo.Comments = "Based On A/P Down Payment " + oRSHeader.Fields.Item("DocNum").Value + ".";


                // Get the DP Invoices line information.
                query = @"  SELECT
                                ItemCode,
                                Quantity,
                                Price,
                                Usage,
                                WhsCode,
                                LineNum
                            FROM 
                                DPO1
                            WHERE 
                            DocEntry = {0}";
                query = string.Format(query, docEntry);
                oRSLines.DoQuery(query);


                for (int j = 0; j < oRSLines.RecordCount; j++)
                {
                    //// Adds line detail and ties the Return to the GRPO.
                    oMemo.Lines.BaseEntry = Convert.ToInt32(oRSHeader.Fields.Item("DocEntry").Value);
                    oMemo.Lines.BaseLine = oRSLines.Fields.Item("LineNum").Value;
                    oMemo.Lines.BaseType = (int)BoObjectTypes.oPurchaseDownPayments;
                    //Valid CFOP for me
                    oMemo.Lines.CFOPCode = "5202";//Change this


                    oMemo.Lines.Add();


                    oRSLines.MoveNext();
                }


                if (oMemo.Add() != 0)
                {
                    throw new Exception(oCompany.GetLastErrorDescription());
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oMemo);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oRSHeader);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oRSLines);
                oMemo = null;
                oRSHeader = null;
                oRSLines = null;
            }
        }

Hope it helps,

Kind Regards,

Diego Lother

Share
10 |10000 characters needed characters left characters exceeded
DIEGO LOTHER Oct 20, 2016 at 11:09 AM
0

Hi Zach,

Try the following:

Fill the TaxCode property at the line level of your document. The TaxCode will be provide the information for CSTforIPI automatically. Probably your CSTforIPI that you provided doesn't match with the CSTforIPI of the TaxCode. After this, propably you will receive a message of invalid CFOPCode if this was not provided at the line of your A/P down Payment Invoice.

Hope it helps.

Kind Regards,

Diego Lother

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thanks Diego for the suggestion, but is doesn't seem to work. I generated (but didn't add) a Copy-To -> A/P Credit Memo document from my test DP Invoice, took the values that B1 set, and set the TaxCode (then on later runs the CFOP Code and the CSTforIPI Code) in the code. Still got the same error. I'm thinking either I'm still missing some other reference field I have to fill or when B1 does the auto generation from the BaseEntry, BaseType and BaseLine its overwriting what I put.

I would leave the BaseEntry, Type and Line empty and not tie the CM to the Invoice but if I do it asks to select batches (which the Invoice doesn't have). I seem to be stuck either way.

Thanks again.

0
Zach Hunter Nov 02, 2016 at 07:03 PM
0

Hi Diego,

I ran your code and it seems if I create the DP Invoice using the code, or manually, then the credit memo works, but it still gives the same error if I run it on a DP invoice already in the Test DB (which was copied directly from a instance of production). Although I think I found the issue. Most of these DP invoice are posted from a integrated system, which is putting in values for the CSTfIPI, CSTfPIS, CSTfCOFINS on the DP Invoice Lines. In the Invoice generated manually or from the code above these DB value for those fields are NULL. If the DB values are NULL it doesn't have a problem generating a Credit Memo, if they have values, it does. You can see this in the attached picture, the DocEntry #3932 is the one that was generated by the integrated system and the DocEntry #3975 is the one generated by code above and didn't have a problem. Its also where I think the '03' in the error message is coming from.

It looks like there really isn't a way to work around this so I'll have to look at other approaches. Thanks for your help!


capture.jpg (14.6 kB)
Share
10 |10000 characters needed characters left characters exceeded