Showing posts with label 2011. Show all posts
Showing posts with label 2011. Show all posts

Friday, April 22, 2011

Writing Leads from your corporate web site to on-premise Microsoft Dynamics CRM 2011 using XRM, LINQ to CRM, C#, and Web Services

Scenario

I was asked by our sales team to modify their web form so it wouldn’t just send emails but also write a new Lead to our Dynamics CRM 2011 system.

l4fpv030

>

aocqtheo
Email

+

yzjjyw0f

CRM

Solution

LINQ to CRM Generation

I used Hosk’s method to generate and use the LINQ to CRM classes for our specific server.

 

C# Function to Write Lead to CRM
public void AddLead(string subject, string contactFullName,
string companyName, Guid ownerId, LeadSourceWebSiteName leadSourceWebSiteName,
Uri organizationUri, string runAsUser, string runAsPassword, string runAsDomain, string emailAddress,
string phone, string message)
{
try
{
var credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential(runAsUser, runAsPassword, runAsDomain); ;
var serviceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null);
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
var oc = new ErServiceContext(serviceProxy);
var newLead = new Lead()
{
Subject = subject,
LastName = contactFullName,
CompanyName = companyName,
OwnerId = new EntityReference(SystemUser.EntityLogicalName, ownerId),
LeadSourceCode = new OptionSetValue(8),
new_LeadSourceWebSiteName = leadSourceWebSiteName.ToString(),
EMailAddress1 = emailAddress,
Telephone1 = phone,
Description = string.Format("Message from web lead on {0}: {1}", DateTime.Now, message)
};
//serviceProxy.Create(newLead);
oc.AddObject(newLead);
oc.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
}

Calling it from the Web Form
protected void SendLeadToCrm(string subject, string name, string company, string email, string phone, string message)
{
try
{
//Colyer
var ownerId = new Guid("AB56EF33-4913-DE11-9EE8-00156D0A7301"); //You have to get this from the server
var serviceUrl = new Uri("http://server/company/xrmservices/2011/Organization.svc");
var serviceAccountUser = "serviceAccount";
var serviceAccountPassword = "password";
var crm = new CrmServiceClient();
crm.AddLead("Web Lead", name, company, ownerId, LeadSourceWebSiteName.extendedresults,
serviceUrl, serviceAccountUser, serviceAccountPassword, "yourdomain", email, phone, message);
}
catch (Exception ex)
{
//
}
}

Conclusions


This was a fun but long and drawn out project.  I got to learn a lot about CRM 2011 and how it works with C#.  It is way better than CRM 4.0 and the LINQ to CRM stuff is pretty slick.


I hope you find the above useful.  There are a lot of “hidden tricks” in the code above but I think it’ll get you pointed in the right direction if you dissect it.


Happy programming!

Monday, March 21, 2011

Updating Read-Only Dynamics CRM 2011 Fields (Pipeline Phase and Probability) from a Sales Stage Dropdown

We’re setting up Dynamics CRM 2011 at my workplace and I had to figure out how to set up our pipeline. Here’s my solution:

fffcpplt

I created a Sales Stage field that updates the Pipeline Phase and Probability fields on change. I followed Ayaz Ahmad’s solution for hooking up a javascript function to my Opportunity form and then wrote the following function (after some iteration).

function new_SalesStage_OnChange()
{
var new_salesstageAttr = Xrm.Page.getAttribute("new_salesstage");
var stepnameAttr = Xrm.Page.getAttribute("stepname");
var closeprobabilityAttr = Xrm.Page.getAttribute("closeprobability");

closeprobabilityAttr.setSubmitMode("always");
stepnameAttr.setSubmitMode("always");

if (new_salesstageAttr.getSelectedOption() == null)
{
stepnameAttr.setValue("");
closeprobabilityAttr.setValue(null);
}
else
{
var ssVal = new_salesstageAttr.getSelectedOption().text;
var pl = ssVal.lastIndexOf('%');
var probability = parseInt(ssVal.substring(pl-2, pl));

stepnameAttr.setValue(ssVal);
closeprobabilityAttr.setValue(probability);
}
}

The biggest tricks were:



  • Using LOWER CASE names for my field names.  This breaks if I use getAttribute(“new_SalesStage”), which is it’s real name.  Booo, Microsoft!!!
  • Handling the null value.
  • Using the setSubmitMode(“always”) call to make sure the readonly fields were beiing saved.

This solution should have been quicker but I learned a lot on it.