private void WriteChildRecordResponse() { string response = ""; if (TABLE_MAPPINGS.ContainsKey(Table) && BusinessObjectFactory.CanBuildBusinessObject(Table) && PriKey.HasValue) { string childTable = TABLE_MAPPINGS[Table]; if (BusinessObjectFactory.CanBuildBusinessObject(childTable)) { int patientId = int.Parse(Session[SessionKey.PatientId].ToString()); // load parent BusinessObject parent = BusinessObjectFactory.BuildBusinessObject(Table); parent.Get(PriKey.Value); if (!parent.IsEmpty) { // get prefix (i.e. "MedTx", "RadTx") string prefix = childTable.Substring(0, childTable.IndexOf("Tx") + 2); // turn on validation of patient parent.EnableSaveValidation(patientId); // create child instance BusinessObject child = BusinessObjectFactory.BuildBusinessObject(childTable); child.EnableSaveValidation(patientId); // set foreign key child[child.ParentKeyName] = parent[parent.PrimaryKeyName]; // set fields foreach (string field in COPY_FIELDS) { string parentField = prefix + field; string childField = prefix + "Admin" + field; if (parent.FieldNames.Contains(parentField) && child.FieldNames.Contains(childField)) { child[childField] = parent[parentField]; } } // sepcial date fields DateTime date = QueryDate.HasValue ? QueryDate.Value : DateTime.Today; child[prefix + "AdminStartDateText"] = date.ToShortDateString(); child[prefix + "AdminStartDate"] = date; // insert child.Save(); response = "- Administered " + (DateTime.Today.Date == date.Date ? "Today" : date.ToShortDateString()) + " -"; } } } Response.Write(response); }