Start a new topic

Making Constituent Codes Primary

Here is some code I wrote to make a constituent code "primary" when you add it

Call it in BeforeConstituentSave in Profile.vb using:

GlobalCode.ImportPrimaryConsCode(oRec, Import.Fields("YourConsCodeColumn").Value, Import)

Code for Global.vb below

=======================================

' ImportPrimaryConsCode

' Wayne Pozzar 2014-03-05

'

' DESCRIPTION

'   This function will rearrange the constituent codes on the current record

' to place the desired constituent code in position 1 making it the "primary"

' code according to RE.

' INPUT

' oRec - The current constituent record (should be called in BeforeConstituentSave)

' sImportCode - The constituent code to be moved to position 1

' oImport - The current Import session

' OUTPUT

' None

'=======================================

  Public Shared Sub ImportPrimaryConsCode(oRec As Blackbaud.PIA.RE7.BBREAPI.CRecord, sImportCode As String, oImport As ImportOM.API.iImportSession)


  ' If the import field is blank then just leave

    If sImportCode = "" Then

      Exit Sub

    End If


    Dim sRECode As String ' current code in loop

    Dim oCCode As Blackbaud.PIA.RE7.BBREAPI.CConstituentCode ' ConsCode object for loop

    Dim iSequence As Integer ' sequence of current code in loop


    ' Go through all of the cons codes on the record and rearrange them

    For Each oCCode In oRec.ConstituentCodes

      ' Get the current code from RE

      sRECode = oCCode.Fields(Blackbaud.PIA.RE7.BBREAPI.ECONSTITUENT_CODEFields.CONSTITUENT_CODE_fld_LONG_DESC_READONLY)

      ' Get the current sequence from RE

      iSequence = CInt(oCCode.Fields(Blackbaud.PIA.RE7.BBREAPI.ECONSTITUENT_CODEFields.CONSTITUENT_CODE_fld_SEQUENCE))

      ' Check the incoming code against this existing code

      If String.Compare(sRECode, sImportCode, True) = 0 Then

        ' Found the incoming code

        If iSequence = 1 Then

          ' The code is already in the right spot so we are done

          Exit For

        Else

          ' the code is not in the right spot so reassign it to the first position

          oCCode.Fields(Blackbaud.PIA.RE7.BBREAPI.ECONSTITUENT_CODEFields.CONSTITUENT_CODE_fld_SEQUENCE) = 1

          ' Now we are done since it goes in sequence order and we have already reassigned previous codes ahead one

          Exit For

        End If

      Else

        ' Not the correct code so move this one up one

        oCCode.Fields(Blackbaud.PIA.RE7.BBREAPI.ECONSTITUENT_CODEFields.CONSTITUENT_CODE_fld_SEQUENCE) = iSequence + 1

      End If

    Next oCCode

  End Sub

Login or Signup to post a comment