Monday, December 10, 2007

Write Log file. After specified file size save as it as

public void createLog(string msg)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/OpenActiityLog.txt"));
FileInfo f = new FileInfo(Server.MapPath("~/OpenActiityLog.txt"));
int i = Convert.ToInt32(f.Length.ToString());

if (i < sw =" new" date="">" + DateTime.Now.ToString() + "- " + msg.ToString());
sw.WriteLine();
sw.WriteLine("============================================================================================");
sw.WriteLine();
sw.Flush();
sw.Close();
}
else
{
string sdate = System.DateTime.Today.ToShortDateString();
string temp = sdate.Replace('/', '-');
f.CopyTo(Server.MapPath("~/OpenActiityLog.txt") + temp + ".txt", true);


StreamWriter sw = new StreamWriter(Server.MapPath("~/OpenActiityLog.txt"), false);
sw.WriteLine("Date=>" + DateTime.Now.ToString() + "- " + msg.ToString());
sw.WriteLine();
sw.WriteLine("============================================================================================");
sw.WriteLine();
sw.Flush();
sw.Close();

}

Friday, December 7, 2007

Create new Phonecall \ Email Activity from existting one.

Create new Phonecall \ Email Activity from existting one.

String IdG=”Guid of ur Activity”

Guid LeadId = new Guid(IdG);

CrmService service = new CrmService();
service.Url = ConfigurationManager.AppSettings["Url"];
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
//service.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["CredentialsUName"], ConfigurationManager.AppSettings["CredentialsPwd"], ConfigurationManager.AppSettings["CredentialsDomain"]);

ColumnSet cols = new ColumnSet();
cols.Attributes = new string[] { "activityid", "from", "to", "phonenumber", "directioncode", "subject", "regardingobjectid", "ownerid", "actualdurationminutes", "prioritycode", "scheduledend", "category", "subcategory" };

ConditionExpression cond1 = new ConditionExpression();
cond1.AttributeName = "activityid";
cond1.Operator = ConditionOperator.Equal;
cond1.Values = new string[] { LeadId.ToString() };

FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] { cond1 };


QueryExpression query = new QueryExpression();
query.EntityName = EntityName.phonecall.ToString();
query.ColumnSet = cols;
query.Criteria = filter;

