You might want to add some null checks in there to make sure you aren't erroring out prior because of null values.
Here's some code we use:
DataClassInfo dci = DataClassInfoProvider.GetDataClassInfo(<classID>);
if (dci != null)
{
FormInfo fi = new FormInfo(dci.ClassFormDefinition);
if (fi != null)
{
if (!fi.FieldExists(<yourFieldName>))
{
FormFieldInfo ffi1 = new FormFieldInfo()
{
Name = trackingFieldName,
AllowEmpty = false,
DefaultValue = "0",
System = true, // don't let them delete it
FieldType = FormFieldControlTypeEnum.CalendarControl,
Visible = false,
Caption = "Date sent to integration point",
Enabled = true,
DataType = "datetime",
Precision = 0
};
fi.AddFormItem(ffi1);
TableManager tm = new TableManager("CMSConnectionString");
tm.AddTableColumn(dci.ClassTableName, ffi2.Name, "datetime2", false, "0");
dci.ClassXmlSchema = tm.GetXmlSchema(dci.ClassTableName);
dci.ClassFormDefinition = fi.GetXmlDefinition();
FormHelper.UpdateInheritedClasses(dci);
}
}
}