Can't remove a user defined field with DI API.

Hi,
I'm using the code below to remove a user field.  However, the first time I run it, I get the message that:
"Ref count for this object is higher then 0"
The second time I run it, I get the message that:
"No matching records found (ODBC -2028)"
As you can see, I am killing the UserFieldsMD object.  In fact, when the function is entered, the object is nothing but later instantiated with oCompany.
The result in both cases is that the field doesn't get removed.  What am I missing?  Any ideas?
Thanks,
Mike
    Public Function RemoveUserField(ByVal FieldName As String, ByVal TableName As String, ByRef ErrMsg As String) As Boolean
        Dim Result As Boolean = False
        Dim Err As Integer
        Dim oUserFieldsMD As SAPbobsCOM.UserFieldsMD
        Try
            If Not FieldExists(TableName, FieldName) Then
                Result = True
            Else
                oUserFieldsMD = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)
                With oUserFieldsMD
                    .TableName = TableName
                    .Name = FieldName
                    Err = .Remove
                    If Err Then
                        oUtilCompany.GetLastError(Err, ErrMsg)
                        If ErrMsg = "No matching records found (ODBC -2028)" Then
                            Result = True
                        Else
                            ErrMsg = "Was not able to remove field " & FieldName & " from table " & TableName & " programmatically." & vbCrLf & _
                                     "Please remove it manually using the Tools menu."
                        End If
                    Else
                        Result = True
                    End If
                End With
            End If
        Catch ex As Exception
            Throw ex
        Finally
            KillObject(oUserFieldsMD)
        End Try
        Return Result
    End Function
    Public Sub KillObject(ByRef Obj As Object)
        If Not Obj Is Nothing Then ObjectRelease(Obj)
    End Sub
    Private Sub ObjectRelease(ByRef Obj As Object)
        'This routine releases objects that were acquired with oCompany.GetBusinessObject().
        System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj)
        Obj = Nothing
        GC.Collect()
    End Sub

