cancel
Showing results for 
Search instead for 
Did you mean: 

user defined fields via DI-API

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Nussi
Active Contributor
0 Kudos

Hi Frank,

ich versteh ehrlich gesagt nicht warum Du nicht

oItem.UserFields.Fields.Item("U_FIELDNAME").Value

verwendest um darauf zuzugreifen ? dann brauchst Du den Index nicht ...

lg David

Former Member
0 Kudos

Hallo David,

zunächst mal 10 Punkte für Dich!

Zu Deiner Frage ("ich versteh ehrlich gesagt nicht warum ..."): Ganz einfach, weil ich gerade neu mit dem DI anfange und der Begriff 'Index' mich automatisch nur an int hat denken lassen ... das zum Thema selbsterklärende Namen ...

Danke!

Gruß Frank

Answers (1)

Answers (1)

Former Member

Dear Frank Romeni,

UserFieldsMD is a business object that enables you to mange user defined fields.

The main function like add, update ... a UDF.

It is not a user fields collection object and you could not use it for your UDF retrieve requirement.

The basic way to access a UDF is first get the object, such as Item object,

then get the UDF belong the object by item.UserFields.

Best Regards

Jane Jing

SAP Business One Forums team

Edited by: Jane Jing on Sep 5, 2008 9:10 AM