Community

How to add a Workflow Stage that will write data to a SQL table?

I am wanting to create a custom workflow stage that inserts information about the documents into a SQL database table. I would like to write field information for invoices, such as the InvoiceNumber, InvoiceDate, Total, Vendor, etc. Has anyone done this before or knows how to do it? If so, would you mind sharing the steps to accomplish this?

Was this article helpful?

0 out of 0 found this helpful

Comments

2 comments

  • Avatar
    Vladimer Nikuradze

    If I understand you correctly, then after processing the documents you need the information to be recorded in the database, right?

    using System;
    using System.Data.SqlClient;

    string connectionString = @"Data Source=;Initial Catalog=ExportDocument;Integrated Security=True";//In source add your DB source

    /*
    if you don't have this field ServiceField then you can remove it and change the code a little
    I created it to get the name of the document from the path, well, in this case I use it to check in the database. If there is already an existing sing in the database, then there is no need to add a value
    */
    string selectStatement = "SELECT COUNT(*) FROM Invoice WHERE Service_Field = @Value8";
    string insertStatement = "INSERT INTO Invoice (InvoiceNumber, Service_Field) VALUES (@Value1, @Value8)";// check your table and columns

    string value1 = "";
    /*
    ...
    ...
    */
    string value8 = "";

    /*
    here I have a check for a specific document definition since my invoices are divided into categories
    */
    if (Document.DefinitionName=="G2_ინვოისი_ლიმონათი")// add your document definition
    {
        value1 = Document.Field("Invoice\\InvoiceNumber").Text; //for example InvoiceLayout//InvoiceNumber
        /*
        ...
        ...
        */
        value8 = Document.Field("Invoice\\Service Field").Text; //for example InvoiceLayout//ServiceField
        
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            using (SqlCommand selectCommand = new SqlCommand(selectStatement, connection))
            {
                selectCommand.Parameters.AddWithValue("@Value8", value8);

                int existingRowsCount = (int)selectCommand.ExecuteScalar();

                if (existingRowsCount == 0)
                {
                    using (SqlCommand insertCommand = new SqlCommand(insertStatement, connection))
                    {
                        insertCommand.Parameters.AddWithValue("@Value1", value1);
                        insertCommand.Parameters.AddWithValue("@Value8", value8);

                        insertCommand.ExecuteNonQuery();
                    }
                }
            }
        }
    }

    In general the script works like this.
    After processing the documents, if the database already has a record from this document and this is checked through the service field, then a new record will not appear, and if there is no such service field, then a new record will be added.

    The screenshot shows how I sent 2 different document definitions for processing, one condition worked and wrote a record to the database, and the other document had a different definition for which I did not have a condition and, accordingly, there is no record either. But at the same time, the record that was added, and if I submit this document for processing again, then after recognition the record will not be added to the database.

    I hope I understood you correctly and if any problems arise, I will try to help you

    0

Please sign in to leave a comment.