cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to create a UDF at item_pressed event?

Former Member
0 Kudos

Hai,

I have designed a form with some edit text and one Button. When Button is pressed, given values are stored to a table Master Table.

At the same time I want to insert a particular edit text value as a field in another No Object Table.

Is it possible to do or any other option for this?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Yes it is possible.

First of all, you need to diferentiate - creating a UDF is something you only ever do once, then the field is created in the database.

So do you want to actually add a field to a table every time the event is fired, or do you just want to add a value to a field? Either way there are objects in the DI that allow you do both - UserFieldsMD object allows you to create fields, UserTable object allows you to add rows to a usertable (or update a row too). If you search the forums for those two objects you will find many examples.

This example from the helpfile shows how to add a table, add a field to the table and add a value to that field, all using the DI API

Dim ret As Long
d
Private Sub Add_Table_Click()
    Dim oUserTablesMD As SAPbobsCOM.UserTablesMD
    Set oUserTablesMD = oCompany.GetBusinessObject(oUserTables)

    '**************************************************
    ' When adding user tables or fields, use a prefix
    ' identifying your partner name space. This will
    ' prevent collisions from different partner add-ons
    '
    ' SAP's name space prefix is "BE_"
    '**************************************************

    'Set the two mandatory fields
    oUserTablesMD.TableName = "T1"
    oUserTablesMD.TableDescription = "Table1"

    'Add the table (which contains 2 default, mandatory fields, 'Code' and 'Name')
    ret = oUserTablesMD.Add

    If ret <> 0 Then
        oCompany.GetLastError ret, Str
        MsgBox Str
    Else
        MsgBox "Table: " & oUserTablesMD.TableName & " was added successfully"
    End If
End Sub


Private Sub Add_UDF_Click()
    Dim oUserFieldsMD As SAPbobsCOM.UserFieldsMD
    Set oUserFieldsMD = oCompany.GetBusinessObject(oUserFields)

    oUserFieldsMD.TableName = "T1"
    oUserFieldsMD.Name = "AlbUDF"
    oUserFieldsMD.Description = "Albert UDF"


    'Add the field to the table
    lRetCode = oUserFieldsMD.Add

    If lRetCode <> 0 Then
        oCompany.GetLastError ret, Str
        MsgBox Str
    Else
        MsgBox "Field: '" & oUserFieldsMD.Name & "' was added successfuly to " & oUserFieldsMD.TableName & " Table"
    End If
End Sub


Private Sub Add_Data_Click()
    Dim oUserTable As SAPbobsCOM.UserTable
    Set oUserTable = oCompany.UserTables.Item(1)

    oUserTable.GetByKey ("T1")

    'Set default, mandatory fields
    oUserTable.Code = "A"
    oUserTable.Name = "Albert"
    'Set user field
    oUserTable.UserFields.Fields.Item("U_AlbUDF").Value = "1"

    oUserTable.Add

    If ret <> 0 Then
        oCompany.GetLastError ret, Str
        MsgBox Str
    Else
        MsgBox "Value to field: '" & oUserTable.UserFields.Fields.Item("U_AlbUDF").Name & "' was updated successfuly to " & oUserTable.TableName & " Table"
    End If
End Sub

Also look at DI sample 2: 02.MetaDataOperations

Former Member
0 Kudos

Hai,

Thanks for your reply.

Actually I can add field, only if the function is called immediately after connecting to company.But I cant add field at Item_pressed event.Im struggling here.Hope you got my query.

Thanks.

Former Member
0 Kudos

Hi ParvathaSolai,

i think you can add field when connect to company but you should set authorization for one user. example addmin

regards,

H2

Former Member
0 Kudos

This is a similar discussion to another recent thread here!

First of all, creating fields should be something the addon does when it first connects. Adding fields during runtime, when multiple users might be logged in, is not good practice. On top of that, it shouldn't be neccesary, you should know in advance what fields are needed by your addon.

But putting your reasons for doing it aside - apart from strongly advising that you do all field creation at addon startup - you should be able to add a field at any time. It is that same code - the only thing to be careful of is that you are not running any other MetaData operations: For example do you have an undisposed recordset object?

