cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Order costs with C#

0 Kudos

Hello,

I am having huge problems with acquiring order cost data to my program in C#.

I run the "BAPI_PRODORD_COSTING" remote function module. I get an answer that Costing was executed. I also get all suborders from selected order, their texts and numbers.

IRfcFunction orderCosting = rfcRepo.CreateFunction("BAPI_PRODORD_COSTING");

IRfcTable orders = orderCosting.GetTable("ORDERS");

orders.Append();

codePO = codePO.PadLeft(12, '0'); //trailing zeros

orders.SetValue("ORDER_NUMBER", codePO);

orderCosting.Invoke(rfcDestination);

But regardless how much I try I can't get the actual costs.
I tried to get App log with "/SDF/GET_APP_LOG" and a log handle passed from previous function.

All I get is only some unclear data messages which does not correspond to actual cost data I'd expect.

Please help.

Thank you

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Actually I need a RFC which implements KOB1 transaction.

It is also possible to get this information via CO03 -> Goto -> Cost -> Analysis OR also S_ALR_87013019.

But I cant find a way to this info with Remote Function module call.

Please help.


Thank you.

0 Kudos

Done it by myself.

I read OBJNR (Object number) from AUFK table for AUFNR order number.

Than I passed OBJNR to COEP table and got all records for selected order.
At last I summarise records depending on the sign. Here is the function:


public static double costEstimate(string productionOrder)

        {

            RfcDestination rfcDestination;

            double sumCost=0;

            try

            {

                rfcDestination = RfcDestinationManager.GetDestination(destConfigName);

                if (rfcDestination != null)

                {

                    RfcRepository rfcRepo = rfcDestination.Repository; //create repository

                    productionOrder = productionOrder.PadLeft(12, '0'); //trailing zeros

                    #region READ_TABLE_AUFK

                    IRfcFunction readTable1 = rfcRepo.CreateFunction("BBP_RFC_READ_TABLE");

                    //IMPORTING

                    readTable1.SetValue("QUERY_TABLE", "AUFK");

                    readTable1.SetValue("ROWCOUNT", "1");

                    readTable1.SetValue("DELIMITER", ";");

  //Desired fields in the answer

                    IRfcTable fieldsTable = readTable1.GetTable("FIELDS");

                    fieldsTable.Append();

                    fieldsTable.SetValue("FIELDNAME", "OBJNR");

  //"SQL" like clauses

                    var options1 = readTable1.GetTable("OPTIONS");

                    options1.Append();

                    options1.SetValue("TEXT", "AUFNR EQ '" + productionOrder + "'");

  //INVOKE

                    readTable1.Invoke(rfcDestination);

  //EXPORTING

                    var data = readTable1.GetTable("DATA");

  string OBJNR = data[0].GetString("WA"); // <-here is the object number we were searching for

  #endregion READ_TABLE_AUFK

  #region READ_TABLE_COEP

                    IRfcFunction readTable2 = rfcRepo.CreateFunction("BBP_RFC_READ_TABLE");

  //IMPORTING

                    readTable2.SetValue("QUERY_TABLE", "COEP");

                    readTable2.SetValue("DELIMITER", ";");

  //"SQL" like clauses

                    var options2 = readTable2.GetTable("OPTIONS");

                    options2.Append();

                    options2.SetValue("TEXT", "OBJNR EQ '" + OBJNR + "'");

  //Desired fields in the answer

                    var flds = readTable2.GetTable("FIELDS");

                    flds.Append();

                    flds.SetValue("FIELDNAME", "WOGBTR"); //Total Value in Object Currency

                    flds.Append();

                    flds.SetValue("FIELDNAME", "TIMESTMP");//Timestamp

  //INVOKE

                    readTable2.Invoke(rfcDestination);

  //EXPORTING

                    var d = readTable2.GetTable("DATA");

  #endregion READ_TABLE_COEP

                    #region SUMM_COMPONENTS

                    foreach (var v in d)

                    {

                        string[] answers = v.GetString("WA").Split(';'); //split by previously selected delimiter

                        double currentElementCost = double.Parse(answers[0].Substring(0, 14).Replace('.', ',')); //parse answer from string to double, replace . with , for correct parsing

                        bool add = answers[0].Substring(14, 1) == "-" ? false : true; //check the sign, if ' ' add, if '-' subtract

                        if (add) sumCost += currentElementCost;

                        else sumCost -= currentElementCost;

                    }

                    #endregion SUMM_COMPONENTS

                }

            }

            catch (Exception ex)

            {

                Debug.WriteLine(ex.Message);

            }

            return sumCost;

        }

I hope it helps anyone.

More information about COEP  here.

Answers (0)