Skip to Content

How to update valid values of an existing values UDF using C#

Hi All,

In my User Defined Filed 'U_WID_Test', already i have created 3 valid values, like example

  • Value Description
  • 1 A
  • 2 B
  • 3 C

Now. I'm trying update 4th valid value as value is 4 and Description is D.

  • 4 D

In this case in my valid value updated with last value, It means in the place of 3-C replaced with 3-D , but actually, I'm looking after updating valid value O/P like below

  • Value Description
  • 1 A
  • 2 B
  • 3 C
  • 4 D

Below mention UDF create and Update code

public void AddUdf(string tablename, string udfname, string desription, BoFieldTypes fieldType, BoFldSubTypes subType, int size, Dictionary<string, string> dictionary, string defaultvalue)
        {
            GC.Collect();
            var SboCompany = ServiceLocator.SboCompany;
            var udfs = new List<Udf>();
            var rs = (Recordset)SboCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            rs.DoQuery(string.Format("SELECT FieldID, AliasID FROM CUFD WHERE TableID = '{0}' OR TableID = '@{0}'", tablename));
            while (!rs.EoF)
            {
                udfs.Add(new Udf((int)rs.Fields.Item("FieldId").Value, (string)rs.Fields.Item("AliasID").Value));
                rs.MoveNext();
            }
            Marshal.ReleaseComObject(rs);
            var sboUdf = (UserFieldsMD)SboCompany.GetBusinessObject(BoObjectTypes.oUserFields);
            var ud = udfs.SingleOrDefault(x => x.Name.Equals(udfname));
            if (ud != null)
            {
                if (!sboUdf.GetByKey(tablename, ud.Id))
                {
                    sboUdf.GetByKey("@" + tablename, ud.Id);
                }
            }
            else
            {
                sboUdf.TableName = tablename;
                sboUdf.Name = udfname;
            }
            var vv = sboUdf.ValidValues;
            sboUdf.Description = desription;
            sboUdf.EditSize = size;
            sboUdf.Type = fieldType;
            sboUdf.SubType = subType;
            if (dictionary != null)
            {
                var valids = new List<string>();

                for (int i = 0; i < vv.Count; i++)
                {
                    vv.SetCurrentLine(i);
                    valids.Add(vv.Value);
                }

                foreach (var pair in dictionary)
                {
                    if (valids.Contains(pair.Key))
                    {
                        continue;
                    }
                    sboUdf.ValidValues.Value = pair.Key;
                    sboUdf.ValidValues.Description = pair.Value;
                    sboUdf.ValidValues.Add();
                }
            }
            if (defaultvalue != null)
            {
                //sboUdf.Mandatory = BoYesNoEnum.tYES;
                sboUdf.DefaultValue = defaultvalue;
            }
            var errorcode = ud != null ? sboUdf.Update() : sboUdf.Add();

            if (errorcode != 0)
            {
                Application.SBO_Application.SetStatusBarMessage(string.Format("{0} - {1}.{2}", ServiceLocator.SboCompany.GetLastErrorDescription(), tablename, udfname));
            }
            Marshal.ReleaseComObject(sboUdf);
            sboUdf = null;
            GC.Collect();
        }

Please suggest me. update valid values with existing values, with out replace values.

Thanks,
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Aug 29, 2017 at 04:24 PM

    Hi,

    Could you try to delete that UDF and add it again? It should have the valid value then.

    Kind regards,

    ANKIT CHAUHAN

    SAP SME Support

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Ankit,

      Thank you for your prompt replay,

      I think this is not a right way, Because, if i delete UDF and add it again, what about my previous data related UDF,

      I'm having a some old data in my UDF related Table, if i delete UDF, i lose all my previous data related UDF.

      Even i already tried the same way, delete UDF and add it again, but as i mention above lines, in this case i lose all previous data.

      Can you please suggest me any another option for updating valid values.

      Thanks & regards,

      Chenna.

  • Aug 30, 2017 at 06:34 AM

    Hi Chenna,

    If your scenario requires you to add values programmatically, I assume that new values will have to be added and or updated on a regular basis.

    In such a scenario you should create a User Defined Table, and link that table to the UDF. Now you may simple add, delete, and update records in the User Defined Table. You wouldn't necessarily even need to use the DI API to do so. It would make your code a lot simpler, and your life a lot easier.

    Regards,

    Johan

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Chenna,

      Remember, the linked table only holds all possible choices. The UDFs hold the actually chosen values.

      You could use a formatted search to show the required value in the second UDF, or if necessary, you can make your application fill in the value in the second UDF. It being a UDF, you can even use direct SQL to insert the value.

      Regards,

      Johan