Posts categorized “VB.NET”.

CRM 3.0, Entity Conversion done easy! Vb.Net

Ok, so your working with Microsoft CRM 3.0, right? And you have hundreds if not thousands of leads. You need to Qualify these leads, and Convert them to Accounts or Contacts. You’ve checked the Web API, but it doesn’t seem possible, or even easy… Well it is. After I spent countless hours surfing google, looking for an answer, I couldn’t find a good one. Here it is folks, and done in the wonderful world of VB instead of C#!

The first order of business is to create a new project, and add the Web Reference for CRM 3.0 to your project. It does not matter what type of project you create, winforms, console, asp.net. This is general VB code that will sit behind what ever interface you choose.

I will not cover CRM Web Reference addition to your project, you should be able to find that through google very easily, or know it before you attempt this :).

First import the CRM namespace. For the purposes of this write up our project is called ConsoleApplication1. And our WebReference is called CRM

Imports ConsoleApplication1.CRM

Next, we need to create an instance of the CRM service.

Dim oCrmService As New CrmService

Great! Now we are getting somewhere :). Lets follow it up by creating a function to return all of our Leads. We will be using FetchXML for this. This will return formatted XML, where you will be able to pull out all of your GUIDs from the LeadID field. The prefered method here, would be to load these into a XML document. I will write an XML memory document post later.

Dim fetch1 As String = _
'change [ and ] to < and > wordpress messed this up
“[fetch mapping='logical']” + _
“[entity name='lead']” + _
“[all-attributes/]” + _
“[/entity]” + _
“[/fetch]”

‘ Fetch the results. Into a String
Dim returnString1 = service.Fetch(fetch1)

To qualify the lead, we do it like this. Very short.

Dim oLead As SetStateLeadRequest = New SetStateLeadRequest 'create request for new change to the state of the lead
'qualify the lead
oLead.EntityId = LeadGUID ' This is the GUID from the fetch statement.
oLead.LeadState = LeadState.Qualified 'set the state of the lead to qualified
oLead.LeadStatus = -1 'not open anymore
oCrmService.Execute(oLead) 'execute the lead change

Now we need to Convert the Lead to an Entity of our Choice.


Dim oContact As InitializeFromRequest = New InitializeFromRequest 'create a request to make a contact
Dim oContactResponse As InitializeFromResponse = New InitializeFromResponse
Dim oContactObject As contact
'setup a entity
oContact.EntityMoniker = New Moniker
oContact.EntityMoniker.Id = LeadGUID
oContact.EntityMoniker.Name = "target entity as string"
oContact.TargetEntityName = TargetEntity
oContact.TargetFieldType = TargetFieldType.All 'target all the records on the lead entity to the target entity
'tell crm server to setup the entity
oContactResponse = oCrmService.Execute(oContact)
'tell crm to create and confirm the entity it setup
oCrmService.Create(oContactResponse.Entity)

Now as you see, this task is much easier than you thought! And there you have it, for you in VB.NET!

Vb.Net: Recursive File Count


Function CountFiles(ByVal Directory As String) As Integer
'Recursive File Counter
'by Derek Anderson
'dim variables
Dim FileCount As Integer = 0
Dim SubDirectory() As String
Dim i As Integer

'get the count of files in the current directory
FileCount = System.IO.Directory.GetFiles(Directory).Length
'get a list of sub directories from current directory
SubDirectory = System.IO.Directory.GetDirectories(Directory)

'for ever sub directory, count the files and add to file count
'this is a recursive routine, so it will call this again and
'return the file count of every directory, and add it to the file
'count originally started
For i = 0 To SubDirectory.Length - 1
FileCount = CountFiles(SubDirectory(i)) + FileCount
Next

Return FileCount

End Function