Start a new topic

Add an Attribute to a record

I have seen lots of posts here about adding an attribute to a record so I thought I would make a function to help.  This should be placed in the Global.vb section.

You call it from your code like this:
  GlobalCode.AddAttribute(Import, oRec.Attributes,"AttributeType","Blah", "","4/1/2014")

It will take any record type that has an Attributes collection (Constit, Individual, Org, Address, Gift, etc.).  It looks up the type name based on the type of attribute.

 

'=======================================
' AddAttribute - Add an attribute to an object
' Wayne Pozzar 2014-04-01
'=======================================
	Public Shared Sub AddAttribute( oImport As ImportOM.API.iImportSession, oAttributes As IBBAttributesAPI, sCategory As String, sDescription As String, sComments As String, sDate As String)
	
		' Create and init an attribute type server to find the type id
		Dim oAttTypeServer As New CAttributeTypeServer
		oAttTypeServer.Init(oImport.SessionContext)
		Dim lAttTypeID As Long
		
		' Create the lookup dictionary to convert from Object Type to Attribute type
		Dim dAttributeTypesLookup As New System.Collections.Generic.Dictionary(Of String, String)
		dAttributeTypesLookup.Add(714, 1) ' bbmoCONSTITUENT_ATTRIBUTE -> bbAttributeRecordType_CONSTITUENT
		dAttributeTypesLookup.Add(723, 2) ' bbmoGIFT_ATTRIBUTE -> bbAttributeRecordType_GIFT
		dAttributeTypesLookup.Add(756, 13) ' bbmoJOB_ATTRIBUTE -> bbAttributeRecordType_JOB
		dAttributeTypesLookup.Add(763, 4) ' bbmoEDUCATION_ATTRIBUTE -> bbAttributeRecordType_EDUCATION
		dAttributeTypesLookup.Add(764, 17) ' bbmoFINANCIAL_ATTRIBUTE -> bbAttributeRecordType_FINANCIAL
		dAttributeTypesLookup.Add(770, 5) ' bbmoEVENT_ATTRIBUTE -> bbAttributeRecordType_SPECIAL_EVENT
		dAttributeTypesLookup.Add(771, 6) ' bbmoPARTICIPANT_ATTRIBUTE -> bbAttributeRecordType_PARTICIPANT
		dAttributeTypesLookup.Add(774, 3) ' bbmoACTION_ATTRIBUTE -> bbAttributeRecordType_ACTION
		dAttributeTypesLookup.Add(775, 16) ' bbmoRELATION_ORGANIZATION_ATTRIBUTE -> bbAttributeRecordType_ORGANIZATION_RELATIONSHIP
		dAttributeTypesLookup.Add(776, 15) ' bbmoRELATION_INDIVIDUAL_ATTRIBUTE -> bbAttributeRecordType_INDIVIDUAL_RELATIONSHIP
		dAttributeTypesLookup.Add(822, 12) ' bbmoCONSTITUENT_ADDRESS_ATTRIBUTE -> bbAttributeRecordType_CONSTIT_ADDRESS
		dAttributeTypesLookup.Add(827, 18) ' bbmoPROPOSAL_ATTRIBUTE -> bbAttributeRecordType_PROPOSAL
		dAttributeTypesLookup.Add(839, 15) ' bbmoSPOUSE_ATTRIBUTE -> bbAttributeRecordType_INDIVIDUAL_RELATIONSHIP
		dAttributeTypesLookup.Add(840, 12) ' bbmoSPOUSE_ADDRESS_ATTRIBUTE -> bbAttributeRecordType_CONSTIT_ADDRESS
		dAttributeTypesLookup.Add(841, 16) ' bbmoBUSINESS_ATTRIBUTE -> bbAttributeRecordType_ORGANIZATION_RELATIONSHIP
		dAttributeTypesLookup.Add(842, 12) ' bbmoBUSINESS_ADDRESS_ATTRIBUTE -> bbAttributeRecordType_CONSTIT_ADDRESS
		dAttributeTypesLookup.Add(843, 15) ' bbmoCONTACT_ATTRIBUTE -> bbAttributeRecordType_INDIVIDUAL_RELATIONSHIP
		dAttributeTypesLookup.Add(844, 12) ' bbmoCONTACT_ADDRESS_ATTRIBUTE -> bbAttributeRecordType_CONSTIT_ADDRESS
		dAttributeTypesLookup.Add(846, 12) ' bbmoRELATION_INDIVIDUAL_ADDRESS_ATTRIBUTE -> bbAttributeRecordType_CONSTIT_ADDRESS
		dAttributeTypesLookup.Add(848, 12) ' bbmoRELATION_ORGANIZATION_ADDRESS_ATTRIBUTE -> bbAttributeRecordType_CONSTIT_ADDRESS
		dAttributeTypesLookup.Add(849, 14) ' bbmoMEMBERSHIP_ATTRIBUTE -> bbAttributeRecordType_MEMBERSHIP
		dAttributeTypesLookup.Add(881, 7) ' bbmoCampaignAttribute -> bbAttributeRecordType_CAMPAIGN
		dAttributeTypesLookup.Add(882, 10) ' bbmoFundAttribute -> bbAttributeRecordType_FUND
		dAttributeTypesLookup.Add(883, 8) ' bbmoAppealAttribute -> bbAttributeRecordType_APPEAL
		dAttributeTypesLookup.Add(885, 11) ' bbmoAppealPackageAttribute -> bbAttributeRecordType_PACKAGE
		dAttributeTypesLookup.Add(960, 4) ' bbmoRELATION_EDUCATION_ATTRIBUTE -> bbAttributeRecordType_EDUCATION
		dAttributeTypesLookup.Add(961, 4) ' bbmoCONTACT_EDUCATION_ATTRIBUTE -> bbAttributeRecordType_EDUCATION
		dAttributeTypesLookup.Add(962, 4) ' bbmoSPOUSE_EDUCATION_ATTRIBUTE -> bbAttributeRecordType_EDUCATION
		dAttributeTypesLookup.Add(965, 12) ' bbmoSPOUSE_BUSINESS_ADDRESS_ATTRIBUTE -> bbAttributeRecordType_CONSTIT_ADDRESS
		dAttributeTypesLookup.Add(967, 16) ' bbmoSPOUSE_BUSINESS_ATTRIBUTE -> bbAttributeRecordType_ORGANIZATION_RELATIONSHIP
		
		' Lookup the ID for the Attribute Category
		lAttTypeID = oAttTypeServer.GetAttributeTypeID(sCategory, dAttributeTypesLookup.Item(oAttributes.MetaField.MetaObjectID))
		
		' Add the attribute with the supplied data
		With oAttributes.Add() 
			.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTETYPES_ID) = lAttTypeID
			.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_VALUE) = sDescription
			.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_COMMENTS) = sComments
			.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTEDATE) = sDate
		End With 
		
		'Cleanup
		oAttTypeServer.CloseDown()
		oAttTypeServer = Nothing
		dAttributeTypesLookup = Nothing
		
	End Sub

 

 
 



