cancel
Showing results for 
Search instead for 
Did you mean: 

Adding a batch to a line item in an Order with B1WS

Former Member
0 Kudos

Hi,

I am trying to create code which can update a batch in a line item in an order. The updating works great, if the batch is already attached to the line item. However, I am unable to add a batch if one doesn't already exist.

Can you please have a look at this code and let me know what is wrong?

I've tried various things, though am still unable to get it to work.

Thank you very much!

Mike


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using orderWebRef;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // B1BATCH performs batch operations on SAP via DISERVER and B1WS
        // The inputs are:
        // o = Order Number (this is the DocEntry value, NOT the DocNum value)
        // b = Batch Number
        // q = Quantity
        // i = Item Code
        // p = Project
        //
        // debug = 1 or true if you want to see output messages
        //
        // For some security, we will encode some values at the client,
        // and verify them here for consistency.
        // The "Secret Formula" is these variables, which will be passed to the URL
        // 
        // 1 = Order Number * 2 (encoded at the client)
        // 2 = Batch Number * 2 (encoded at the client)
        //
        // To decode, we do this:
        //
        // Correct decoding if ((1 = (o/2)) AND (2 = (b/2))

        string o = "";
        string b = "";
        string q = "";

        string i = "";
        string p = "";

        string v1 = "";
        string v2 = "";

        int orderNumber = 0; // This is the DocEntry value, NOT the DocNum value
        int batchNumber = 0;
        int quantity = 0;

        int v1int = 0;
        int v2int = 0;

        bool debugMode = false;
        bool allVarsPresent = true;
        bool allVarsValid = true;

        try
        {
            o = Request.QueryString["o"].ToString();
        }
        catch
        {
            allVarsPresent = false;
        }

        try
        {
            b = Request.QueryString["b"].ToString();
        }
        catch
        {
            allVarsPresent = false;
        }

        try
        {
            q = Request.QueryString["q"].ToString();
        }
        catch
        {
            allVarsPresent = false;
        }

        try
        {
            i = Request.QueryString["i"].ToString().ToUpper();
        }
        catch
        {
            allVarsPresent = false;
        }

        try
        {
            p = Request.QueryString["p"].ToString().ToUpper();
        }
        catch
        {
            allVarsPresent = false;
        }

        try
        {
            v1 = Request.QueryString["1"].ToString();
        }
        catch
        {
            allVarsPresent = false;
        }

        try
        {
            v2 = Request.QueryString["2"].ToString();
        }
        catch
        {
            allVarsPresent = false;
        }

        try
        {
            string debug = Request.QueryString["debug"].ToString();

            if ((debug == "1") || (debug.ToLower() == "true"))
            {
                debugMode = true;
            }
        }
        catch
        {
            debugMode = false;
        }

        if (!allVarsPresent)
        {
            WriteDebugMessage(debugMode, "All required variables are not in the request.");

            return;
        }
        else
        {
            // All variables are present, now check that they are valid.
            //
            // First, check the v1 and v2 variables are correct
            // Then check that the order exists
            // Then check that the batch exists and has the appropraite quantity available

            try
            {
                orderNumber = Convert.ToInt32(o);
                batchNumber = Convert.ToInt32(b);
                quantity = Convert.ToInt32(q);

                v1int = Convert.ToInt32(v1);
                v2int = Convert.ToInt32(v2);

                if ((v1int / 2) != orderNumber)
                {
                    allVarsValid = false;
                }

                if ((v2int / 2) != batchNumber)
                {
                    allVarsValid = false;
                }
            }
            catch
            {
                WriteDebugMessage(debugMode, "Error in processing variables.");

                return;
            }

            if (!allVarsValid)
            {
                WriteDebugMessage(debugMode, "Error in validating checksum.");

                return;
            }

            // All variables are present and valid!
            WriteDebugMessage(debugMode, "All variables are present and valid.");
        }

        try
        {
            loginWebRef.LoginService ls = new loginWebRef.LoginService();

            string sessionID = ls.Login("DBSERVER", "DBNAME", loginWebRef.LoginDatabaseType.dst_MSSQL2005, true, "sa", "PASSWORD", "manager", "managerPass", loginWebRef.LoginLanguage.ln_English, true, "LICENSE:30000");

            WriteDebugMessage(debugMode, "<BR><BR>SessionId: " + sessionID + "<BR><BR>");

            orderWebRef.MsgHeader msgHeader = new orderWebRef.MsgHeader();

            msgHeader.SessionID = sessionID;
            msgHeader.ServiceName = MsgHeaderServiceName.OrdersService;
            msgHeader.ServiceNameSpecified = true;

            orderWebRef.OrdersService order = new OrdersService();
            order.MsgHeaderValue = msgHeader;

            DocumentParams param = new DocumentParams();
            param.DocEntry = orderNumber;
            param.DocEntrySpecified = true;

            orderWebRef.Document document = new orderWebRef.Document();
            document = order.GetByParams(param);

            StringBuilder orderSb = new StringBuilder();

            orderSb.Append("Order " + document.DocNum.ToString() + "<BR><BR>");

            foreach (DocumentDocumentLine docLine in document.DocumentLines)
            {
                if ((docLine.ProjectCode == p) && (docLine.ItemCode == i))
                {
                    orderSb.Append("Line Num:      " + docLine.LineNum + "<BR>");
                    orderSb.Append("Item Code:     " + docLine.ItemCode + "<BR>");
                    orderSb.Append("Project Code:  " + docLine.ProjectCode + "<BR>");
                    orderSb.Append("Description:   " + docLine.ItemDescription + "<BR>");

                    // Find the batch if it exists and update the quantity
                    // Add the batch if it doesn't exist

                    bool batchExistsInLineItem = false;
                    int batchCount = 0;

                    foreach (DocumentDocumentLineBatchNumber batch in docLine.BatchNumbers)
                    {
                        batchCount++;

                        if (batch.BatchNumber == b)
                        {
                            batchExistsInLineItem = true;

                            orderSb.Append("<BR>");
                            orderSb.Append("+++++ Batch exists in line item.  Updating.<BR><BR>");
                            orderSb.Append("+++++ Batch Line:   " + batchCount.ToString() + "<BR>");
                            orderSb.Append("+++++ Batch Num:    " + batchNumber + "<BR>");
                            orderSb.Append("+++++ Quantity:     " + quantity + "<BR>");

                            batch.Quantity = quantity;
                        }
                    }

                    if (!batchExistsInLineItem)
                    {
                        docLine.BatchNumbers = new DocumentDocumentLineBatchNumber[1];
                        DocumentDocumentLineBatchNumber batch = new DocumentDocumentLineBatchNumber();

                        batch.BatchNumber = b;
                        batch.Quantity = quantity;

                        docLine.BatchNumbers[0] = batch;

                        orderSb.Append("<BR>");
                        orderSb.Append("+++++ Batch does not exist in line item.  Adding.<BR><BR>");
                        orderSb.Append("+++++ Batch Line:   " + batchCount.ToString() + "<BR>");
                        orderSb.Append("+++++ Batch Num:    " + batchNumber + "<BR>");
                        orderSb.Append("+++++ Quantity:     " + quantity + "<BR>");
                    }
                }
            }

            try
            {
                order.Update(document);
            }
            catch (Exception ex)
            {
                WriteDebugMessage(debugMode, "SAP Exception:  " + ex.Message.ToString());

                return;
            }

            WriteDebugMessage(debugMode, orderSb.ToString());
        }
        catch (Exception ex)
        {
            WriteDebugMessage(debugMode, "Error in processing the request.  Exception:<BR><BR>" + ex.Message);

            return;
        }
    }

    private void WriteDebugMessage(bool debugMode, string exitMessage)
    {
        if (debugMode)
        {
            Response.Write(exitMessage);
        }
    }
}

Accepted Solutions (0)

Answers (2)

Answers (2)

Trinidad
Product and Topic Expert
Product and Topic Expert
0 Kudos

Please mark the message as Solved if it is solved, this time I did it for you.

Former Member
0 Kudos

Hi everyone, I am very sorry, I do not know why the formatting on the code or the post isn't working. I hope that you are able to read it. Please find the code formatted for reading here: Link: [Code here|http://www.paste-it.net/public/k99da90/]

Former Member
0 Kudos

Hi Everyone,

I found the answer, you can find the code here: [B1WS Batch Manipulations with Orders|http://paste-it.net/public/dbbcea0/]

Cheers,

Mike