Sample: Updating event handler method

The following C# example shows the Updating event handler method used to save the contact history information for a customer object. The event handler examines the Extension objects passed along with the customer. If one of the Extension objects has the ExtensionId value "ContactHistory", the event handler will process it. The code extracts the contact history information from XML element of the Extension object. After retrieving a connection to the current company's database and storing it in the connection private variable, the event handler executes a SQL statement to save the updated contact history information for the customer into the IG003 table.

// Declare private variable of type Microsoft.Dynamics.Common.Connection
private static Connection connection;

public static void UpdateContactHistory(object sender, BusinessObjectUpdateEventArgs e)
{
    bool found;
    int rowsAffected;
    string contact;
    string firstContactDate;
    string firstContactSalesperson;
    string lastContactDate;
    string lastContactSalesperson;
    string updateStatement;
    string insertStatement;

    Customer customer;
    Extension ContactHistoryExtension = new Extension();

    if (e.BusinessObject.GetType() == typeof(Customer))
    {
        customer = (Customer)e.BusinessObject;

        // Look at the Extension list passed along
        found = false;
        foreach (Extension ext in customer.Extensions)
        {
            if (ext.ExtensionId == "ContactHistory")
            {
                ContactHistoryExtension = ext;
                found = true;
                break;
            }
        }
        if (found == true)
        {
            // Found an extension, so it should be processed
            XmlElement contactHistory;
            contactHistory = ContactHistoryExtension.DocExtension;

            XmlNodeList nodeList;
            nodeList = contactHistory.ChildNodes;

            //First contact date
            firstContactDate = nodeList[0].InnerText.ToString();

            //First contact salesperson
            firstContactSalesperson = nodeList[1].InnerText.ToString();

            //Last contact date
            lastContactDate = nodeList[2].InnerText.ToString();

            //Last contact salesperson
            lastContactSalesperson = nodeList[3].InnerText.ToString();

            // Get the connection to the database for the current company
            connection = Connection.GetInstance();

            // The SQL statement to update contact history information
            updateStatement = "UPDATE IG003 SET ContactSalespersonID1='" +
            firstContactSalesperson
                + "', FirstContactDate='" + firstContactDate
                + "', ContactSalespersonID2='" + lastContactSalesperson
                + "', LastContactDate='" + lastContactDate
                + "' WHERE CUSTNMBR = '" + customer.Key.Id + "'";

            // Create the SQL connection
            SqlCommand command = new SqlCommand(updateStatement);
            SqlConnection sqlConnection = new SqlConnection
            (connection.GetConnectionString(e.Context.OrganizationKey));
            command.Connection = sqlConnection;

            // Open the SQL connection
            sqlConnection.Open();

            // Execute the SQL statement
            rowsAffected = command.ExecuteNonQuery();

            if (rowsAffected == 0)
            {
                // The row did not exist, so try creating it.

                // Has a ContactPerson been specified? If not, set it to empty.
                if (customer.Addresses.Count == 0)
                    contact = "";
                else
                    contact = customer.Addresses[0].ContactPerson;

                insertStatement = "INSERT IG003 VALUES ('"
                    + firstContactSalesperson + "', '"
                    + lastContactSalesperson + "', '"
                    + contact
                    + "', '" + lastContactDate + "', '"
                    + firstContactDate + "', '" + customer.Name
                    + "', '" + customer.Key.Id + "')";
                command.CommandText = insertStatement;
                rowsAffected = command.ExecuteNonQuery();
            }

            // Close the SQL connection
            sqlConnection.Close();
        }
    }
}