Friday, April 24, 2009

Shared Variable in JScript

An occasion came up where the value of a field needed to be masked but still allow the correct value to be saved to the database. Envision sensitive information like social security numbers, credit card numbers, etc. These are things that data entry people need to enter but not necessarily see the entire number after they enter it.

You could use another field to house the real information and hide that field on the form so no one can see it, but then you run the risk of having that information in another place unnecessarily. Also, if someone is going to this length to make sure users can't see this information they probably want to make sure that the data gets encrypted in the database so that users couldn't get the information from the grid any way.

So, to keep this more hidden and more secure you actually can use a variable between your methods on the same document. It's actually really easy, here's how:

In the OnLoad of the form write:

document.newvariable = 5;

Now in your OnChange or OnSave you can reference that variable the same way.

crmForm.all.new_securefield.DataValue = document.newvariable;

So, if you wanted to mask something:

In the OnLoad:
document.originalvalue = crmForm.all.new_securefield.DataValue;
crmForm.all.new_securefield.DataValue = "xxxxxxxx" + crmForm.all.new_securefield.DataValue.SubString(8, crmForm.all.new_securefield.DataValue.Length - 8);

And now in the OnSave:
//Hide the field so the real value doesn't appear just before the save = 'hidden';
crmForm.all.new_securefield.DataValue = document.newvariable;

Simple but effective for keeping data secure on the form. I'm sure there are many more uses for global variables like this, this is just one way that it can be implemented.

David Fronk
Dynamic Methods Inc.

No comments:

Post a Comment