Tuesday, October 5, 2010

Getting/Creating Accounts/Contacts in crm2011/crm5.0

Creating Contact...........


Contact primaryContact = new Contact()
{
FirstName = "Anuj",
LastName = "Govil",
JobTitle = "Software Programmer"
};
orgContext.AddObject(primaryContact);
orgContext.SaveChanges();


Getting Accounts.....

var accounts = (from acc in orgContext.CreateQuery()
where acc.Name.StartsWith("s")
select new Account()
{
Name = acc.Name,
AccountId = acc.AccountId
}).ToList();

foreach (var acc in accounts)
{
Console.WriteLine(acc.Name);
acc.PrimaryContactId = new EntityReference(primaryContact.LogicalName, primaryContact.Id);
orgContext.UpdateObject(acc);
}
orgContext.SaveChanges();

No comments:

Post a Comment