HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Constructor to transform an Outlook contact into a Dynamics CRM contact

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
contactintoconstructoroutlookdynamicstransformcrm

Problem

This is my constructor to transform an Outlook contact into a Dynamics CRM contact:

```
public Contact(ContactItem contactItem)
{
LogicalName = EntityLogicalName;
FirstName = contactItem.FirstName;
LastName = contactItem.LastName;
//if no last name, use the first name
if (String.IsNullOrEmpty(LastName))
{
LastName = contactItem.FirstName;
//if no First name, use the Full Name.
if (String.IsNullOrEmpty(LastName))
{
LastName = contactItem.FullName;
//if no last name or first name, use the company name
if (String.IsNullOrEmpty(LastName))
{
LastName = contactItem.CompanyName;
//If no last name, no first name and no company name, use the email address.
if (String.IsNullOrEmpty(LastName))
{
LastName = contactItem.Email1Address;
//if no Last name, no first name, no company name, no email address, default to naam niet bekend.
if (String.IsNullOrEmpty(LastName))
{
LastName = @"Naam niet bekend";
}
}
}
}
else
{
FirstName = "";
}
}

Address1_City = contactItem.BusinessAddressCity;
Address1_Country = contactItem.BusinessAddressCountry;
Address1_Line1 = contactItem.BusinessAddressStreet;
Address1_PostalCode = contactItem.BusinessAddressPostalCode;
Telephone1 = contactItem.BusinessTelephoneNumber;
Telephone2 = contactItem.HomeTelephoneNumber;
Fax = contactItem.BusinessFaxNumber;
Address2_Line1 = contactItem.HomeAddressStreet;
Address2_City = contactItem.HomeAddressCity;
Address2_PostalCode = contactItem.HomeAddressPostalCode;
Address2_Country = contactItem.HomeAddressCountry;
EMailAddress1 = contactItem.Email1Address;
EMailAddress2 = contactItem.Email2Address;
EM

Solution

-
You could create a helper function with a loop:

public string getFirstNonNullOrEmpty(params string[] names) {
    foreach (string name in names) {
        if (String.IsNullOrEmpty(name)) {
            continue;
        }
        return name;
    }
    return @"Naam niet bekend";
}


Usage:

LastName = getFirstNonNullOrEmpty(contactItem.LastName, contactItem.FirstName, 
    contactItem.FullName, contactItem.CompanyName, contactItem.Email1Address);


You might also need this because of the else branch:

if (String.IsNullOrEmpty(contactItem.LastName)) 
    && !String.IsNullOrEmpty(contactItem.FirstName)) {
    FirstName = "";
}


-
You might want to use IsNullOrWhiteSpace instead of IsNullOrEmpty.

(I'm not too familiar with C# and don't have a C# compiler right now so the syntax might be invalid.)

Code Snippets

public string getFirstNonNullOrEmpty(params string[] names) {
    foreach (string name in names) {
        if (String.IsNullOrEmpty(name)) {
            continue;
        }
        return name;
    }
    return @"Naam niet bekend";
}
LastName = getFirstNonNullOrEmpty(contactItem.LastName, contactItem.FirstName, 
    contactItem.FullName, contactItem.CompanyName, contactItem.Email1Address);
if (String.IsNullOrEmpty(contactItem.LastName)) 
    && !String.IsNullOrEmpty(contactItem.FirstName)) {
    FirstName = "";
}

Context

StackExchange Code Review Q#46797, answer score: 10

Revisions (0)

No revisions yet.