Skip to Content
avatar image
Former Member

Adding a UDF via C#

I have tried to create a simple method to add a UDF onto the chart of accounts, but it is not working.

Here is the code of my method:

        static void AddUserFields_CoA()
            //define our company
            SAPbobsCOM.Company oCompany = (SAPbobsCOM.Company)Application.SBO_Application.Company.GetDICompany();

            //Add UDF to OACT table for DefAcc
            SAPbobsCOM.UserFieldsMD oDefAcc = ((SAPbobsCOM.UserFieldsMD)(oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)));
            oDefAcc.TableName = "OACT";
            oDefAcc.Name = "DefAcc";
            oDefAcc.Description = "Is Deferral Account?";
            oDefAcc.Type = SAPbobsCOM.BoFieldTypes.db_Alpha; 
            oDefAcc.EditSize = 1;
            //add valid values
            oDefAcc.ValidValues.Value = "Y";
            oDefAcc.ValidValues.Description = "Yes";
            oDefAcc.ValidValues.Value = "N";
            oDefAcc.ValidValues.Description = "No";

I added the method to my Main method so it looks like this (note: my "AddUserFields_CoA" method is withing the same Program class):

static void Main(string[] args)
                Application oApp = null;
                if (args.Length < 1)
                    oApp = new Application();
                    oApp = new Application(args[0]);

                Menu MyMenu = new Menu();
                Application.SBO_Application.AppEvent += new SAPbouiCOM._IApplicationEvents_AppEventEventHandler(SBO_Application_AppEvent);
            catch (Exception ex)

The code runs without any issues (in debug mode), but it is not creating the UDF on the chart of accounts for me.

Most of the examples that I've seen call the class for UserFieldsMD oUserFieldsMD, but eventually I'm going to be adding a significant number of UDF's, and thought it would be better to have separate names for each UDF being added.

Would it be better to create an "Add UDF" method with parameters that I fill in for the information, and call that method repeatedly, rather than adding a method to add the fields to each table? The reason that I opted out of doing that is because I need to have some valid values on a few of the UDF's that I want to add.

Edit: Edited to remove unnecessary comments from the code.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Jun 26, 2017 at 07:56 PM


    You need to add your ValidValues before adding the field. Try to remove your field manually and run doing the changes below and see if it works.

    oDefAcc.Add();//addvalidvalues ---------------THIS LINE GOES AT THE END, IT WAS AT THE BEGINNING.
    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Danilo Kasparian

      It looks like it fails to add the UDF most of the time, but it worked some of the times that I tried it when I had the User-Defined Fields menu open in the background.

      Edit: Looks like it's all working correctly! It's just taking forever to add.