Skip to Content
0

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

Aug 29, 2017 at 01:22 PM

338

avatar image

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,
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

ANKIT CHAUHAN
Aug 29, 2017 at 04:24 PM
0

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

Show 1 Share
10 |10000 characters needed characters left 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.

0
Johan Hakkesteegt Aug 30, 2017 at 06:34 AM
0

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

Show 5 Share
10 |10000 characters needed characters left characters exceeded

Hi Johan,

Thank you for your replay,

My requirement is something different with your suggestion,

Actually i have created two UDF's into two different Table's, in my First UDF, i have already linked the related Table into first UDF. Now in my UDF(linked table list), i have selected one value and click save, the data will save into my first Table along with UDF value data.
Now, i open another window, in this window, I'm shows second User Defined Filed , in this User Defined Filed, the Standard value( from First UDF value shall be per-filled) , along with linked table list. User can change value with drop down list.

Window 1;

this my Window 1, linked Table into first User defined Filed , here selected sample value and save the details.

Window 2 (Open another Form)

when open Window 2, the standard selected value(from the window 1 as 'sample') per-filled into

drop-down list and here user can change value with drop down list.

In this case, when user click save details in First Window 1 , i have update the 2 UDF details valid values with default selected value.

Thanks,

Chenna

udf1.png (5.0 kB)
udf2.png (5.1 kB)
0

Hi Chenna,

The solution I recommended will work equally well for this scenario. In this case you should simply link the same table to both UDFs. That way, you don't even have to code anything at all.

Regards,

Johan

0

Hi,

Any one having a solution, regarding my requirement.

Thanks,

0

Hi Johan,

If i link the same table into both UDFs, how can i set the default pre-filled valid value into 2nd UDF.

It means, If i link the table into UDF, when open a SAP B1 form or window, by default UDF shows blank drop down list, with liked table data into drop-down list. But in my case, i want to show UDF with pre-filled value selected into drop-down list along with linked table details.

Thanks,

Chenna

0

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

0