Skip to Content

Adding a UDF via C#

Jun 26, 2017 at 06:21 PM


avatar image

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Danilo Kasparian 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.

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

If I just completely comment out the ValidValues bits, it still doesn't add it at all. It feels like the method is just not getting called by Main for some reason.

I also tried it with your updated code, and it still didn't add the field, either. :(


Use the return code when adding to see the error, and post the results you have.

int err = oDefAcc.Add();

string errdesc = oCompany.GetLastErrorDescription()


I changed up my strategy. Instead of running the code when the add-on loads, I've added it to a button on a window I created. The code runs successfully (when I put a break point on each line, it hits the break point), but it doesn't create the fields that I expect it to create, even though it should.

When I click the button, it looks like everything is running properly (it stops at each line when I put break points on them), but it doesn't actually create the field.

Here's the code to (one of) the buttons:

        private void OACTButton_ClickBefore(object sboObject, SAPbouiCOM.SBOItemEventArg pVal, out bool BubbleEvent)
            BubbleEvent = true;
            //define our company name
            SAPbobsCOM.Company oCompany = (SAPbobsCOM.Company)Application.SBO_Application.Company.GetDICompany();
            //Let us know that we're doing something
            Application.SBO_Application.SetStatusBarMessage("Adding Fields to OACT. Please wait.", SAPbouiCOM.BoMessageTime.bmt_Short, false);

            SAPbobsCOM.UserFieldsMD oRRDefAcc = (SAPbobsCOM.UserFieldsMD)oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
            //Let us know that we're adding this field
            Application.SBO_Application.SetStatusBarMessage("Adding mwa_RR_DefAcc", SAPbouiCOM.BoMessageTime.bmt_Short, false);
            oRRDefAcc.TableName = "OACT";
            oRRDefAcc.Name = "mwa_RR_DefAcc";
            oRRDefAcc.Description = "Is Deferral Account?";
            oRRDefAcc.Type = SAPbobsCOM.BoFieldTypes.db_Alpha;
            oRRDefAcc.EditSize = 1;
            oRRDefAcc.ValidValues.Value = "Y";
            oRRDefAcc.ValidValues.Description = "Yes";
            oRRDefAcc.ValidValues.Value = "N";
            oRRDefAcc.ValidValues.Description = "No";

            //Let us know that we're adding this field
            Application.SBO_Application.SetStatusBarMessage("Adding fields to OACT complete.", SAPbouiCOM.BoMessageTime.bmt_Short, false);


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.