Thursday, 3 May 2012

Code to create Letter Entity record in CRM 2011


In this article , I am going to explain how to create Letter entity record

For Better Understanding I divided this article in three parts
(i)      Retrieve the Contact records
(ii)     Create an Activity Party record for each Contact
(iii)    Create Letter Activity

Namespace need to include 
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;

Retrieve the Contact records
List<Contact> contacts = (from c in orgContext.CreateQuery<Contact>()
                            where c.Address1_City == "chandigarh"
                            select new Contact
                            {
                                ContactId = c.ContactId,
                                FirstName = c.FirstName,
                                LastName = c.LastName
                            }).ToList<Contact>();

Create an Activity Party record for each Contact
var activityParty1 = new ActivityParty
{
    PartyId = new EntityReference(Contact.EntityLogicalName,
        contacts[0].ContactId.Value),
};

var activityParty2 = new ActivityParty
{
    PartyId = new EntityReference(Contact.EntityLogicalName,
        contacts[1].ContactId.Value),
};

var activityParty3 = new ActivityParty
{
    PartyId = new EntityReference(Contact.EntityLogicalName,
        contacts[2].ContactId.Value),
};

Create Letter Activity
var letter = new Letter
{
    RegardingObjectId = new EntityReference(Contact.EntityLogicalName,
        contacts[2].ContactId.Value),
    Subject = "Activity Letter ",
    ScheduledEnd = DateTime.Now + TimeSpan.FromDays(5),
    Description = @"Greetings, Mr. XYZ,

                    This is a sample letter activity .

                    Sincerely,
                    ABC Co.",
    From = new ActivityParty[] { activityParty1 },
    To = new ActivityParty[] { activityParty3, activityParty2 }
};
orgContext.AddObject(letter);
orgContext.SaveChanges();

Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
                                                    serverConfig.HomeRealmUri,
                                                    serverConfig.Credentials,
                                                    serverConfig.DeviceCredentials))
{
    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());


    OrganizationServiceContext orgContext =
        new OrganizationServiceContext(_serviceProxy);

    // Retrieve the Contact records .
    List<Contact> contacts = (from c in orgContext.CreateQuery<Contact>()
                                where c.Address1_City == "chandigarh"
                                select new Contact
                                {
                                    ContactId = c.ContactId,
                                    FirstName = c.FirstName,
                                    LastName = c.LastName
                                }).ToList<Contact>();

    // Create an Activity Party record for each Contact.
    var activityParty1 = new ActivityParty
    {
        PartyId = new EntityReference(Contact.EntityLogicalName,
            contacts[0].ContactId.Value),
    };

    var activityParty2 = new ActivityParty
    {
        PartyId = new EntityReference(Contact.EntityLogicalName,
            contacts[1].ContactId.Value),
    };

    var activityParty3 = new ActivityParty
    {
        PartyId = new EntityReference(Contact.EntityLogicalName,
            contacts[2].ContactId.Value),
    };


    // Create Letter Activity and set From and To fields to the
    // respective Activity Party entities.
    var letter = new Letter
    {
        RegardingObjectId = new EntityReference(Contact.EntityLogicalName,
            contacts[2].ContactId.Value),
        Subject = "Activity Letter ",
        ScheduledEnd = DateTime.Now + TimeSpan.FromDays(5),
        Description = @"Greetings, Mr. XYZ,

                        This is a sample letter activity.

                        Sincerely,
                        ABC Co.",
        From = new ActivityParty[] { activityParty1 },
        To = new ActivityParty[] { activityParty3, activityParty2 }
    };
    orgContext.AddObject(letter);
    orgContext.SaveChanges();
}


No comments:

Post a Comment

Note: only a member of this blog may post a comment.