on 09-04-2008 6:46 PM
I want to read user defined fields using the DI-API.
The following code works but lists only UDFs defined for articles:
SAPbobsCOM.Items item =
(SAPbobsCOM.Items) GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems);
int count = item.UserFields.Fields.Count;
MessageBox.Show("count == "+anzahl.ToString(), "OK");
for(int i=0; i<count; i++){
MessageBox.Show("index == "+i.ToString(), "OK");
MessageBox.Show("name == "+item.UserFields.Fields.Item(i).Name, "OK");
}
If I try to list all UDFs defined in the system using the oUserFields object I get an exception when I assign the business object:
try{
SAPbobsCOM.UserFields ufd = (SAPbobsCOM.UserFields)
GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields); // exception
int count = ufd.Fields.Count;
...
}
catch(System.Exception ex){
MessageBox.Show("ex.ToString(), "OK");
}
The exception says something like
InvalidCastException: COM object of type 'System.__ComObject' can not casted into
interface type SAPbobsCOM.UserFields
Thank you for help,
Frank Romeni
Frank,
The BoObjectTypes.oItems represents the Items object (Main MenuStock ManagementItem Master Data).
the BoObjectTypes.oUserFields is connected to the UserFieldsMD object.
Can you explain what you are trying to do?
Regards,
Vítor Vieira
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Vítor,
thank you for the hint to UserFieldsMD - now the access works.
You wanted to know what I am trying to do - let me explain the background even it is a bit complicated:
I have to access a certain UDF defined for articles.
To access this UDF I can't use a fixed index like e.g. '5' in
item.UserFields.Fields.Item(5).Value
because this index is 5 only on my local machine - it could be a different index on the target machine.
I solved this in writing some sql-code to access table CUFD and to find the index of this UDF in CUFD.FieldID.
But sometimes there is a problem when there are UDFs deleted from the database. It is possible that there are 'holes' between the FieldId of UDFs of one object, e.g.
Initial entries in CUFD (Table, FieldId, Name => code to access field):
...
OITM 0 'myUDF1' ==> item.UserFields.Fields.Item(0).Value
OITM 1 'myUDF2' ==> item.UserFields.Fields.Item(1).Value
OITM 2 'myUDF3' ==> item.UserFields.Fields.Item(2).Value
...
After deletion of 'myUDF2':
...
OITM 0 'myUDF1'
OITM 2 'myUDF3'
...
Now the access to 'myUDF2' with item.UserFields.Fields.Item(2).Value fails!
You have to use index '1' in .Item(index) to access 'myUDF2' because this UDF is now the second UDF in the item object (zero based).
After I realized this I didn't use the sql-code to get FieldID any longer and searched with a loop all existing indices and compared them with the name of my special UDF, e.g. (this code works as expected):
public int getUDFIndex(string udfName){
index = -1;
for(int i=0; i<item.UserFields.Fields.Count; i++{
if(item.UserFields.Fields.Item(i).Name == udfName){
index = i;
break;
}
}
return index;
Now I tried to make this method more general to find UDFs in any object - not only in item objects.
This is the background I wanted to access SAPbobsCOM.UserFields for.
The problem is that UserFieldsMD has no method like Item(index) as I used it in my example.
Do you have an idea to solve the problem with the 'holes' between FieldId in the UFD-table CUFD?
Frank Romeni
User | Count |
---|---|
104 | |
12 | |
11 | |
6 | |
5 | |
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.