Sample: ValidatingForCreate event handler method

The following C# example shows the ValidatingForCreate event handler method used to validate contact history information that is being added as extended data 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. After extracting the XML element from the Extension object, the two Salesperson ID values included in the extended data are validated. If the Salesperson ID values cannot be validated, validation errors are added to the ValidationResults object that was passed into the event handler.

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

public static void ValidateCreateContactHistory(object sender, BusinessObjectValidateEventArgs e)
{
    bool found;
    string firstContactDate;
    string firstContactSalesperson;
    string lastContactDate;
    string lastContactSalesperson;
    string selectStatement;
    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();
            SqlCommand command = new SqlCommand();
            command.Connection = (SqlConnection)connection.GetConnection
            (e.Context.OrganizationKey);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable table = new DataTable();

            // Verify that the First Contact Salesperson and Last Contact
            // Salesperson are valid

            // The SQL statement to verify the First Contact Salesperson
            selectStatement = "SELECT SLPRSNID FROM RM00301 WHERE SLPRSNID =
            '" + firstContactSalesperson + "'";
            command.CommandText = selectStatement;
            adapter.Fill(table);

            if (table.Rows.Count < 1)
            {
                // Add an exception, because the First Contact salesperson was
                // not found
                ValidationError validationError = new ValidationError();
                validationError.Id = "1001";
                validationError.Message = "Invalid First Contact Salesperson
                specified in Contact History.";
                validationError.ObjectType = typeof(Customer).ToString();
                validationError.PropertyNames.Add("FirstContactSalesperson");
                e.ValidationResult.Errors.Add(validationError);
            }

            // The SQL statement to verify the Last Contact Salesperson
            selectStatement = "SELECT SLPRSNID FROM RM00301 WHERE SLPRSNID =
            '" + lastContactSalesperson + "'";
            command.CommandText = selectStatement;
            adapter.SelectCommand = command;

            table.Clear();
            adapter.Fill(table);
            if (table.Rows.Count < 1)
            {
                // Add an exception, because the Last Contact salesperson was
                // not found
                ValidationResult validationResult = new ValidationResult();
                ValidationError validationError = new ValidationError();
                validationError.Id = "1002";
                validationError.Message = "Invalid Last Contact Salesperson
                specified in Contact History.";
                validationError.ObjectType = typeof(Customer).ToString();
                validationError.PropertyNames.Add("LastContactSalesperson");
                e.ValidationResult.Errors.Add(validationError);
            }
        }
    }
}