Every so often you discover something useful from the vault of information that is the API/VBA help file. The help file is a reference guide for the whole API but in order to really make use of it you have to study the various classes carefully. This is generally not something I enjoy doing.
I stumbled across a useful solution to a problem that could easily have been solved another way. I had been manipulating address objects (CConstitAddress) but did not have the name and constituent id of the constituent that the address referred to. The system id of the constituent was available:
oAddress.Fields(CONSTIT_ADDRESS_fld_CONSTIT_ID)
but this is not useful to display to the user. Of course I could have passed the constituent object into the procedure too but it was several function calls deep and this is not always possible to do either.
Previously I would have loaded the constituent using this system id and then obtained the name and constituent id. However I was looking in the IBBUtilityCode class and saw the unseemingly verbose method name of GetConstituentNameConstitIDandKeyInd. It does exactly as it says. You supply it the system id and it returns the constituent id, full name and key indicator. It will also give you the first, last and middle names too if you supply variables to do it.
However I would probably never use this code if I had to create all the objects that were required. Here is the code in full. It assumes that I am not interested in the first, last and middle names that can optionally be returned. We assume that oAddress has been brought into the procedure prior to this code.
Code:
Dim sConID As String Dim sFullname As String Dim lKeyInd As Long Dim oReservices As New reservices Dim oUtilcode As IBBUtilityCode oReservices.Init SessionContext Set oUtilcode = oReservices oUtilcode.GetConstituentNameConstitIDandKeyInd oAddress.Fields CONSTIT_ADDRESS_fld_CONSTIT_ID), sFullname, sConID, lKeyInd MsgBox "The constituent id is: " & sConID oReservices.CloseDown Set oReservices = Nothing
This is 11 lines of code. Whereas my alternative:
Code:
Dim oConstit As New CRecordoConstit.Init SessionContext oConstit.Load oAddress.Fields(CONSTIT_ADDRESS_fld_CONSTIT_ID) MsgBox "The constituent id is: " & oConstit.Fields(RECORDS_fld_CONSTITUENT_ID) oConstit.CloseDown Set oConstit = Nothing
This is only 6 lines.
So why use the first method at all?
While I have been working on various projects I have created my own libary of useful code. These include connecting to RE, storing a copy of the SessionContext and having methods to retrieve the IBBUtilityCode object without having to create an REServices object first (this is done once in the code adn stored for later use). What is more you do not actually have to supply reference variables to the GetConstituentNameConstitIDandKeyInd if you do not intend to use them (some may say that this is bad practise and could be confusing). My new abbreviated code looks as follows:
Code:
Dim sConID As String REUtil.getUtilityCode.GetConstituentNameConstitIDandKeyInd oAddress.Fields(CONSTIT_ADDRESS_fld_CONSTIT_ID), "dummy text", sConID, 0 MsgBox "The constituent id is: " & sConID
This is only three lines of code. Note that I am not interested in the full name or the value of the key indicator so I just supply them with a value that does not need to be declared.
The IBBUtilityCode class contains a load of methods that could be of use. They are not well documented but most can be understood by their names.