Importing Data in Case (incident) /custom entities in CRM 3.0 with SDK

As i write the code for fun , so i continue with the fun.

As Microsoft CRM does not allow to import data in entities other than “Account”, “Lead”, Contact”, CampaignResponse”. So i decide to use limited knowledge of programming to import data in entities which are not supported by standard Microsoft Import tool. In this example, i will be importing data in Incident (case ) entity , one can extend it to import data in other entities (including custom entities).

Step 1 = I am using Northwind database for my demo

I am going to create connection with a table [Customers] , where i have all data about customers and case ( e.g company name , case title ).

Step 2 = what will be imported ?

I will use [CompanyName] column from Northwinddatabse to create relationship with the “Customer” attribute on case Form . So if there is match between [CompanyName] in database and [AccountName ] on Account entity , then relationship will be created and data will be imported .

Step 3 = After initial steps , now we are ready to write codes

// I will make simple asp.net application with one button (Importcase_Click) and label (label1)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using crm;
using System.Data.SqlClient;

public partial class Case : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Importcase_Click(object sender, EventArgs e)
{

CrmService service = new CrmService();
service.Credentials =
System.Net.CredentialCache.DefaultCredentials;
SqlConnection con = new SqlConnection(@”Data Source=NP_XP_ABA\CRM;Initial Catalog=Northwind;Integrated Security=True”);

// Customers is the table in database

SqlDataAdapter thisAdapter = new SqlDataAdapter(“SELECT * FROM Customers”, con);

DataSet thisDataSet = new DataSet();

thisAdapter.Fill(thisDataSet, “Customers”);

try
{

foreach (DataRow theRow in thisDataSet.Tables[“Customers”].Rows)
{

// i would like to create relationship of “Customer” attribute on Case entity with the //AccountName attribute, so i need to retrieve the accounts

QueryExpression query = new QueryExpression();

query.EntityName = “account”;

ColumnSet columns = new ColumnSet();
columns.Attributes = new string[] { “accountid”, “name” };
query.ColumnSet = columns;

query.Criteria = new FilterExpression();
query.Criteria.FilterOperator = LogicalOperator.And;

ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = “name”;
condition1.Operator = ConditionOperator.Equal;
// CompanyName contains the information about Accounts in database .
condition1.Values = new object[] { (theRow[“CompanyName”].ToString()) };

query.Criteria.Conditions = new ConditionExpression[] { condition1 };

BusinessEntityCollection ret = service.RetrieveMultiple(query);

for (int i = 0; i < ret.BusinessEntities.Length; i++)
{
account see = (account)ret.BusinessEntities[i];

// Now i will create Case and link the case with Account

incident aba1 = new incident();

// I used the CustomerID column from Northwind database and assigned it to the //”Title of Case”.

aba1.title = (theRow[“CustomerID”].ToString());

// now i need to create the relationship between Customer on Incident and Account //name on Account

aba1.customerid = new Customer();
aba1.customerid.type = EntityName.account.ToString();
aba1.customerid.Value = see.accountid.Value;

service.Create(aba1);

Label1.Text = “created”;
}
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
ex.InnerException.ToString();
}

}
}

Advertisements

1 Response to “Importing Data in Case (incident) /custom entities in CRM 3.0 with SDK”


  1. 1 Anonymous January 2, 2008 at 8:40 am

    Hej Aamir.

    Jeg har kontaktet dig et par gange uden du har svaret. Kan du give mig din rigtige e-mail adresse?

    Du kan maile til mig på mh@micropartner.dk

    På forhånd tak


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: