Start a new topic

API code for adding multiple events to a constituent record

Hi

I'd like to be able to add multiple events from an event enquiry from on a constituent record.  The code I wrote below does not work. No events are imported.  The participant attributes are the same for all the constituents I'm new to coding and I'd be so grateful for any advice on my coding below. 

If Import.Fields.GetByExcelName("R").Value.ToLower.Startswith ("x") THEN
    Import.Fields.GetByExcelName("AY").Value = "SL2P17"
    Microsoft.VisualBasic.MsgBox(Import.Fields.GetByExcelName("AY").Value.ToString)

End If

If Import.Fields.GetByExcelName("S").Value.ToLower.Startswith ("x") THEN
    Import.Fields.GetByExcelName("AZ").Value = "SFBZ17"
    Microsoft.VisualBasic.MsgBox(Import.Fields.GetByExcelName("AZ").Value.ToString)

End If

If Import.Fields.GetByExcelName("T").Value.ToLower.Startswith ("x") THEN
    Import.Fields.GetByExcelName("BA").Value = "SFSTM17"
    Microsoft.VisualBasic.MsgBox(Import.Fields.GetByExcelName("BA").Value.ToString)

End If

Thanks so much in advance.

Felicia


Felicia - Two questions:
For the fields AY, AZ, and BA, how do you have them mapped? Is to Event ID?
In addition, where do you have this code? which procedure do you have this code in?

Hi Steve

Yes AY, AZ and BA are mapped event ID. 

 I don't understand your 2nd question.  The code is written in code editor (Launch code editor in virtual field).  I don't have any procedure in which to have this code in.  

 

I'm sorry if this is so stupid sounding. I'm new to this and not quite sure where I'm going wrong.

 

Thanks so much for helping me.

 

Felicia

Felicia - What I mean by the second question is that there are "procedures", or what could also be called events in this case where you can put code. You will see lines of code such as the following:
Public Overrides Sub BeforeImport(ByVal Cancel As ImportOM.API.iCancel)
Public Overrides Sub BeforeComputedColumns(Cancel As ImportOM.API.iCancel)
Public Overrides Sub BeforeDictionaries(Cancel As ImportOM.API.iCancel)
Public Overrides Sub AfterDictionaries(Cancel As ImportOM.API.iCancel)

It matters where you have your code. Your code has to be put in the proper place for it to be read and applied appropriately. For example, you would not want to put your code in the procedure AfterRowCommit() or AfterImport() because that would mean the code would be applied too late.

You should try putting your code in the AfterDictionaries procedure and giving it a go. This would mean that after IOM applies dictionaries for a row of data coming in, your code would fire BEFORE data is actually added to RE via IOM. So the procedure would end up looking something like:

Public Overrides Sub AfterDictionaries(Cancel As ImportOM.API.iCancel)
'The data row at this point has gone through dictionaries
'Use the Fields object to access the data
'Example: Import.Fields.GetByExcelName("D").Value = "New Value for D Column"

If Import.Fields.GetByExcelName("R").Value.ToLower.Startswith ("x") THEN
Import.Fields.GetByExcelName("AY").Value = "SL2P17"
Microsoft.VisualBasic.MsgBox(Import.Fields.GetByExcelName("AY").Value.ToString)

End If

If Import.Fields.GetByExcelName("S").Value.ToLower.Startswith ("x") THEN
Import.Fields.GetByExcelName("AZ").Value = "SFBZ17"
Microsoft.VisualBasic.MsgBox(Import.Fields.GetByExcelName("AZ").Value.ToString)

End If

If Import.Fields.GetByExcelName("T").Value.ToLower.Startswith ("x") THEN
Import.Fields.GetByExcelName("BA").Value = "SFSTM17"
Microsoft.VisualBasic.MsgBox(Import.Fields.GetByExcelName("BA").Value.ToString)

End If
End Sub
Felicia - Something else to consider with your use case is that you could achieve the same thing by using Virtual Fields, where you copy from the field of data coming in and applying a dictionary to the virtual field. So, for example, if field AY is a Virtual Field, mapped to Event ID, you would have it set to Copy From field R. Then you would have a dictionary set on field AY which does a similar translation (if begins with x then results in SL2P17 put in the field, otherwise put in a Blank). You would need a regular expression in your dictionary to perform the begins with x.
Hi Steve
My initial code was in the After Dictionary procedure but it did not work. I don't know why IOM cannot import multiple events at the same time. I will try your second suggestion and see if that works. Thanks again for your help. Felicia.
Hi Felicia - It dawned on me what the problem would be, which I just confirmed. Your code is fine. The problem is that ImportOmatic only allows you to add one participant per each row of data. You could write customization code in your profile to actually add the participant records in RE for the second two events, but you really have to understand the RE API to do that. The other option as I see it is that you have 3 profles, one for each event. So you only map one of the fields to the Participant - Event ID in each of the profiles.
Hi Steve, I thought that would be the case i.e. IOM allowing only one event ID at a time. How very limiting. I don't really underthe RE API enough to do this so if you are able to help me, I would be most grateful. If you can't, then I'll just have to create 3 profiles. Many thanks again for all your help. Felicia
Felicia- here is a basic set of code to show you how to add a participant record on an event. I am using the AfterConstituentSave as we have the constituent that we want to add to the event in the oRec object. This is very basic but should get you started.