Wayne, Do you want to know something that's going to make you mad, then eventually glad? :)

Jeff
I am going to guess that it has something to do with either:

a) there is a much easier way to do this in REAPI already
b) you are adding IOM functionality to do this
c) there is already IOM functionality to do this and I just didn't find it
".Fields(Attribute_fld_ATTRIBUTETYPES_ID)" will accept either an ID or the string name of the attribute, so no need to translate to the ID. I found that out after years of using the CAttributeTypeServer as well!



KHAAAAAAAN!

Some more excellent Blackbaud documentation!

At least I learned a lot in working all of that out so not a total loss :)
I still have to use the typeID when I am looking up an attribute though right?

Like when I am looping through looking at each attribute to get the one that I want?
Wayne,

Thanks for your post! I just tested your code and it works great.

The only question I have is that IOM does not let me close down the oAttTypeServer object as in this line:

[code] oAttTypeServer.CloseDown() [/code]

It says CloseDown is not a member of CAttributeTypeServer.

I took out that line but not sure if I am supposed to close it down or not.

Any Idea?

Philip
Not sure Philip but according to Jeff we don't actually need the server anyways!

".Fields(Attribute_fld_ATTRIBUTETYPES_ID)" will accept either an ID or the string name of the attribute, so no need to translate to the ID. I found that out after years of using the CAttributeTypeServer as well!

So I guess you can just put in the name of the attribute type instead of the ID and it will work.
You are right! I will test this. This means effectively that I only need this code, right?

[code] ' Add the attribute with the supplied data
With oAttributes.Add()
.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTETYPES_ID) = sAttributeName
.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_VALUE) = sDescription
.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_COMMENTS) = sComments
.Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTEDATE) = sDate
End With
[\code]

Ok. That worked. Here is the final code I am using:


Public Shared Sub AddAttribute(oAttributes As IBBAttributesAPI, sCategory As String, sDescription As String, sComments As String, sDate As String)

' Add the attribute with the supplied data With oAttributes.Add() .Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTETYPES_ID) = sCategory .Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_VALUE) = sDescription .Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_COMMENTS) = sComments .Fields(Blackbaud.PIA.RE7.BBREAPI.EattributeFields.Attribute_fld_ATTRIBUTEDATE) = sDate End With End Sub


Of course the record also needs to be saved in the calling code. e.g.: oRec.Save

Login or Signup to post a comment