Hi Mike,
The issue with your code is that you are not calling the GetByKey method of the field object. Until you do this the FieldID property is 0 so when the Remove method is called it will not try and remove the correct UDF (if you run SQL profiler then you'll see that SBO is looking for the UDF based on the table and fieldID values).
Something like the following works for me:
private void DeleteUDF(string sTableID, string sFieldName)
    SAPbobsCOM.UserFieldsMD sboField = (SAPbobsCOM.UserFieldsMD)_sboCompany.GetBusinessObject(BoObjectTypes.oUserFields);
    try
        int iFieldID = GetFieldID(sTableID, sFieldName);
        if(sboField.GetByKey(sTableID, iFieldID))
            if (sboField.Remove() != 0)_sboApp.SetStatusBarMessage("Error removing UDF: " + _sboCompany.GetLastErrorDescription(), SAPbouiCOM.BoMessageTime.bmt_Short, true);
    finally
        System.Runtime.InteropServices.Marshal.ReleaseComObject(sboField);
        sboField = null;
        GC.Collect();
private int GetFieldID(string sTableID, string sAliasID)
    int iRetVal = 0;
    SAPbobsCOM.Recordset sboRec = (SAPbobsCOM.Recordset)_sboCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
    try
        sboRec.DoQuery("select FieldID from CUFD where TableID = '" + sTableID + "' and AliasID = '" + sAliasID + "'");
        if (!sboRec.EoF) iRetVal = Convert.ToInt32(sboRec.Fields.Item("FieldID").Value.ToString());
    finally
        System.Runtime.InteropServices.Marshal.ReleaseComObject(sboRec);
        sboRec = null;
        GC.Collect();
    return iRetVal;
Note that the parameter sAliasID is the field name without the U_ prefix.
Kind Regards,
Owen

Similar Messages

  • Error -5002 when adding linked user defined field using DI API

    Hello,
    When I try to add a linked user defined field using DI API I get the error number -5002 with description:
    "The field 'Related Table' should consist of 8 alphanumeric characters with no valid or default values"
    I Get the error when I use the Add method.
    What is the solution for this problem? I use SBO 2005 A SP1 Patch 18
    The code I use is (.NET C# 2.0):
    SAPbobsCOM.IUserFieldsMD uf = (SAPbobsCOM.IUserFieldsMD)company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
    uf.Name = "S_BUCO";
    uf.TableName = "OPOR";
    uf.Type = SAPbobsCOM.BoFieldTypes.db_Memo;
    uf.SubType = SAPbobsCOM.BoFldSubTypes.st_Link;
    uf.LinkedTable = "S_BU";
    uf.Description = "Description";
    uf.Add()
    Regards,
    Jeffrey

    Hi Jeffrey,
    Your code above does not match the settings you are using in the UI. In particular, the type and subtype you are setting in code are not correct.
    To create the UDF via code, set the field types as follows:
    SAPbobsCOM.IUserFieldsMD uf = (SAPbobsCOM.IUserFieldsMD)company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields);
    uf.Name = "S_BUCO";
    uf.TableName = "OPOR";
    uf.Type = SAPbobsCOM.BoFieldTypes.db_Alpha;
    uf.EditSize = 8;
    uf.LinkedTable = "S_BU";
    uf.Description = "Description";
    uf.Add()
    There's no need to set the SubType property as you require a regular alphanumeric field.
    Kind Regards,
    Owen

  • Opening User defined Fields Form via API

    Hi
    Is there a posiibility to open those user defined field - forms via API. If yes is it possible to check if it is already open?
    TIA
    Phile

    Hi,
    In your eventhandler, you can use this:
    Set oSboForm = Sbo_application.Forms.GetFormByTypeAndCount(-pval.FormType, pval.FormTypeCount)
    form and udf-form are linked through a minus sign.
    I do not know of a nice way to check if they are anebled. What I'm currently doing is running the above line of code contained in an error handler. The error handler does a sendkeys of control shift u (which enables the udf-form) and displays a message. Problem is after control shift u, screen is not yet updated so your code cannot continue immediatelly. Development knows of this problem, but I never received a promise when this will be fixed.
    Hope this helps,
    Jacques

  • User Defined Field with Link Type

    Hi,
    I am trying to create a user defined field to allow users to attach word documents to Item Master Data.
    I have successfully created a User Defined Field under master data with Type: Data, Structure: Link.
    It appears in the Item Master Data as I would expect. My questions relates to how to use this field. The first time I click on the field, a file browser window appears where I can select a file. Once I select the file I want I click ok and the path to this file appears in the field. The next time that I click on the field the file will open in its respective application.
    My question is: How when the field already contains a path to a file do I change this path so it points to a different file.
    Any help I can get on this would be greatly appreciated.
    Thanks
    Brian

    Hi,
    If you hold down Ctrl on your keyboard and then double click in the link it will allow you to browse to a different file.
    Regards,
    Adrian

  • User defined field with Linked Table property

    Hi All,
    I know this has been posted several times but I cannot get this to work.  I am trying to add a user defined field and link it to a table. 
    I am getting the error "The field 'Related Table' should consist of 8 alphanumeric characters with no valid or default values " 
    Everything I check says that the code I am using is correct.  Is it possible the problem is that I am trying to connect to an SAP table instead of a user defined table.  Is it possible to link a user defined field to an SAP table?
    My code below:
                    oUserFieldsMD.TableName = "OITM"
                    oUserFieldsMD.Name = "SHP"
                    oUserFieldsMD.Description = "Must ship via"
                    oUserFieldsMD.LinkedTable = "OSHP"
                    oUserFieldsMD.Type = SAPbobsCOM.BoFieldTypes.db_Alpha
                    oUserFieldsMD.SubType = SAPbobsCOM.BoFldSubTypes.st_None
                    oUserFieldsMD.EditSize = 8
                    oUserFieldsMD.Size = 8
                    oUserFieldsMD.Mandatory = SAPbobsCOM.BoYesNoEnum.tNO
    Thanks
    Karen

    Hello  Karen,
    you cannot link a userfield to a system table. it has to be link to a user table.
    oUserFieldsMD.LinkedTable = "OSHP"
    when you have error, just try to do it using the same value in the application. If you cannot do it, it means you cannot do it neither with the DI
    Sebastien

  • I can't see the user-defined fields I'm supposed to - where are they?

    Following a recent CACI release upgrade users are expecting to see several user-defined fields within student details in the Enrolments Universe but we can't. Following upgrades, do I need to refresh or run something that'll encorporate any new additions to the existing tables?
    Apologies, but this is the 1st time I have had to do this. 
    regards,
    Steven

    Hi,
    which version of BOB are you talking about here? Did you already made your changes to the universe and exported it in your repository? What kind of reporting tool (WebI, DeskI, CR) are you using?
    Regards,
    Stratos

  • User defined fields via DI-API

    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

    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

  • Creating User Defined Fields via DI API

    Hello,
    Has anyone tried creating User Defined Fields via DI without direct database intervention ? My add on relies on some UDF's that have to be created on install.
    Thank you

    I regularly use the UI API to do this.  You can find several discussions on this forum.  I have not put this in my installation program, but I use UserFieldsMD in the DI API and a CSV file from Excel to store the information about the fields.

  • How to remove User Defined field in programitically

    Hi,
           How to remove user defined field in programitically. send the code
    Thanks,
    P.Suresh Kumar

    Hi,
    Please take a look at this thread:
    Re: Can't remove a user defined field with DI API.
    Kind Regards,
    Owen

  • Cannot remove user define fields

    Hi,
    I am trying to remove a user defined field (U_Prirev) in Table (OHEM) and am getting the following error:
    Ref count for this object in higher then 0. Error No -1120
    The code is as follows :
            Dim sRequete As String
            Dim errCode As Long
            Dim errMsg As String
            Dim IdField As Integer
            Dim oUFields As SAPbobsCOM.UserFieldsMD
            oUFields = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)
            Dim oRecordSet As SAPbobsCOM.Recordset
            oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            sRequete = "SELECT FieldID FROM CUFD where TableId = '" & pNomTable & _
                       "' and AliasID = '" & pNomChamp & "'"
            oRecordSet.DoQuery(sRequete)
            If (oRecordSet.EoF = False) Then
               IdField = CType(oRecordSet.Fields.Item(0).Value, Integer))
                If (oUFields.GetByKey(pNomTable, IdField) = True) Then
                    If (oUFields.Remove() <> 0) Then
                        oCompany.GetLastError(errCode, errMsg)
                        Console.WriteLine("Erreur => Code = " & errCode.ToString + " Message = " + errMsg)
                    End If
                End If
            End If
                 System.Runtime.InteropServices.Marshal.ReleaseComObject(oUFields)
            oRecordSet = Nothing
        End Sub
    Anyone knows why i am getting this error.
    Thanks
    Laurent.

    Hi Laurent
    That error you are getting is the error that you get is when there is more than one instance of meta data object open. You see only one metedata object can be open at a time.....if it's higher than zero it means others are available. I see you have,
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oUFields)
    oRecordSet = Nothing
    this is good and what closes the meta data object. But check that you have done this in previous funtions where you might of added other things.Also your recordset, close it before adding.....move oRecordSet = Nothing to before your Remove of the field. that has be the case for me before. Do it without the recordset as a test and you'll see it works.
    Hope it helps

  • User Defined Fields/ XML loading problem

    hai,
    1. i can able to create user defined fields , but i need my fields are in UNIUQE KEY how can i achive through the
    VB Code. ?
    2. I load a XML form but i cant able to make it as fixed single form, or deactivate the minimize / maximaze button , how can i achive it through the VB Code?
    regards
    Thiru

    I don't think you can disable the min max button, but you can just remove them
    The following XML create a form that can't be Resize, maybe it can help you.
    <?xml version="1.0" encoding="UTF-16"?>
    <Application>
      <forms>
        <action type="add">
          <form AutoManaged="0" BorderStyle="6" FormType="60007" ObjectType="-1" SupportedModes="15" appformnumber="" client_height="119" client_width="60" color="0" default_button="" height="121" left="0" mode="1" pane="0" title="Toolbar Like" top="0" type="6" uid="Try" visible="1" width="160">
            <datasources>
              <dbdatasources>
                <action type="add" />
              </dbdatasources>
              <userdatasources>
                <action type="add" />
              </userdatasources>
            </datasources>
            <Menus />
            <items>
              <action type="add">
                <item AffectsFormMode="1" backcolor="-1" description="" disp_desc="0" enabled="1" font_size="0" forecolor="-1" from_pane="0" height="21" left="112" linkto="" right_just="0" supp_zeros="0" tab_order="0" text_style="0" to_pane="0" top="0" type="100" uid="Line1" visible="1" width="1">
                  <AutoManagedAttribute />
                  <specific />
                </item>
              </action>
            </items>
            <FormMenu />
            <DataBrowser />
          </form>
        </action>
      </forms>
    </Application>

  • User Defined Field in Material master

    Hi Friends,
    Can we add any user defined fields in any views in  material master (preferably with F4 help)
    If so, please let me know how to do it and its impact.
    Regards
    Ram

    Hi Ramachandran,
    Of course. 
    1.  If there is an existing Material Class, use it, unless modifying that class interferes with existing business processes.  Otherwise create a new class using CL01.  Use class type 001.
    2.  Create a characteristic using CT04 that represents your new field.  The characteristic will be your new field name and the description will be the description.
    3.  Using CL02, assign the characteristic to the existing material class, or to the new one you just created in step 1.
    4.  If this is a new system, you will have to create user instructions as to how to maintain the field.  The field will exist in the Classification view of the Material master in MM01/02/03, and the contents can be edited just like any field.
    5.  If this is an existing system, you will have to populate this field in all of the existing material masters.  Depending on the size and complexity of data, you may have to create a Legacy System Migration Workbench (transaction LSMW) project to bring all the fields up to date.
    As far as F4 help, depends on where you want to see it.  Within the classification view of the material master, it is possible to establish a validation in CT04 that will require your users to pick from an established validation list when they select the F4 key.  If you want F4 help in other places, you should consult your ABAP'ers, who should be able to set this up for you.
    Impact of a new characteristic/class is zero.  Impact of adding a characteristic to an existing class is dependent on your current use of the existing classes.  Writing reports which use these data is a well understood technique within the ABAP community.  Although the field will not exist in MARA table, it can still give you the same functionality as if it did.
    I do not recommend that you add a field to MARA, even though this is possible.  Although hotpack upgrades will leave a customer generated MARA field alone, whatever functionality you create to use this new field will be endangered with every OSS note and every hotpack upgrade you install.  Since Characteristics and Classes are considered master data, SAP hotpacks pretty much leave them alone.
    Regards,
    DB49

  • User defined field values doesnot show up in summary tab

    Hi,
    I have created my own stylesheet to display some of my user defined field values (associated with organization address book entry type) in the summary tab of the addressbook entry. But Groupwise 8.0 client FAILS to display any of the user defined field values in the summary tab FOR ONLY ORGANIZATION ADDRESSBOOK ENTRY types. NOTE: These organization addressbook entries are created using the C++ Groupwise object API. But the Advanced tab of these organization addressbook entry shows those user defined fields with values in it.
    Also Groupwise 8.0 Client is able to display the user defined field values in the summary tab for the PERSON ADDRESSBOOK ENTRY types, that are created using the Groupwise Object API. Also when I create a Organization addressbook entry using the Groupwise Client (NEW ORGANIZATION) and fill in the user defined field values using the Advanced tab or that addressbook entry, the summary tab displays those user defined field values.
    So there could be some problem in setting the user defined field value for the Organization address book entry using the Object API or it could be Groupwise 8.0 Client issue in displaying the user defined field values ONLY FOR ORGANIZATION ADDRESS BOOK ENTRY, WHICH ARE CREATED USING THE OBJECT API. I also tried to see the underlying XML data in the summary tab and that XML data is missing the USERDEFINEDFIELDS and CATEGORIES tag for the organization address book entry.
    I would like to know, if there is any need for special handling only for Organization addressbook entry in Groupwise 8.0 API. Any thoughts or help is really appreciated.

    Hi,
    Does anyone have an idea?? Think I have the same issue. Except, when I log in the client (ver. 802) then all the info is displayed. But when the user logs in on the same client and the same pc, he sees the xml info except the office address.
    Any help is greatly appreciated!
    Grtz,
    Joost Brenters

  • User defined field in CO01,CO02 ?

    Dear Gurus,
    My client want a User define Field in Production order , just for information purpose
    How can I Create a user define field in this transaction ( CO01 / CO02 )
    Pl. Guide
    Regards,
    Ishwar

    Hi,
    Go to operation view in the SFO. Select the operation, then follow menu path Operation - Operation details (key board shortcut - F7). Now you will find some tabs, the tab user field is somewhere near the end so you would need to proceed to the right.
    Once you're in that tab, select the field you defined & hit enter. The fields will be displayed.
    Also the user-ext PPCO0006 is used to default values to order header fields, i am not sure how this can be used to add tabs in order header.
    Regards,
    Vivek

  • User Defined Fields Form

    Hi Philipp,
    This is in reference to ur post Dated Jan13,2005. "Opening User Defined Fields Form via API".
    Is it possible for u to give a code snippet. what i'm unable to get is
    - Check if i can get a handle of the UDF-Form in the Mainform Activate-Event
    and
    -disable the close button
    Thanx in anticipation.
    Manu.

    Hi Manu,
    Here is a code snipet where looks for the related UDF form. The FormTypeNum´s value is the same as pVal.FormType property.
    Dim oUDFForm As SAPbouiCOM.Form
    Try
    oUDFForm = con.Forms.GetForm("-" + FormTypeNum.ToString.Trim, con.Forms.Item(FormUID).TypeCount)
    Catch ex As Exception
       con.ActivateMenuItem("6913")
       oUDFForm = con.Forms.GetForm("-" + FormTypeNum.ToString.Trim, con.Forms.Item(FormUID).TypeCount)
    End Try
    The other issue is solved in your other post.
    Regards,
    Ibai Peñ

Maybe you are looking for

  • Error 500: Model binding context not initialized

    I have created a JHeadStart Bc4J application in JDeveoper 10g and moved it WebSphere 6. I have moved all the required binary libraries to the lib/ext directory of the WebSphere AppServer. JSP and HTML pages are being displayed correctly but whenever

  • Charge adaptor for 3G

    so the 3g has been out a while now and i still cant find an adapter so my CD player in my car will charge my iphone 3g. i see cablejive has made one that will be on sale soon, does anybody know if there is any other adapters already fro sale? http://

  • Confusion  regarding logical standby database

    While designing the logical standby database, Oracle recommends to add the supplement information in the primary database, so that the changed columns are uniquely recorded in redo log file. In this case the amount of information in redo log files wi

  • Passing array as method to activex

    Dear Users; Sorry if this is a repeat question - I could not find relevant code in the faq. I have an activeX control which contains a method (PassArray) which accepts an array, and modifies its contents... The array *seems* to be correctly passed in

  • Captivate preview and publish text does not match slide. (Magic Shrinking Text)

    The text size on the Captivate slide matches the PowerPoint. So far so good. When I preview (or publish) the SCORM module, the text shows up as a smaller size and moved to the left. I don't understand why it doesn't match the original captivate slide