But your query is not clear.. Are you getting an error? An exception? What do you mean by "I cant add field at Item_pressed event"? What happens when you try? Are you adding a field to the database or adding data to an existing UDF?

Former Member
0 Kudos

Hai,

Thanks for your reply.

I want to add Field to an already existing No Object Table.

When I am trying to add field, in Item_Pressed event, it shows error code as -1120 and message as "Reference to object is higher then 0"

But I can add , If I wrote coding immediately after connecting to company.

Thanks.

Former Member
0 Kudos

The most likely solution is you have some other metadata operation happening - do you have an open recordset object in your code? Dispose it and clean up using the GC. There are many threads that do this.

Or have you added some other field or table without disposing of the object.

If you still get the error, please show us your code.

Former Member
0 Kudos

Hai,

My code is:

If (pVal.FormUID = "DefPayCod" Or pVal.FormUID = "DefDedCod" Or pVal.FormUID = "DefBnfCde" Or pVal.FormUID = "DefLnAdvns") And pVal.ItemUID = "1" And pVal.ActionSuccess = True Then

Try

Dim codetoinsert, desctoinsert, sErrMsg As String

Dim lRetCode, lErrCode As Long

Dim found1, found2 As Boolean

oForm = SBO_Application.Forms.ActiveForm()

recordset = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

Dim query As String

If oForm.UniqueID = "DefPayCod" Then

query = "SELECT top 1 T0.[Code], T0.[U_PName] FROM [dbo].[@TI_PAYCODEHDR] T0 ORDER BY T0.[DocEntry] desc"

recordset.DoQuery(query)

codetoinsert = recordset.Fields.Item("Code").Value

desctoinsert = recordset.Fields.Item("U_PName").Value

End If

If oForm.UniqueID = "DefDedCod" Then

query = "SELECT top 1 T0.[Code], T0.[U_DName] FROM [dbo].[@TI_DEDUCTIONHDR] T0 ORDER BY T0.[DocEntry] desc"

recordset.DoQuery(query)

codetoinsert = recordset.Fields.Item("Code").Value

desctoinsert = recordset.Fields.Item("U_DName").Value

End If

If oForm.UniqueID = "DefBnfCde" Then

query = "SELECT top 1 T0.[Code], T0.[U_BName] FROM [dbo].[@TI_DFBNFCDEHDR] T0 ORDER BY T0.[DocEntry] desc"

recordset.DoQuery(query)

codetoinsert = recordset.Fields.Item("Code").Value

desctoinsert = recordset.Fields.Item("U_BName").Value

End If

If oForm.UniqueID = "DefLnAdvns" Then

query = "SELECT top 1 T0.[Code], T0.[U_DName] FROM [dbo].[@TI_DEDUCTIONHDR] T0 ORDER BY T0.[DocEntry] desc"

recordset.DoQuery(query)

codetoinsert = recordset.Fields.Item("Code").Value

desctoinsert = recordset.Fields.Item("U_DName").Value

End If

Dim RetVal As Long

Dim oUDF As SAPbobsCOM.UserFieldsMD

oUDF = ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)

oUDF.Name = codetoinsert

oUDF.Type = SAPbobsCOM.BoFieldTypes.db_Alpha

oUDF.TableName = "@TI_ADDCODES"

lRetCode = oUDF.Add()

if lRetcode<>0

ocompany.GetLastError(lErrCode, sErrMsg)

end if

Catch ex As Exception

SBO_Application.MessageBox(ex.Message)

End Try

End If

Thanks in Advance

Former Member
0 Kudos

You have an undisposed recordset object so it is blocking you from creating the fields - this is covered in lots of posts on the forum if you search for the error message.

e.g.:

change

Dim RetVal As Long

Dim oUDF As SAPbobsCOM.UserFieldsMD

to

System.Runtime.InteropServices.Marshal.ReleaseComObject(recordset);
recordset = Nothing
GC.Collect()
GC.WatForPendingFinalizers()

Dim RetVal As Long

Dim oUDF As SAPbobsCOM.UserFieldsMD

Former Member
0 Kudos

Hai,

It works. I got the Solution.

Thanks.

Former Member
0 Kudos

Hai,

It works. I got solution.

Thanks

Answers (0)