Sample: Retrieved event handler method
The following C# example shows the Retrieved event handler method used to retrieve contact history information associated with a customer object. The event handler retrieves the customer object from the event arguments passed in. After retrieving a connection to the current company's database and storing it in the connection private variable, it executes a SQL statement to retrieve the contact history information for the customer from the IG003 table. Finally, it builds the XML element that will contain the contact history information. It adds the XML element to an Extension object in the Extensions collection the customer object.
// Declare private variable of type Microsoft.Dynamics.Common.Connection private static Connection connection; public static void GetContactHistory(object sender, BusinessObjectEventArgs e) { string firstContactDate; string firstContactSalesperson; string lastContactDate; string lastContactSalesperson; XmlDocument doc; XmlElement contactHistoryXML; XmlElement firstContactDateXML; XmlElement firstContactSalespersonXML; XmlElement lastContactDateXML; XmlElement lastContactSalespersonXML; XmlText text; Customer customer; if (e.BusinessObject.GetType() == typeof(Customer)) { customer = (Customer)e.BusinessObject; // Get the connection to the database for the current company connection = Connection.GetInstance(); // The SQL command to retrieve contact history information string selectCommand = "SELECT FirstContactDate, ContactSalespersonID1, LastContactDate, ContactSalespersonID2 FROM IG003 WHERE CUSTNMBR='" + customer.Key.Id + "'"; SqlDataAdapter adapter = new SqlDataAdapter(selectCommand, (SqlConnection)connection.GetConnection(e.Context.OrganizationKey)); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { // Get the data from the SQL result firstContactDate = table.Rows[0].ItemArray[0].ToString(); firstContactSalesperson = table.Rows[0].ItemArray[1].ToString(); lastContactDate = table.Rows[0].ItemArray[2].ToString(); lastContactSalesperson = table.Rows[0].ItemArray[3].ToString(); // Build the Extension object to return from the service Extension ContactHistory = new Extension(); ContactHistory.ExtensionId = "ContactHistory"; // Make the XML extension document doc = new XmlDocument(); contactHistoryXML = doc.CreateElement("ContactHistory"); // First Contact Date firstContactDateXML = doc.CreateElement("FirstContactDate"); text = doc.CreateTextNode(firstContactDate); firstContactDateXML.AppendChild(text); contactHistoryXML.AppendChild(firstContactDateXML); // First Contact Salesperson firstContactSalespersonXML = doc.CreateElement("FirstContactSalesperson"); text = doc.CreateTextNode(firstContactSalesperson); firstContactSalespersonXML.AppendChild(text); contactHistoryXML.AppendChild(firstContactSalespersonXML); // Last Contact Date lastContactDateXML = doc.CreateElement("LastContactDate"); text = doc.CreateTextNode(lastContactDate); lastContactDateXML.AppendChild(text); contactHistoryXML.AppendChild(lastContactDateXML); // Last Contact Salesperson lastContactSalespersonXML = doc.CreateElement("LastContactSalesperson"); text = doc.CreateTextNode(lastContactSalesperson); lastContactSalespersonXML.AppendChild(text); contactHistoryXML.AppendChild(lastContactSalespersonXML); // Add the extension to the Customer object ContactHistory.DocExtension = contactHistoryXML; e.BusinessObject.Extensions.Add(ContactHistory); } } }