protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
//FileInfo f = new FileInfo(@"c:\temp\RandDcreatePage1.txt");
//w = f.AppendText();
//w.WriteLine("START R and D create Page");
string id = "";
crmService = createService();
#if DEBUG
id = "9E1215D6-7366-DE11-94B2-001D09681BEB";
#else
if (Request.QueryString["id"] != null)
{
id = Request.QueryString["id"].ToString();
}
#endif
ColumnSet col = new ColumnSet();
col.Attributes = new string[] { "incidentid", "description", "statuscode", "customerid", "responsiblecontactid", "title", "new_randdrelatedrecordid" };
BusinessEntity be = crmService.Retrieve(EntityName.incident.ToString(), new Guid(id), col);
currentCase = (incident)be;
// Check if currentCase is not null
// that case that triggered the program is active
// and that there is no randd record related to the current case
if (currentCase != null && (currentCase.statuscode.Value != 5 || currentCase.statuscode.Value != 6) && currentCase.new_randdrelatedrecordid == null)
{
#region Retrieve Enable Users
// Retrieve Enable Users
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "isdisabled";
condition.Operator = ConditionOperator.Equal;
condition.Values = new string[] { "false" };
FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] { condition };
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.systemuser.ToString();
query.ColumnSet = new AllColumns();
query.Criteria = filter;
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
retrieve.Query = query;
RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(retrieve);
#endregion Retrieve Enable Users
ListItem item;
systemuser user;
List<ListItem> list = null;
// Fill Drop Down List with retrieved users
if (retrieved != null && retrieved.BusinessEntityCollection.BusinessEntities.Length > 0)
{
list = new List<ListItem>();
foreach (BusinessEntity entity in retrieved.BusinessEntityCollection.BusinessEntities)
{
item = new ListItem();
user = (systemuser)entity;
item.Value = user.systemuserid.Value.ToString();
item.Text = user.fullname;
list.Add(item);
}
}
if (list != null && list.Count > 0)
{
list.Sort(this as IComparer<ListItem>);
foreach (ListItem li in list)
{
ddlAssignTo.Items.Add(li);
}
}
if (currentCase != null)
{
Cache["Case"] = currentCase;
}
if (currentCase.title != null)
{
lblNewName.Text = string.Format("New R and D record {0} was created successfuly",currentCase.title);
}
// Set attributes for the new R and D record
new_randd randd = new new_randd();
randd.new_description = currentCase.description;
randd.new_name = currentCase.title;
randd.new_customerid = new Lookup();
randd.new_customerid.Value = ((Customer)currentCase.customerid).Value;
randd.new_customerid.name = ((Customer)currentCase.customerid).name;
randd.new_relatedcaseid = new Lookup();
randd.new_relatedcaseid.Value = currentCase.incidentid.Value;
randd.new_relatedcaseid.name = currentCase.title;
randd.new_responsiblecontactid = currentCase.responsiblecontactid;
// Create new R and D record
newRandD = crmService.Create(randd);
// Update the case that triggered the program
if (newRandD != null && newRandD != Guid.Empty)
{
currentCase.new_randdrelatedrecordid = new Lookup();
currentCase.new_randdrelatedrecordid.Value = newRandD;
crmService.Update(currentCase);
Cache["Case"] = currentCase;
}
Cache["randdID"] = newRandD;
}
else
{
lblExists.Visible = true;
table.Visible = false;
table2.Visible = false;
}
}
catch (SoapException ex)
{
//w.WriteLine("SoapException : " + ex.Message);
//w.WriteLine();
//w.WriteLine("SoapException detail : " + ex.Detail.InnerText);
//w.WriteLine();
throw new Exception("SoapException : An error occurred in the R and D create Page." + ex.Message);
}
catch (Exception ex)
{
lblExists.Text = " R&D create failed. Please contact your administrator";
lblExists.Visible = true;
table.Visible = false;
table2.Visible = false;
//w.WriteLine("Exception : " + ex.Message);
//w.WriteLine();
throw new Exception("An error occurred in the R and D create Page." + ex.Message);
}
finally
{
//w.WriteLine();
//w.WriteLine("end R and D create Page");
//w.Close();
}
}
}
protected void btnAssign_Click(object sender, EventArgs e)
{
if (Cache["randdID"] != null && Cache["Case"] != null && ddlAssignTo.SelectedIndex > 0)
{
try
{
crmService = createService();
currentCase = (incident)Cache["Case"];
new_randd randd = new new_randd();
randd.new_randdid = new Key();
randd.new_randdid.Value = (Guid)Cache["randdID"];
randd.ownerid = new Owner();
randd.ownerid.Value = new Guid(ddlAssignTo.SelectedValue);
randd.new_responsibleuserid = new Lookup();
randd.new_responsibleuserid.Value = new Guid(ddlAssignTo.SelectedValue);
// Assign the new R and D record to specific user
SecurityPrincipal assignee = new SecurityPrincipal();
assignee.Type = SecurityPrincipalType.User;
assignee.PrincipalId = new Guid(ddlAssignTo.SelectedValue);
TargetOwnedDynamic target = new TargetOwnedDynamic();
target.EntityId = (Guid)Cache["randdID"];
target.EntityName = EntityName.new_randd.ToString();
AssignRequest assign = new AssignRequest();
assign.Assignee = assignee;
assign.Target = target;
AssignResponse assignResponse = (AssignResponse)crmService.Execute(assign);
crmService.Update(randd);
// Update Responsible User field of Current Case
currentCase.new_responsibleuserid = new Lookup();
currentCase.new_responsibleuserid.Value = new Guid(ddlAssignTo.SelectedValue);
crmService.Update(currentCase);
btnAssign.Enabled = false;
btnAssign.Visible = false;
ddlAssignTo.SelectedIndex = 0;
ddlAssignTo.Enabled = false;
lblResult.Text = " R and D record was assigned successfuly";
lblResult.Visible = true;
}
catch (Exception ex)
{
btnAssign.Enabled = false;
btnAssign.Visible = false;
ddlAssignTo.SelectedIndex = 0;
ddlAssignTo.Enabled = false;
lblResult.Text = " R&D assigning to specific user failed. Please contact your administrator";
lblResult.Visible = true;
}
}
}