Skip to Content
0

Adding a UDF via C#

Jun 26, 2017 at 06:21 PM

238

avatar image
Former Member

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;
            oDefAcc.Add();
            //add valid values
            oDefAcc.ValidValues.Value = "Y";
            oDefAcc.ValidValues.Description = "Yes";
            oDefAcc.ValidValues.Add();
            oDefAcc.ValidValues.Value = "N";
            oDefAcc.ValidValues.Description = "No";
            oDefAcc.ValidValues.Add();
    }

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)
        {
            try
            {
                Application oApp = null;
                if (args.Length < 1)
                {
                    oApp = new Application();
                }
                else
                {
                    oApp = new Application(args[0]);
                }


                Menu MyMenu = new Menu();
                MyMenu.AddMenuItems();
                oApp.RegisterMenuEventHandler(MyMenu.SBO_Application_MenuEvent);
                Application.SBO_Application.AppEvent += new SAPbouiCOM._IApplicationEvents_AppEventEventHandler(SBO_Application_AppEvent);
                oApp.Run();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            AddUserFields_CoA();
        }

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
1

Hi,

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.ValidValues.Value="Y";
            oDefAcc.ValidValues.Description="Yes";
            oDefAcc.ValidValues.Add();
            oDefAcc.ValidValues.Value="N";
            oDefAcc.ValidValues.Description="No";
            oDefAcc.ValidValues.Add();
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
Former Member

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. :(

0

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

int err = oDefAcc.Add();

string errdesc = oCompany.GetLastErrorDescription()

0
Former Member
Danilo Kasparian

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);


            //mwa_RR_DefAcc
            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.Add();
            oRRDefAcc.ValidValues.Value = "N";
            oRRDefAcc.ValidValues.Description = "No";
            oRRDefAcc.ValidValues.Add();
            oRRDefAcc.Add();


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

0
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.

0