Skip to content
nairdo edited this page Feb 17, 2014 · 1 revision

Avoiding Duplicates

Normally you'll always want a person to login in order to do something with their record. However in certain cases, it might be considered OK to tie new data to their record without having them login (authenticate) first.

In situations where you've got a person's first name, last name, and email address, and you need to either get the existing Person record or create a new record (in order to do something with it), you should use the following strategy illustrated by the sample code shown below. However you must always abide by the App Laws regarding matching an existing person. NEVER connect the authenticated person who provides the simple details (first, last, email) to the matched record because it could be an attempt to hijack the record.

public Person GetOrCreatePerson( string firstName, string lastName, string email )
{
  Person person;
  var personService = new PersonService();

  if ( CurrentPerson != null )
  {
      person = CurrentPerson;
  }
  else
  {
      var people = personService.GetByMatch( firstName, lastName, email );
      if ( people.Count() == 1 )
      {
          person = people.FirstOrDefault();
      }
      else
      {
          // TODO multiple matches, identify the correct person otherwise you're creating duplicates.
          person = null;
      }
  }

  if ( person == null )
  {
      var definedValue = DefinedValueCache.Read( new Guid( GetAttributeValue( "DefaultConnectionStatus" ) ) );
      person = new Person
      {
          FirstName = firstName,
          LastName = lastName,
          Email = email,
          ConnectionStatusValueId = definedValue.Id,
      };

      new GroupService().SaveNewFamily( person, null, CurrentPersonAlias );
  }

  return person;
Clone this wiki locally