Start a new topic

Problems with removing attributes

When trying to programmatically remove education attributes the process has unwanted effects of deleting education attributes at what feels like at random. I have posted a snippet of my code below so it is clean for review. I can confirm that the same thing is happening to me for constituent attributes as well (I know there is a way to remove constit attributes via data file/profile). Is my delete process flawed?

Public Overrides Sub BeforeConstituentSave(ByVal oRec As Blackbaud.PIA.RE7.BBREAPI.CRecord, _
                                        Cancel As ImportOM.API.iCancel)

Dim oEd As Blackbaud.PIA.RE7.BBREAPI.CEducation
Dim oAttrTypesServer As New Blackbaud.PIA.RE7.BBREAPI.CAttributeTypeServer
Dim oAttribute As Blackbaud.PIA.RE7.BBREAPI.IBBAttribute
Dim lTypeId As Long




oAttrTypesServer.Init(oImport.SessionContext)



For Each oEd In oRec.Relations.Education


If oEd.Fields(EEDUCATIONFields.EDUCATION_fld_SCHOOL_ID) = "School" Then


For Each oAttribute In oEd.Attributes


lTypeId = oAttribute.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTETYPES_ID) 


Select Case (oAttrTypesServer.GetAttributeTypeDescription(lTypeId))  

Case "Student Status"
oEd.Attributes.Remove(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTES_ID)

   Case "Student Action"
oEd.Attributes.Remove(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTES_ID)

   Case "Grad Type"
oEd.Attributes.Remove(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTES_ID)

  End Select  
 
Next



End If


Next oEd


oEd = Nothing
oAttrTypesServer = Nothing
oAttribute = Nothing
lTypeId = Nothing

End Sub


Travis,

Your code does need some work. You are removing the same value each time.

Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTES_ID = 1

This amounts to oEd.Attributes.Remove(1)

The remove method accepts an attribute or an ID.

Because you are not exiting your attributes for loop after you remove an attribute (I assume there could be multiple attributes), you will want to instead, store a temporary collection of attributes or the ID's of the attributes and then do you removal outside of the attributes for loop. Removing objects from a collection while inside a loop that is iterating through that collection can cause errors.

Here is an example:

Dim list as New List(of Integer)
For Each oAttribute In oEd.Attributes
lTypeId = oAttribute.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTETYPES_ID)
Select Case (oAttrTypesServer.GetAttributeTypeDescription(lTypeId))
Case "Student Status", "Student Action", "Grad Type"
list.Add(oAttributes.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTES_ID) )
End Select
Next

For Each i As Integer in list
oEd.Attributes.Remove(i)
Next



Thank you Nic for getting me on the right track!
Login or Signup to post a comment