コード例 #1
0
        /// <summary>
        /// Create field on table
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="fieldDescription"></param>
        /// <param name="type"></param>
        /// <param name="size"></param>
        /// <param name="editSize"></param>
        /// <param name="subType"></param>
        /// <param name="validValues">Dropdown values</param>
        /// <param name="defaultValue"></param>
        /// <param name="linkedTable"> use '@' with usertables</param>
        /// <returns></returns>
        public static void CreateField(string tableName, string fieldName, string fieldDescription,
                                       BoFieldTypes type = BoFieldTypes.db_Alpha, int size = 50, BoFldSubTypes subType = BoFldSubTypes.st_None,
                                       IDictionary <string, string> validValues = null, string defaultValue = null,
                                       string linkedTable = null, int editSize = 0)
        {
            UserFieldsMD userFieldsMd = null;

            try
            {
                userFieldsMd = SboApp.Company.GetBusinessObject(BoObjectTypes.oUserFields) as UserFieldsMD;

                if (userFieldsMd == null)
                {
                    throw new NullReferenceException("Failed to get UserFieldsMD object");
                }

                var fieldId = GetFieldId(tableName, fieldName);
                if (fieldId != -1)
                {
                    return;
                }

                userFieldsMd.TableName    = tableName;
                userFieldsMd.Name         = fieldName;
                userFieldsMd.Description  = fieldDescription;
                userFieldsMd.Type         = type;
                userFieldsMd.SubType      = subType;
                userFieldsMd.Size         = size;
                userFieldsMd.EditSize     = editSize != 0 ? editSize : size;
                userFieldsMd.DefaultValue = defaultValue;

                if (validValues != null)
                {
                    foreach (var validValue in validValues)
                    {
                        userFieldsMd.ValidValues.Value       = validValue.Key;
                        userFieldsMd.ValidValues.Description = validValue.Value;
                        userFieldsMd.ValidValues.Add();
                    }
                }

                if (linkedTable != null)
                {
                    if (DatabaseHelper.TableExists(linkedTable))
                    {
                        userFieldsMd.LinkedTable = linkedTable.Trim('@');
                    }
                    else
                    {
                        throw new Exception($"Linked table '{linkedTable}' could not be found");
                    }
                }

                ErrorHelper.HandleErrorWithException(userFieldsMd.Add(), "Could not create field");
            }
            catch (Exception ex)
            {
                SboApp.Logger.Error($"Create Field {tableName}.{fieldName} Error: {ex.Message}", ex);
                throw;
            }
            finally
            {
                if (userFieldsMd != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(userFieldsMd);
                }
                userFieldsMd = null;
                GC.Collect();
            }
        }