BusinessEntityCollection retrived = service.RetrieveMultiple(query);
phonecall actpointer = new phonecall();
if (retrived.BusinessEntities.Length > 0)
{

for (int j = 0; j < retrived.BusinessEntities.Length; j++)
{

phonecall ld = (phonecall)retrived.BusinessEntities[j];

if (ld.subject != null)
{
actpointer.subject = ld.subject.ToString();
}
else
{
actpointer.subject = null;
}


if (ld.phonenumber != null)
{
actpointer.phonenumber = ld.phonenumber;
}
else
{
actpointer.phonenumber = null;
}

Picklist p = new Picklist();
if (ld.prioritycode.Value != null)
{
p.Value = ld.prioritycode.Value;
actpointer.prioritycode = p;
}
else
{
p.Value = 1;
actpointer.prioritycode = p;
}

if (ld.category != null)
{
actpointer.category = ld.category.ToString();
}
else
{
actpointer.category = null;
}

if (ld.subcategory != null)
{
actpointer.subcategory = ld.subcategory.ToString();
}
else
{
actpointer.subcategory = null;
}

if (ld.ownerid.Value.ToString() != null)
{
Guid ow = new Guid(ld.ownerid.Value.ToString());

actpointer.ownerid = new Owner();
actpointer.ownerid.type = EntityName.systemuser.ToString();
actpointer.ownerid.Value = ow;

activityparty party = new activityparty();
party.partyid = new Lookup();
party.partyid.type = EntityName.systemuser.ToString();
party.partyid.Value = ow;
actpointer.from = new activityparty[] { party };
}



activityparty[] act = ld.to;

if (act[0].partyid.Value.ToString() != null)
{
Guid ow1 = new Guid(act[0].partyid.Value.ToString());
activityparty party1 = new activityparty();
party1.partyid = new Lookup();
party1.partyid.type = EntityName.lead.ToString();
party1.partyid.Value = ow1;
actpointer.to = new activityparty[] { party1 };
}

if (ld.regardingobjectid.Value != null)
{
Guid ow2 = new Guid(ld.regardingobjectid.Value.ToString());
Lookup lookup = new Lookup();
lookup.Value = ow2;
lookup.type = EntityName.lead.ToString();
actpointer.regardingobjectid = lookup;
}

if (ld.directioncode.Value != null)
{

CrmBoolean dc = new CrmBoolean();
dc.Value = ld.directioncode.Value;
actpointer.directioncode = new CrmBoolean();
actpointer.directioncode.Value = Convert.ToBoolean(dc.Value.ToString());
}
else
{
string stt = "true";
CrmBoolean dc = new CrmBoolean();
dc.Value = Convert.ToBoolean(stt.ToString());
actpointer.directioncode = new CrmBoolean();
actpointer.directioncode.Value = Convert.ToBoolean(dc.Value.ToString());

}


actpointer.actualdurationminutes = new CrmNumber();
int v = Convert.ToInt32(ld.actualdurationminutes.Value.ToString());
actpointer.actualdurationminutes.Value = Convert.ToInt32(30);

DateTime testdate = new DateTime();
testdate = Convert.ToDateTime(System.DateTime.Now.Date.ToString());//Convert.ToDateTime(dt.date.ToString());
DateTime testdate1 = new DateTime();
testdate1 = Convert.ToDateTime(testdate.AddDays(1).ToString());
if (testdate1.DayOfWeek.ToString().Trim() == "Sunday")
{
testdate1 = Convert.ToDateTime(testdate1.AddDays(1).ToString());
CrmDateTime dt1 = new CrmDateTime();
dt1.Value = testdate1.ToString();
actpointer.scheduledend = new CrmDateTime();
actpointer.scheduledend.Value = dt1.Value;
Guid aid = service.Create(actpointer);
newday = testdate1.DayOfWeek.ToString().Trim() + " " + testdate1.Date.ToString();
CloseActivity(objId);
Label1.Text = "New Phone Call Activity is created on " + newday.ToString();


}

else if (testdate1.DayOfWeek.ToString().Trim() == "Saturday")
{
testdate1 = Convert.ToDateTime(testdate1.AddDays(2).ToString());
CrmDateTime dt1 = new CrmDateTime();
dt1.Value = testdate1.ToString();
actpointer.scheduledend = new CrmDateTime();
actpointer.scheduledend.Value = dt1.Value;
Guid aid = service.Create(actpointer);
newday = testdate1.DayOfWeek.ToString().Trim() + " " + testdate1.Date.ToString();
CloseActivity(objId);
Label1.Text = "New Phone Call Activity is created on " + newday.ToString();

}
else
{

CrmDateTime dt1 = new CrmDateTime();
dt1.Value = testdate1.ToString();
actpointer.scheduledend = new CrmDateTime();
actpointer.scheduledend.Value = dt1.Value;
Guid aid = service.Create(actpointer);
newday = testdate1.DayOfWeek.ToString().Trim() + " " + testdate1.Date.ToString();
CloseActivity(objId);
Label1.Text = "New Phone Call Activity is created on " + newday.ToString();

}

break;
}
}
else
{
Label1.Text = "Cannot create New PhoneCall Activity for this Activity.";
}

Change status of Phone Call / Email Activity In CRM3.0

Change status of Phone Call / Email Activity In CRM3.0


You can change status of any activity by two ways.

Here I am showing example of Phone Call activity.

First Way:

try
{
CrmService service = new CrmService();
service.Url = ConfigurationManager.AppSettings["Url"];
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

SetStatePhoneCallRequest state = new SetStatePhoneCallRequest();

// Set the properties of the request object.
state.PhoneCallState = PhoneCallState.Completed;
state.PhoneCallStatus = -1;
state.EntityId = new Guid(“Guid of activity”);

SetStatePhoneCallResponse stateSet = (SetStatePhoneCallResponse)service.Execute(state);

}
catch (Exception ex)
{
string ms = ex.Message;
}



Second Way :


try
{
CrmService service = new CrmService();
service.Url = ConfigurationManager.AppSettings["Url"];
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

incident changest=new incident();

Status st=new Status();
st.Value=11;//Status code u want to set.
changest.statuscode=st;

changest.incidentid=new Key();
Guid gd=new Guid(CaseId);

changest.incidentid.Value=gd;

service.Update(changest);

}
catch (Exception ex)
{
string ms = ex.Message;
}

