cancel
Showing results for 
Search instead for 
Did you mean: 

Get next BusinessPartners Code/Key

Former Member
0 Kudos

Hi together,

I am looking for a way to get the next CardCode of a BusinessPartner to add a new one by DIApi.

I tried it by using some Code like this, but it does not work fine:

Dim oSeriesService As SAPbobsCOM.BusinessPartnersService = cmpService.GetBusinessService(SAPbobsCOM.ServiceTypes.BusinessPartnersService)

Dim oSeriesCollection As SAPbobsCOM.BusinessPartners = oSeriesService.GetDataInterface(SAPbobsCOM.BusinessPartnersServiceDataInterfaces.bpsdiBPCodes)

The Bold Text is not very fine, because there is no method like 'GetNextCardCode', otherwise its not tested, so i dont think this would work.

Is there a way like the SeriesService... eg. there are no problems to get the next DocNum of an invoice

Thank you!

Sebastian

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Thanks at all!

I know that the Recordset is DIAPI. I was looking for a way to do it with the integrated methods. For example the same way B1 uses to automatically generate a new CardCode.

Former Member
0 Kudos

hi,

Dim oRecordSet As SAPbobsCOM.Recordset

Dim oBPs As SAPbobsCOM.BusinessPartners

Dim iContact As Integer

'' Reference all the Business Partners

oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

oRecordSet.DoQuery("SELECT * FROM OCRD")

oBPs = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oBusinessPartners)

oBPs.Browser.Recordset = oRecordSet

'' Iterate all the BPs

oBPs.Browser.MoveFirst()

While oBPs.Browser.EoF = False

'Your Code

oBPs.Browser.MoveNext()

End While

regards,

varma

Former Member
0 Kudos

I don't know if B1 has anything to generate a "next" cardcode value but I doubt it since the field is alphanumeric,

but I've written something to do this for myself.

On the before event when the user clicks on the add mode on the BP form, and if they leave the cardcode blank,

I use a sql statement to determine the next number to assign. We always prefix our customer numbers with a "C"

and our Suppliers with a "V" so I look for the highest cardcode (max) out there where the first character is a c/v and

the rest of the card code is completely numeric (like 'C1001', but not 'Customer 1' since it contains alpha values).

I just add one to the 1001 part and return 'C1002'. If by chance two users end up assigning the same number,

I let B1 catch the duplicate.

Maybe this code will work for you:

Public Enum BP_Type
        Customer = 0
        Vendor = 1
    End Enum

                'in add mode, if user leaves cardcode empty, then assign it the next highest number from table
                If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                    If Not IsNothing(cmbx) AndAlso Not IsNothing(cmbx.Selected) Then
                        Dim type As BP_Type
                        Dim prefix As String
                        If cmbx.Selected.Value = "S" Then
                            type = BP_Type.Vendor
                            prefix = "V"
                        Else
                            type = BP_Type.Customer
                            prefix = "C"
                        End If
                        If CardCode.Value = Nothing Then
                            Try
                                CardCode.Value = GetNextCardCode(type, prefix)
                            Catch ex As Exception
                                B1App.MessageBox("Could not generate next Card Code. " & vbCrLf & ex.ToString)
				Bubble = false
                            End Try
                        End If
                    End If

                End If

   Public Shared Function GetNextCardCode(Optional ByVal zCardType As BP_Type = BP_Type.Customer, Optional ByVal zCardCodePrefix As String = "C") As String

        Dim brwsr As Recordset
        Dim sql As String
        Dim cardtype As String


        'sql statement is looking for next number based on C or V in first character of existing cardcodes, via passed in
        'prefix field.
        Select Case zCardType
            Case BP_Type.Customer
                cardtype = "C"
            Case BP_Type.Vendor
                cardtype = "S"
        End Select

        'Get next highest customer number from table where customer number 
        'starts with a C and the remainder of the value is numeric; then add one to the max value
        brwsr = oCompany.GetBusinessObject(BoObjectTypes.BoRecordset)
        sql = "select  COALESCE(cast(max(str(substring(cardcode,2,len(cardcode)-1))) as numeric) +1,1) " & _
        " as newcode from ocrd  where cardtype = '" & cardtype & "' and left(cardcode,1) = '" & zCardCodePrefix & "' AND isnumeric(substring(cardcode,2,len(cardcode)-1)) = '1'"
        brwsr.DoQuery(sql)

        Return zCardCodePrefix & brwsr.Fields.Item("newcode").Value

    End Function

HTH

Former Member
0 Kudos

This seems to be a helpful answer, thank you! I need to check the way CardCodes are designed by our customer.

Maybe someone give me a advice to get the next cardcode by DIApi without using sql statements.

Nussi
Active Contributor
0 Kudos

Hi,

the DIAPI RecordSet with a max SELECT is already the best way to get the next CardCode.

it's way faster than any other way - i would do it that way

lg David