Start a new topic

Can API code use a Virtual Field?

Greetings,

It has been years since I coded on a regular basis, and I wish I had more time to spend on it now. I am making progress with using the IOM API by stealing code examples and re-purposing them.  However, I have not found a guide to the basic rules of how/when to call and release fields. Is it possible to use code in the API to manipulate the value of a Virtual Field?

Here is what I am trying to do:

In my data file, I have a field that tells me the '# of children in a household' (Field "P" in the code example below).  I want to combine that data with the 'Membership Level' (also in the data file) to calculate the 'Admit #'.  'Admit #' is not in the data file, but I created it as a Virtual Field (it is a Constituent Attribute Description in Raiser's Edge).

Here is the code:

Public Overrides Sub BeforeConstituentSave(ByVal oRec As Blackbaud.PIA.RE7.BBREAPI.CRecord, _
                                        Cancel As ImportOM.API.iCancel)
                                       
        If Import.Fields.GetByName("Membership Level").Value = "Individual Basic" Then Import.Fields.GetByName("Admit #").Value = 2 Else
        If Import.Fields.GetByName("Membership Level").Value = "Individual Premium" Then Import.Fields.GetByName("Admit #").Value = 3 Else 
        If Import.Fields.GetByName("Membership Level").Value = "Household" Then Import.Fields.GetByName("Admit #").Value = 3 + Import.Fields.GetByExcelName("P").Value Else
        If Import.Fields.GetByName("Membership Level").Value = "Household Premium" Then Import.Fields.GetByName("Admit #").Value = 4 + Import.Fields.GetByExcelName("P").Value
        'Calculates Value for Constituent Attribute "Admit #" based on "Membership Category" & Value of "# Children Under 18 Living at Home".
 
    End Sub

 

The logic makes sense, but I get an error of "Object reference not set to an instance of an object."  So, that tells me that I am not properly addressing the 'Admit #' field.  Right? 

Any help/clues/suggestions that you can provide will be much appreciated!

Thanks,

Mark Mathyer


Hi Mark, welcome!

One issue here is that you are working in BeforeConstituentSave. This is (I believe) after attributes have been processed so I don't think that updating the field will actually accomplish what you are trying to do. In this case, since you are just manipulating fields from the file, you can do this in a "Virtual Column" function which you create using the little Add Virtual Column button at the top of the code editor. This will give you the shell of a new function that you will then be able to access from your profile.

Now, to your original question, I don't really see a problem with this code. That is how you would access a virtual field called "Admit #" as far as I know. Maybe there is no space in the actual column name so it's not finding it? Regardless, if you make this a function (using the Add Virtual Column button) and add it to the Admit # virtual column then you will just be able to return whatever value you want in the column and not actually have to reference it.

Additionally, I think a dictionary would be very helpful in converting the text in Membership Level and P to numbers so that you don't have to hard code it here.

So here is what I would suggest:
1) Create dictionaries to convert the text to numbers for those two columns
2) Create a function (using the Add Virtual Column button in the code editor) to then just add those two columns together and return that value and set that to execute on the Admit # virtual column.

Let us know how it works out!





Wayne,

Thanks for the tip on the Add Virtual Column button - using that function cleared up my errors.

I already have a Dictionary applied to the 'Membership Level' field to convert a user friendly string to the exact text string that I need to match values in Raiser's Edge.  The "# of children in the household" comes in as a number.  I guess I can build a 2nd dictionary for the 'Membership Level' field that translates the name of the level into a numerical value representing the base admit value (that the '# of children...' is then added to).  I wonder if I can apply that new dictionary to the "Admit" virtual field (and change my code from a ".Value =" to a ".Value.replace"), or if I need to create another virtual field for 'Admit base'?  Hmm, I'll try that tomorrow.

(If Forum Protocol dictates that I shouldn't be thinking 'out-loud' here, someone please tell me, so I don't alienate the crowd.)

Thanks again for your quick response - it has been helpful, and given me a new direction to explore.

Mark

 

Hi Mark,

It looks like the custom functions actually happen before dictionaries are normally applied which means my original plan wouldn't exactly work. However, now that it seems you also want to retain the text value of Membership Level as well as use the numeric value associated with that text I would point you towards Import.ApplyDictionary which lets you get the results from applying a dictionary to a string that you send it. So you could build the second dictionary to convert text to numbers and then only call it from your code in the function.

Wayne,
Thanks for warning me about the process order. Is there a reference text where I can look up functions like "Import.AppyDictionary" to see how they work?

Mark
Omatic has a help file here http://www.omaticsoftware.com/Forums/tabid/108/aft/333/Default.aspx

Note that it only works if you open it from your local machine (as the thread talks about). The documentation is fairly basic but it should give you a sense of what is possible. Otherwise I think Jeff and Nic have done a couple of videos about using the API where they go through some of the built in functionality.

But really we are the ones writing the guide here in the forums! Ask your questions and as they get answered we all build up our knowledge a little more.

-Wayne

Hi Mark,

As Wayne mentioned (thanks for the contributions to the forum Wayne!), Jim and Patrick from the Omatic Product Development team put together an introductory video to the ImportOmatic API here:  http://vimeo.com/omaticsoftware/rev...f81a4fed29

As for your question about "Forum Protocol", please contribute or ask anything that you like. The community of people taking advantage of the IOM API is pretty small, but every post with a question or idea or musing about it helps it grow. I think very few people are aware of this powerful engine sitting there in ImportOmatic just waiting to do their drudgery! :)

Jeff

No problem Jeff, IOM API makes my life a lot easier! (and more interesting!)

Also I am trying to get enough points on the forum for my special reward. That exists right?
Wayne,
I had missed the API document. It looks helpful. Thank You.

Jeff,
I watched the video a while ago, and will go through it again now that I'm working with the code. I'll probably get more out of it this time. Thanks.

Mark
Login or Signup to post a comment