public static void CreateUserKey(string tableName, string keyName, string[] columnAliases, bool unique) { UserKeysMD userKeysMd = null; try { userKeysMd = SboApp.Company.GetBusinessObject(BoObjectTypes.oUserKeys) as UserKeysMD; userKeysMd.TableName = tableName; userKeysMd.KeyName = keyName; userKeysMd.Unique = unique ? BoYesNoEnum.tYES : BoYesNoEnum.tNO; foreach (var columnAlias in columnAliases) { userKeysMd.Elements.ColumnAlias = columnAlias; userKeysMd.Elements.Add(); } userKeysMd.Add(); } catch (Exception e) { SboApp.Application.SetStatusBarMessage("Couldn't create Key {keyName}", BoMessageTime.bmt_Long, true); SboApp.Logger.Error($"Create Key {tableName}.{keyName}. Possible cause: Table must be Empty. Error: {e.Message}", e); throw; } finally { if (userKeysMd != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(userKeysMd); } userKeysMd = null; GC.Collect(); } }
/// <summary> /// Adds the UserKeyMD to the current company Database. /// </summary> /// <param name="company">SAPbobsCOM.Company we are connected to.</param> /// <returns>Return value from the SDK action UserKeysMD.Add().</returns> public int Add(Company company) { UserKeysMD userKeys = null; int ret = -1; try { userKeys = (UserKeysMD) company.GetBusinessObject(BoObjectTypes.oUserKeys); userKeys.TableName = TableName; userKeys.KeyName = Name; userKeys.Unique = (IsUnique) ? BoYesNoEnum.tYES : BoYesNoEnum.tNO; //// elements bool flagFirst = true; foreach (string fieldName in Elements) { if (flagFirst) { flagFirst = false; } else { userKeys.Elements.Add(); } userKeys.Elements.ColumnAlias = fieldName; } ret = userKeys.Add(); #if DEBUG if (ret != 0) { int errcode; string errmsg; company.GetLastError(out errcode, out errmsg); System.Console.Out.WriteLine("Key " + Name + " : " + errmsg); } #endif } catch (Exception ex) { throw ex; } finally { // clean DI object System.Runtime.InteropServices.Marshal.ReleaseComObject(userKeys); userKeys = null; } return(ret); }