Pages

Tuesday, 1 May 2012

Code to Qualify a Lead in CRM 2011


In this article , I am going to explain how to Qualify a lead as Account,contact or Opportunity

Creating Leads Record 

    var lead1 = new Lead
    {
        CompanyName = "ITSoft",
        FirstName = "NAvish",
        LastName = "JAin",
        Subject = "Sample Lead 1"
     };
     Guid _lead1Id = _serviceProxy.Create(lead1);

     var lead2 = new Lead
     {
       CompanyName = "ITSoft Works",
       FirstName = "Sheenam",
       LastName = "Jain",
       Subject = "Sample Lead 2"
     };
     Guid _lead2Id = _serviceProxy.Create(lead2);



Code to Qualify Lead as Account and Contact 

     var qualifyIntoAccountContactReq = new QualifyLeadRequest
     {
         CreateAccount = true,
         CreateContact = true,
         LeadId = new EntityReference(Lead.EntityLogicalName, _lead1Id),
         Status = new OptionSetValue((int)lead_statuscode.Qualified)
     };
     var qualifyIntoAccountContactRes =
                    (QualifyLeadResponse)_serviceProxy.Execute(qualifyIntoAccountContactReq);
     foreach (var entity in qualifyIntoAccountContactRes.CreatedEntities)
     {
        if (entity.LogicalName == Account.EntityLogicalName)
        {
            Guid _leadAccountId = entity.Id;
        }
        else if (entity.LogicalName == Contact.EntityLogicalName)
        {
            Guid _contactId = entity.Id;
        }
     }




Retrieve the organization's base currency ID for setting the transaction currency of the opportunity.

     var query = new QueryExpression("organization");
     query.ColumnSet = new ColumnSet("basecurrencyid");
     var result = _serviceProxy.RetrieveMultiple(query);
     var currencyId = (EntityReference)result.Entities[0]["basecurrencyid"];



Code to Qualify Lead as Opportunity

     // Create an account to relate the opportunity to.
     var account = new Account
     {
       Name = "ITSoft Solutions",
       Address1_StateOrProvince = "Chandigarh"
     };
     Guid _accountId = (_serviceProxy.Create(account));
     // Qualify the second lead, creating an opportunity from it. We use an existing account for the
     // opportunity customer instead.
     var qualifyIntoOpportunityReq = new QualifyLeadRequest
     {
       CreateOpportunity = true,
       OpportunityCurrencyId = currencyId,
       OpportunityCustomerId = new EntityReference(
                        Account.EntityLogicalName,
                        _accountId),
       Status = new OptionSetValue((int)lead_statuscode.Qualified),
       LeadId = new EntityReference(Lead.EntityLogicalName, _lead2Id)
     };
     var qualifyIntoOpportunityRes =
                    (QualifyLeadResponse)_serviceProxy.Execute(qualifyIntoOpportunityReq);


     foreach (var entity in qualifyIntoOpportunityRes.CreatedEntities)
     {
        if (entity.LogicalName == Opportunity.EntityLogicalName)
        {
           Guid _opportunityId = entity.Id;
        }
      }





Complete code to qualify lead to Account,Contact and Opportunity

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

     // Create two leads.
     var lead1 = new Lead
     {
        CompanyName = "ITSoft",
        FirstName = "NAvish",
        LastName = "JAin",
        Subject = "Sample Lead 1"
     };
     Guid _lead1Id = _serviceProxy.Create(lead1);

     var lead2 = new Lead
     {
       CompanyName = "ITSoft Works",
       FirstName = "Sheenam",
       LastName = "Jain",
       Subject = "Sample Lead 2"
     };
     Guid _lead2Id = _serviceProxy.Create(lead2);

     // Qualify the first lead, creating an account and a contact from it
     var qualifyIntoAccountContactReq = new QualifyLeadRequest
     {
         CreateAccount = true,
         CreateContact = true,
         LeadId = new EntityReference(Lead.EntityLogicalName, _lead1Id),
         Status = new OptionSetValue((int)lead_statuscode.Qualified)
     };
     var qualifyIntoAccountContactRes =
                    (QualifyLeadResponse)_serviceProxy.Execute(qualifyIntoAccountContactReq);
     foreach (var entity in qualifyIntoAccountContactRes.CreatedEntities)
     {
        if (entity.LogicalName == Account.EntityLogicalName)
        {
            Guid _leadAccountId = entity.Id;
        }
        else if (entity.LogicalName == Contact.EntityLogicalName)
        {
            Guid _contactId = entity.Id;
        }
     }


     // Retrieve the organization's base currency ID for setting the
     // transaction currency of the opportunity.
     var query = new QueryExpression("organization");
     query.ColumnSet = new ColumnSet("basecurrencyid");
     var result = _serviceProxy.RetrieveMultiple(query);
     var currencyId = (EntityReference)result.Entities[0]["basecurrencyid"];


     // Create an account to relate the opportunity to.
     var account = new Account
     {
       Name = "ITSoft Solutions",
       Address1_StateOrProvince = "Chandigarh"
     };
     Guid _accountId = (_serviceProxy.Create(account));
     // Qualify the second lead, creating an opportunity from it. We use an existing account for the
     // opportunity customer instead.
     var qualifyIntoOpportunityReq = new QualifyLeadRequest
     {
       CreateOpportunity = true,
       OpportunityCurrencyId = currencyId,
       OpportunityCustomerId = new EntityReference(
                        Account.EntityLogicalName,
                        _accountId),
       Status = new OptionSetValue((int)lead_statuscode.Qualified),
       LeadId = new EntityReference(Lead.EntityLogicalName, _lead2Id)
     };
     var qualifyIntoOpportunityRes =
                    (QualifyLeadResponse)_serviceProxy.Execute(qualifyIntoOpportunityReq);


     foreach (var entity in qualifyIntoOpportunityRes.CreatedEntities)
     {
        if (entity.LogicalName == Opportunity.EntityLogicalName)
        {
           Guid _opportunityId = entity.Id;
        }
      }

 }

No comments:

Post a Comment

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