Start a new topic

Changing a virtual field based on date constituent is added

Hi,

I am new to the API and I am trying to change a virtual field that I have mapping to the Acknowledged field.  I want it to change to Not Acknowledged if the constituent record is added to today.  

Can someone help with this code.  


Hi Marie,

This is from a while ago but I just saw it now.

If you are in AfterConstituentOpen you can use oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_DATE_ADDED) to get the date the record was added.

I hope that helps!
Thank you.
How timely - I was just looking to find out how to do this! The only difference is that I want to vary the gift's Campaign ID, but hopefully the principle is the same.

Wayne (or anyone else who knows how!) - would you mind giving an example of how this would look?

oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_DATE_ADDED) .... I assume this be wrapped in some kind of IF statement? How do you say "if it equals today's date (the system date)"?

Also, I can't see a section called AfterConstituentOpen in my profile's code editor...is there another place I could put the code, such as BeforeDictionaries, AfterDictionaries or BeforeGiftSave (or somewhere else? Or maybe I am looking in the wrong place)?

Thank you so much - I would be really grateful if you can spare the time to reply!

Have a lovely day.
For AfterConstituentOpen you have to add it. Just type "Public Overrides" in the editor and a list will come up and you can select AfterConstituentOpen. Then you already have the correct constituent record and you can mess with it before anything is saved!

For the date comparison I think you can do:
If oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_DATE_ADDED) = DateTime.Today.ToString("d") Then
'Set the Campaign ID field
...
End If

1 person likes this
Hi Wayne,

Thanks for your reply! You always seem to know the answer! :-)

I've followed your instructions on how to add AfterConstituentOpen - I was just wondering whether it should go in any particular place - i.e. before or after any particular subs?

Once I've added that in, I'll give the rest of your code a try - exciting!

Sophia
The order doesn't matter so put it wherever you want!
Hi Wayne,

I'm afraid I haven't been able to get it to work. This is what I've got at the moment:

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

If oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_DATE_ADDED) = DateTime.Today.ToString("d") Then
Import.Fields.GetByExcelName("L").Value= "GB0182"

End If

End Sub

The idea is that any gifts on newly-created constituent records should have a Campaign ID of GB0182, as opposed to GB0180 which is what the gift default set associated with this profile would dictate.

However, the new constituents' gifts are still being imported with a campaign ID of GB0180, the default.

I wonder whether this might be an international date format issue? I'm in the UK and our version of RE is set to dd/mm/yyyy date formats; but perhaps this is not the case somewhere in Import-Omatic? Maybe the "date added" isn't in the same format as the DateTime.Today.ToString("d") referenced by the code?

Or maybe not... maybe there is a more obvious (and, hopefully, fixable) solution?

Sophia

Hi Sophia,

Are you sure, when the record was added today, that the values "oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_DATE_ADDED)" and "DateTime.Today.ToString("d")" are exactly equal? You may want to output each of those values to a messagebox on a test row to make sure you're getting the values you expect from them, like this: 

msgbox ("Constit Date = " & oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_DATE_ADDED) & vbCrLf & "Today = " & DateTime.Today.ToString("d"))

The way you are currently comparing is a string comparison (as opposed to a date comparison) so if any part of the string is even the tiniest bit different then the values will not be considered equal. To do a date comparison, you may want to convert both values using CDate (haven't tried it, but ought to work).

Jeff

It might also be that if the constituent is being added by the current operation then Date Added wouldn't be filled in until they are saved. Either way Jeff's suggestion should show you what's up!
Thanks Jeff (and Wayne)!

Excuse my ignorance, but where do I put that messagebox code? I tried pasting it just above End Sub, and then ran an import of two test rows, on Validate mode. Nothing happened... is the messagebox supposed to come up during the import for each row?

Many thanks,

Sophia
If there was no message box then the code didn't run.

Try putting a msgbox line in one of the existing subs just to see if you have that line right. You could put that same line in BeforeConstituentSave and it should work.

Can you post your whole Profile.vb file?
Aha!  It turned out that the profile just hadn't saved (you know how you have to be very careful to Save&Close the code editor, then save the profile itself, etc.... I obviously missed a step).  I tried it again as before, and this time the messagebox came up.

It says:

ConstitDate = 
Today = 09/09/2014

So, I suppose there is nothing there yet for the constituent date.

When I try it with another test file containing an existing constituent, it comes up with this:

ConstitDate = 10/07/2014 13:19:11
Today = 09/09/2014

So, it looks like the RE date has that timestamp on it.  Not sure if there's anything I can do to get the profile to compare the RE date in DD/MM/YYYY format to the system date?
Aha again!

I have tried it again by adding a sub for After Constituent Save, and putting the messagebox code in there.

I then ran the test file of new constituents' gifts.

This time, it came up with today's date (albeit with timestamp)

ConstitDate = 09/09/2014 12:33:48
Today = 09/09/2014


I then tried commenting out the messagebox bit, and moving the actual If statement into AfterConstituentSave, like this (to eliminate the possibility that the sub-procedure was the problem instead of the date format): 


...

Public Overrides Sub AfterConstituentSave(ByVal oRec As Blackbaud.PIA.RE7.BBREAPI.CRecord, ByVal Cancel As ImportOM.API.iCancel)
MyBase.AfterConstituentSave(oRec, Cancel)
'msgbox ("Constit Date = " & oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_DATE_ADDED) & VbCrLf & "Today = " & DateTime.Today.ToString("d"))
If oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_DATE_ADDED) = DateTime.Today.ToString("d") Then
Import.Fields.GetByExcelName("L").Value= "GB0182"
End If

End Sub 

...

But (you will be unsurprised to hear), that didn't work. So, I'm back to thinking about date formats...

Sophia,

The date that RE is giving you can be converted to a date object and then you can use the date object to compare.

If CDate(oRec.Fields(Blackbaud.PIA.RE7.BBREAPI.ERECORDSFields.RECORDS_fld_DATE_ADDED)).ToString("d") = DateTime.Today.ToString("d") Then
Import.Fields.GetByExcelName("L").Value= "GB0182"
End If
Login or Signup to post a comment