Public Overrides Sub AfterConstituentSave(oRec As Blackbaud.PIA.RE7.BBREAPI.CRecord, Cancel As ImportOM.API.iCancel)
MyBase.AfterConstituentSave(oRec, Cancel)

'initialize the participant
Dim participant As Blackbaud.PIA.RE7.BBREAPI.CParticipant = New Blackbaud.PIA.RE7.BBREAPI.CParticipant()

Try
participant.Init(Import.SessionContext)

'set the constituent id of the participant record
participant.Fields(Blackbaud.PIA.RE7.BBREAPI.EParticipantsFields.Participants_fld_RecordsID) = oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_ID)

'Assign the Event ID to the particpant record
'Note - the event id below is not the field Event ID on an event but is the System Record ID of the event.
participant.Fields(Blackbaud.PIA.RE7.BBREAPI.EParticipantsFields.Participants_fld_EventID) = [Your Event System Record ID]

'fill in other fields
'participant.Fields(......)

'save the participant record
participant.Save()

Catch ex As Exception
'if you wanted to cancel the row because we had an error you can uncomment out the line below
'Cancel.CancelRow(String.Format("Error adding participant record to constituent: {0}", ex.Message))
Finally
'close the participant
participant.CloseDown()
System.Runtime.InteropServices.Marshal.ReleaseComObject(participant)
End Try

End Sub
Steve, thanks so much. I will give this a whirl and let you know. Pls accept my grateful thanks for all your help on this. Felicia.

Hi Stephen, I know it has been a long time but is there a way to confirm that your way of adding new participants to an event works? I have been trying it but I get a weird error fired up at the oParticipant.Save() step. Error reads: Can not call friend function on object which is not an instance of defining class.


Thanks in advance.

Jeton

Hi Jeton,


Can you paste your code here so I can take a look?


Thanks,


John

Hi John,


I found where my mistake was, I was doing it in the BeforeImportComputedColumns method, while in the example above it is AfterConstituentSave method. I do not know why it would not work there but when I moved, it worked all well.


For future reference, I am posting my code. Just fyi, in my case, constituent is not the same as event participant, but I need to link constituent's gift to a specific participant (could be same or different based on the context). I am storing fundraiser id in the import file:


Dim EventSystemID As Integer = Utils.getValFromArray(EventID, EventRecordsLength, EventRecords, 0, 2) 'get the event system ID based stored in an array

    Dim oFundraiserRec As New CRecord 'fundraiser record (not donor itself necessarily) 

          Dim oEvent As New CSpecialEvent 'the event


          oEvent.Init(Export.SessionContext)

          oEvent.Load(EventSystemID)

    oFundraiserRec.Init(Export.SessionContext)

    oFundraiserRec.LoadByField(bbRecordUniqueFields.uf_Record_CONSTITUENT_ID, FundraiserConstID) 'load fundraiser based on their ID in the import file (stored in variable FundraiserConstID)


    oPart.EventObject = oEvent 'attach the event to the participant


    'link oFundraiserRec to participant and other relevant fields

    With oPart

     .Fields(EParticipantsFields.Participants_fld_KeyIndicator) = bbKeyIndicator.bbki_ORG

     .Fields(EParticipantsFields.Participants_fld_RecordsID) = oFundraiserRec.Fields(ERECORDSFields.RECORDS_fld_ID)

     .Fields(EParticipantsFields.Participants_fld_ConstituentID) = oFundraiserRec.Fields(ERECORDSFields.RECORDS_fld_CONSTITUENT_ID)

     .Fields(EParticipantsFields.Participants_fld_EventID) = EventSystemID

     .Fields(EParticipantsFields.Participants_fld_Attended) = true

     .Fields(EParticipantsFields.Participants_fld_Registration) = "Registered"

     .Fields(EParticipantsFields.Participants_fld_RegistrationDate) = DateTime.Now.ToString("MM/dd/yyyy")

    End with


oPart.Save 'save participant


    oFundraiserRec.CloseDown

    oFundraiserRec = Nothing

    oEvent.CloseDown

    oEvent = Nothing 

Login to post a comment