CRM 4.0 Helper Class in C#

10. December 2010 19:50

Ok so I was developing some classes to work with CRM 4.0 and noticed I had to keep assigning CRM Types to my ints, doubles, datetimes, etc...  An example of what I mean is something of the following:

 

account objAccount = new account();

if(account.DOB != null)
{
    try
    {
        CrmDateTime dob = new CrmDateTime();
        dob.Value = myObject.DOB; //myObject is the object you are populating with
        objAccount.new_dateofbirth = dob;
    }
    catch{}
}

Ok so notice how you have to set your new date field with the CrmDateTime type. No biggie right, until you have to do this for many fields. Anyway so I wrote this little class to help out with this.  Here are a few of the helpers:

 

public static Key ReturnCRMKey(Guid Value)
{
    Key key = new Key();
    key.Value = Value;

    return key;
}

public static CrmBoolean ReturnCRMBoolean(bool? Value)
{
    CrmBoolean isValue = new CrmBoolean();

    try
    {
        if (Value != null)
        {
            isValue.Value = (bool)Value;
        }
        else
        {
            isValue.Value = false;
        }
    }
    catch (Exception)
    {
        isValue.Value = false;
    }
           

    return isValue;
}

public static Picklist ReturnCRMPickList(int? Value)
{
    Picklist picklist = new Picklist();

    try
    {
        if (Value != null)
        {
            picklist.Value = (int)Value;
        }
        else
        {
            picklist.IsNull = true;
            picklist.IsNullSpecified = true;
        }
    }
    catch (Exception)
    {
        picklist.IsNull = true;
        picklist.IsNullSpecified = true;
    }
           

    return picklist;
}

 

 

So then you can use it accordingly such as:

 

CRMAccount.address1_addresstypecode = Util.ReturnCRMPickList(MyObject.Address1AddressTypeCode);
CRMAccount.new_active = Util.ReturnCRMBoolean(MyObject.Active);
CRMAccount.primarycontactid = Util.ReturnCRMLookup(MyObject.PrimaryContactID, EntityName.account.ToString());

 

File attachement is below. Just the C# file for the class. Expand and use how you'd like.

 

Util.cs (5.67 kb)

 

 

Tags:

CRM

blog comments powered by Disqus



My Random Thought

I think the OCW is a great thing to have available to those who are in school, just finished school or just want to educate themself

http://ocwconsortium.org/

John On Twitter

Discounts