Adding UDF per C# Code by Optimizer
Sebastian Schweer
Hallo,i have written a skript for the Coresuit Optimizer which added some existing UDF on the Form "Item Master Data". That's already worked.
But now, i want to check if the UDFs already exist. And if they don't exist to adding them. But then there ocure an error in the optimizer. I used the following test Script:
SAPbobsCOM.UserFieldsMD userFields = (SAPbobsCOM.UserFieldsMD) SwissAddonFramework.B1Connector.GetB1Connector().Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
userFields.TableName = "OITM";
userFields.Name = "UDF_TEST_AAA";
userFields.Description = "UDF_TEST_AAAA";
userFields.Type = SAPbobsCOM.BoFieldTypes.db_Alpha;
userFields.EditSize = 20;
int errCode = userFields.Add();
This Script works, if i use a Funktion Button or an Menu. But it will not work, if i embedding it in the optimizer. Can anybody can tell me why?
I use the following options:
Type: CSCode
Action: Validate
Only Waring: false
Warning Box: false
Statusbarmessage: false
Form Type: 150
Event Type: FormLoad
Before Event: False
Form Mode: All
Thanks for the Help,
Sebastian
Paolo Manfrin
Hi Sebastian,you can check if the UDF exists using a query like:
SELECT COUNT(*) FROM CUFD WHERE TableID = 'OITM' AND AliasID = 'UDF_TEST_AAAA'
The script you wrote is correct but of course it raises an exception the second time you run it if the UDT already exists.
Check with:
try
{
SAPbobsCOM.UserFieldsMD userFields = (SAPbobsCOM.UserFieldsMD) SwissAddonFramework.B1Connector.GetB1Connector().Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
userFields.TableName = "OITM";
userFields.Name = "UDF_TEST_AAA";
userFields.Description = "UDF_TEST_AAAA";
userFields.Type = SAPbobsCOM.BoFieldTypes.db_Alpha;
userFields.EditSize = 20;
int errCode = userFields.Add();
if (errCode != 0)
{
string errMsg;
SwissAddonFramework.B1Connector.GetB1Connector().Company.GetLastError(out errCode, out errMsg);
StatusBar.WriteError(errCode + " - " + errMsg);
}
else
StatusBar.WriteSucess("rule fired");
}
catch (Exception ex)
{
SwissAddonFramework.Messaging.StatusBar.WriteError(ex.Message);
}
return true;
The second time you run it you would get the error:
-2035 - This entry already exists in the following tables (ODBC -2035)
hth
paolo
Sebastian Schweer
Hallo Paolo,thanks a lot. That works.
I´d kown the Table CUFD and don`t run the skript twice. But haven't had add the try-catch-Block. And without them, i got a coresuite customize compiler error. I picured this error in the following screenshot, just for your information.
But with the try catch Block it works well.
Thanks,
Sebastian
Attachment
[url=http://www.coresystems.ch/wp-content/../wp-content/forum-image-uploads/sebastianschweer/CoresuiteCompilerError.JPG]CoresuiteCompilerError.JPG[/url]
0
Please sign in to leave a comment.
Comments
0 comments