on 06-26-2017 7:21 PM
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
}
User | Count |
---|---|
98 | |
12 | |
10 | |
6 | |
6 | |
4 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.