Show a popup message on Page in CRM 3.0 On Mouse Move



Write down following code on Pageload event of CRM form(Popup will move with ur mouse pointer.)
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
var CRM_FORM_TYPE_READ_ONLY = 3;
var CRM_FORM_TYPE_DISABLED = 4;
var CRM_FORM_TYPE_QUICK_CREATE = 5;
var CRM_FORM_TYPE_BULK_EDIT = 6;
switch (crmForm.FormType)
{
case CRM_FORM_TYPE_CREATE :
case CRM_FORM_TYPE_UPDATE :
case CRM_FORM_TYPE_BULK_EDIT :
var oPopup = window.createPopup();
var oPopupBody = oPopup.document.body;
oPopupBody.style.backgroundColor = 'yellow';
oPopupBody.style.fontFamily = 'Tahoma';
oPopupBody.style.border = '2px solid black';

function My_OrgName_showRatingPopup ()
{
oPopupBody.innerHTML = "_div>_table style='font-size:xx-small'>";

oPopupBody.innerHTML += "_tr>_td>_b>Note :Sent Button Click :_/b>The system will mark this cases as resolved. If you want to keep this case open or on hold, then please reactivate this case and change the case status._/td>_/tr>_/font>_/table>_/div>";var
x_coord = -50;
var y_coord = -70;
var width =230;
var height = 60;
var documentElement = event.srcElement;
oPopup.show(x_coord, y_coord, width, height, documentElement);
}

crmForm.attachEvent('onmouseover', My_OrgName_showRatingPopup);

}

show Popup on CRM form

var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
var CRM_FORM_TYPE_READ_ONLY = 3;
var CRM_FORM_TYPE_DISABLED = 4;
var CRM_FORM_TYPE_QUICK_CREATE = 5;
var CRM_FORM_TYPE_BULK_EDIT = 6;switch (crmForm.FormType)
{
case CRM_FORM_TYPE_CREATE :
case CRM_FORM_TYPE_UPDATE :
case CRM_FORM_TYPE_BULK_EDIT :
// different functions or events in the form.
var oPopup = window.createPopup();
var oPopupBody = oPopup.document.body;
oPopupBody.style.backgroundColor = 'yellow';
oPopupBody.style.fontFamily = 'Tahoma';
oPopupBody.style.border = '2px solid black';


//Create the function that will be attached to the field mouseover event.

function My_OrgName_showRatingPopup ()
{
// Set the message HTML.
oPopupBody.innerHTML = "
";
oPopupBody.innerHTML += "
Note :Sent Button Click :The system will mark this cases as resolved. If you want to keep this case open or on hold, then please reactivate this case and change the case status.
";
// Set the location and size of the pop-up message.
// The x and y coordinates are relative to the form element.
var x_coord = -50;
var y_coord = -70;
var width =230;
var height = 60;
// Associate the pop-up message with the element where the event // occurred.
var documentElement = event.srcElement;
// Show the pop-up message.
oPopup.show(x_coord, y_coord, width, height, documentElement);
}

// Attach the onmouseover event to the field and the function.
// It is important that this be done in script after the function is defined.

crmForm.attachEvent('onmouseover', My_OrgName_showRatingPopup);


}

Resolve cases In CRM 3.0

incidentresolution i_Resolution = new incidentresolution();
Lookup mylookup = new Lookup();
mylookup.Value = new Guid(CaseId.ToString());// CaseId=Ur case guid
i_Resolution.incidentid = mylookup;
Status mystatus = new Status();
mystatus.Value = 2; //completed
i_Resolution.statuscode = mystatus;


IncidentResolutionStateInfo i_stateinfo = new IncidentResolutionStateInfo();
i_stateinfo.Value = IncidentResolutionState.Completed;
i_Resolution.statecode = i_stateinfo;
CloseIncidentRequest i_closereq = new CloseIncidentRequest();
i_closereq.IncidentResolution = i_Resolution;
i_closereq.Status = -1;
CloseIncidentResponse myresponse =(CloseIncidentResponse)service.Execute(i_closereq);