Archive for November, 2007

comparing CRM DateTime with the Today’s date

I have an example field on CRM account “new_lastupdated”.It is having DateTime format.
I will compare itwith the today’s date. If both are equal then a certain action can take place.Such kind of situation can be required in workflow assembly.In this example , i will be passingt he ID of an existing account.

Guid AccountId = new Guid(“7CD689B6-258C-DC11-A70B-000C29C6C877”);

try

{

account myaccount = (account)service.Retrieve(EntityName.account.ToString(), AccountId, new AllColumns());

if (DateTime.Today == DateTime.Parse(myaccount.new_lastupdated.Value.ToString ()))

{

// just for the sake of example , i will add on day in (new_update) date.

myaccount.new_lastupdated.Value = DateTime.Now.AddDays(1).ToString();

service.Update(myaccount);

}

}

catch (System.Web.Services.Protocols.SoapException ex)

{

ex.InnerException.ToString();

}

Hyper link in CRM email (CRM 3.0)

Scenario

Sometime, we need to send out( there is difference between creating email and sending out email in crm) email from workflow when certain event occur .For example when a case or service activity is created. The person, who get the mail, he/ she needs to search for specific case or service activity .Why can we not give them the possibility of a link (url) to recently created case or service activity.

code :

Here is the url for “service activity”.

string url = “a href=’http://crm/activities/serviceappointment/edit.aspx?id=” + ApointID + “‘>Click here to see the Service activity “;

// Please put < before a href in above above line
// AppointID is the id , which we are passing in workflow

// for example : public void Over(Guid ApointID, Guid OwnerID )

//we can add this “string url” in description (body) of email.

// for example email hotmail = new email();
//hotmail.description = url;

//………..

Bulk deleting and de-activavting accounts in CRM 3.0

In CRM 3.0, we are only able to see 250 records per page.If we have to delete 5000 + records, it becomes nights mare. There is a method in SDK (bulk delete). First of all , we need to retrieve the “Accountid” of all accounts and then I will bulk delete .It is bit risky , make it sure that you are entering the web reference of Test server .

try

{

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
ColumnSet cols = new ColumnSet();
cols.Attributes = new string[] { “accountid” };
ConditionExpression condition = new ConditionExpression();
QueryExpression query1 = new QueryExpression();
query1.EntityName = EntityName.account.ToString();
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
retrieve.Query = query1;

RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)service.Execute(retrieve);

BusinessEntityCollection ret = retrieved.BusinessEntityCollection;

for (int i = 0; i < ret.BusinessEntities.Length; i++)

{

account aba1 = (account)ret.BusinessEntities[i];
service.Delete(EntityName.account.ToString(),ab1.accountid.Value);

// in order to de-activate all accounts , use these piece of codes

SetStateAccountRequest stat = new SetStateAccountRequest();
stat.AccountState = AccountState.Inactive;stat.AccountStatus = -1;
stat.EntityId = aba1.accountid.Value;
SetStateAccountResponse stateSet = (SetStateAccountResponse)service.Execute(stat);

}

}

catch (System.Web.Services.Protocols.SoapException ex)

{

Console.WriteLine(ex.Message + “.” + ex.Detail.InnerText);

}
}
}


Programmatically closing all Open Activities (e.g. Email) in CRM 3.0

First we need to retrieve all”Open” activities (e.g email) . We can use Query expression or FetchXML . I am just skipping that step. Once we are done with query , here is the piece of code, which will close the all Open emails.
BusinessEntityCollection ret = service.RetrieveMultiple(query);

for (int i = 0; i < ret.BusinessEntities.Length; i++)
{

email see = (email)ret.BusinessEntities[i];

SetStateEmailRequest closing = new SetStateEmailRequest();
closing.EmailState = EmailState.Completed;
closing.EmailStatus = -1;

closing.EntityId = see.activityid.Value;
SetStateEmailResponse statset = (SetStateEmailResponse)service.Execute(closing);

}

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();
}

}
}

Changing Data Type of an attribute in CRM 3.0

Once i had a problem with simple nvarchar datatype in CRM . My client wanted to have it as integer so that he can sort it out correctly. Literally speaking , it is really head ache if you have existing data in an attribute and you want to change the data type. As it is not supported in CRM . Today, i was reading in crm news group , a guy was having more or less same problem. I thought to share my experience.

Problem = i have a field with format “Date and Time ” and i would like to move that data to another field , which is having “Date only” format.

Solution : Now i will retrieve all accounts and move the data from one field to another one and then i will be updating the accounts.

protected void Button4_Click1(object sender, EventArgs e)

{

CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
QueryExpression query = new QueryExpression();
query.EntityName = “account”;
ColumnSet columns = new ColumnSet(); columns.Attributes = new string[] { “accountid”, “name”, “new_lastupdated” };

query.ColumnSet = columns;
BusinessEntityCollection ret = service.RetrieveMultiple(query);

for (int i = 0; i < ret.BusinessEntities.Length; i++) {

account see = (account)ret.BusinessEntities[i];
see.new_hello = new CrmDateTime(); // new_hello is is having “date only” format see.new_hello.Value = see.new_lastupdated.date; // “lastupdated ” is having ” date and time” format
service.Update(see);

Label1.Text = “updated”;

}