protected void Page_Load object sender EventArgs if IsPostBack try Fil

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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;
}
}
}