public static string CreateField(string tablename, string fieldname, string description, BoFieldTypes type, int size, bool isMandatory, bool isSapTable = false, string likedToTAble = "", string defaultValue = "", BoFldSubTypes subType = BoFldSubTypes.st_None) { // Get a new Recordset object Recordset oRecordSet = (Recordset)Company.GetBusinessObject(BoObjectTypes.BoRecordset); string sqlQuery = $"SELECT T0.TableID, T0.FieldID FROM CUFD T0 WHERE T0.TableID = '@{tablename}' AND T0.AliasID = '{fieldname}'"; oRecordSet.DoQuery(sqlQuery); var updateFlag = oRecordSet.RecordCount == 1; var fieldId = int.Parse(oRecordSet.Fields.Item("FieldID").Value.ToString()); System.Runtime.InteropServices.Marshal.ReleaseComObject(oRecordSet); UserFieldsMD oUfield = (UserFieldsMD)Company.GetBusinessObject(BoObjectTypes.oUserFields); if (updateFlag) { return(""); //oUfield.GetByKey(tablename, fieldId); } try { oUfield.TableName = tablename; oUfield.Name = fieldname; oUfield.Description = description; oUfield.Type = type; oUfield.Mandatory = isMandatory ? BoYesNoEnum.tYES : BoYesNoEnum.tNO; oUfield.DefaultValue = defaultValue; if (type == BoFieldTypes.db_Float) { oUfield.SubType = subType; } if (type == BoFieldTypes.db_Alpha || type == BoFieldTypes.db_Numeric) { oUfield.EditSize = size; } oUfield.LinkedTable = likedToTAble; int ret = updateFlag ? oUfield.Update() : oUfield.Add(); if (ret == 0) { System.Runtime.InteropServices.Marshal.ReleaseComObject(oUfield); return(string.Empty); } else { System.Runtime.InteropServices.Marshal.ReleaseComObject(oUfield); return(Company.GetLastErrorDescription()); } } catch (Exception e) { return($"exeption : {e.Message}, Sap Error {Company.GetLastErrorDescription()}"); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(oUfield); } }
public static void UpdateFieldSettings(string tableName, string fieldAlias, string fieldDescription, string linkedTable) { UserFieldsMD userFieldsMd = null; try { userFieldsMd = SboApp.Company.GetBusinessObject(BoObjectTypes.oUserFields) as UserFieldsMD; if (userFieldsMd == null) { return; } var fieldId = GetFieldId(tableName, fieldAlias); if (fieldId == -1) { return; } if (!userFieldsMd.GetByKey(tableName, fieldId)) { return; } bool changed = false; if (linkedTable != null && linkedTable.Trim('@') != userFieldsMd.LinkedTable.Trim('@')) { if (DatabaseHelper.TableExists(linkedTable)) { userFieldsMd.LinkedTable = linkedTable.Trim('@'); } else { throw new Exception($"Linked table '{linkedTable}' could not be found"); } changed = true; } if (userFieldsMd.Description != fieldDescription) { userFieldsMd.Description = fieldDescription; changed = true; } if (changed) { userFieldsMd.Update(); } } catch (Exception ex) { SboApp.Logger.Error($"Failed updating field: {ex.Message}", ex); throw; } finally { if (userFieldsMd != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(userFieldsMd); } userFieldsMd = null; GC.Collect(); } }
/// <summary> /// Increase UserField Size /// </summary> /// <param name="tableName"></param> /// <param name="fieldAlias"></param> /// <param name="size"></param> /// <param name="editSize"></param> /// <returns></returns> public static void IncreaseUserFieldSize(string tableName, string fieldAlias, int size, int editSize = 0) { UserFieldsMD userFieldsMd = null; try { userFieldsMd = SboApp.Company.GetBusinessObject(BoObjectTypes.oUserFields) as UserFieldsMD; if (userFieldsMd == null) { return; } var fieldId = GetFieldId(tableName, fieldAlias); if (fieldId == -1) { return; } if (!userFieldsMd.GetByKey(tableName, fieldId)) { return; } bool changed = false; if (userFieldsMd.Size < size && userFieldsMd.EditSize < editSize) { if (editSize == 0) { editSize = size; } userFieldsMd.Size = size; userFieldsMd.EditSize = editSize; changed = true; } else if (userFieldsMd.Size < size) { userFieldsMd.Size = size; changed = true; } else if (userFieldsMd.EditSize < editSize) { userFieldsMd.EditSize = editSize; changed = true; } if (changed) { userFieldsMd.Update(); } } catch (Exception ex) { SboApp.Logger.Error($"Increase User Field Size Error: {ex.Message}", ex); throw; } finally { if (userFieldsMd != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(userFieldsMd); } userFieldsMd = null; GC.Collect(); } }