Advertisement

06.18.2008 at 01:28PM PDT, ID: 23496765
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

CRM Callout - Phonecall Can't Update (The object can not be updated because it is read-only)

Asked by mattskelton in Microsoft Dynamics, C# Programming Language

Tags: , , , ,

Hi Experts,

I am trying to update the Sender fields of a phonecall. Essentially, what I am trying to achieve is: When an Account is transfered to a new user, the Contacts and Activities  are transfered to that user to. Unfortunately, the Sender field in the Activity remains in the old ower's name and therefore still appears in their "My Activities". I wrote this callout to drill down through the Acctount=>Contact=>Activities and update the Activities Sender fields. The callout fails with an error code "8004022e" and error message "The object can not be updated because it is read-only because it is read-only".  

Any ideas,

MattStart Free Trial
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:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
/*
 * File:		Sender.cs
 * Author:		Matt Skelton
 * Date:		June 11, 2008		: Created
 *				June 16, 2008		: Upated
 * 
 * Description:	This class is fired before an Account (Facility)
 *				is assigned from one user to another user. 
 *				It drills down from the entity to all of its 
 *				Activities (Phonecalls, Letters, Faxes, Emails)
 *				and assigns the Sender field the value of the new user.
 * 
 *				This is needed to remove the Activities from the
 *				old user's My Activities view.
 */
 
//Error
//Server was unable to process request..
//0x8004022e
//The object cannot be updated because it is read-only.
 
using System;
using System.Collections;
using Microsoft.Crm.Callout;
using ChangeSenderOnTransfer;
using ChangeSenderOnTransfer.brodacrm;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
 
namespace ChangeSenderOnTransfer
{
	/// <summary>
	/// Summary description for Sender.
	/// </summary>
	public class Sender : CrmCalloutBase
	{
		private CrmService service;
		private Guid assignedToId;
		private ArrayList contactList	= new ArrayList();
		private ArrayList phonecallList = new ArrayList();
		private ArrayList letterList	= new ArrayList();
		private ArrayList emailList		= new ArrayList();
		private ArrayList faxList		= new ArrayList();
		
		public override void PostAssign(CalloutUserContext userContext, CalloutEntityContext entityContext,
			string preImageEntityXml, string postImageEntityXml)
		{
			// Set up the CrmService
			service								= new CrmService();
			service.Url							= "http://brodacrm:5555/mscrmservices/2006/crmservice.asmx";
			service.CallerIdValue				= new CallerId();
			service.CallerIdValue.CallerGuid	= new Guid("A016F63C-4370-DC11-85F6-001372F8A7FD");
			service.Credentials					= System.Net.CredentialCache.DefaultCredentials;
 
			System.Xml.XmlDocument postImageEntity = new XmlDocument();
			postImageEntity.LoadXml(postImageEntityXml);
 
			foreach (XmlElement element in postImageEntity.GetElementsByTagName("Property"))
			{
				string elementName = element.Attributes.GetNamedItem("Name").Value;
 
				switch(elementName)
				{
					case "ownerid":
 
						assignedToId = new Guid(element.FirstChild.InnerText);
 
						break;
				}
			}
			
			switch (entityContext.EntityTypeCode)
			{
				//Account
				case 1:
					
					contactList = RetrieveContactsForAccount(entityContext.InstanceId);
					
					foreach (contact contact in contactList)
					{
						ClearLists();
 
						phonecallList	= RetrievePhonecallsForContact(contact.contactid.Value);
						letterList		= RetrieveLettersForContact(contact.contactid.Value);
						emailList		= RetrieveEmailsForContact(contact.contactid.Value);
						faxList			= RetrieveFaxesForContact(contact.contactid.Value);
 
						try
						{
							if (IsValidGuid(assignedToId.ToString()))
							{
								UpdateActivities(assignedToId);
							}
							else
							{
								throw new Exception("assignedToId != Valid Guid");
							}
						}
						catch(System.Web.Services.Protocols.SoapException ex)
						{
							TextWriter log = TextWriter.Synchronized(File.AppendText(@"C:\CRM_SDK_Drop\Assign.txt"));
							log.WriteLine("Error");
							log.WriteLine(ex.Message + "." + ex.Detail.InnerText);
							log.WriteLine();
							log.Close();
						}
						catch(Exception ex)
						{
							TextWriter log = TextWriter.Synchronized(File.AppendText(@"C:\CRM_SDK_Drop\Assign.txt"));
							log.WriteLine("Error");
							log.WriteLine(ex.Message);
							log.WriteLine();
							log.Close();
						}
					}
	
					break;
			}
		}
 
		#region Helper Methods
 
		/// <summary>
		/// Returns true if argument is valid Guid
		/// </summary>
		/// <param name="guidCandidate"></param>
		/// <returns></returns>
		private bool IsValidGuid(string testGuid)
		{
			bool isValidGuid	= false;
			
			try
			{
				Guid guid		= new Guid(testGuid);
				isValidGuid		= true;
			}
			catch (Exception)
			{
				//
			}
			
			return isValidGuid;
		}
		
		/// <summary>
		/// Updates Phonecall, Email, Letter, and Fax activies
		/// </summary>
		private void UpdateActivities(Guid assignedToId)
		{
			foreach (phonecall phonecall in phonecallList)
			{
				UpdatePhonecall(phonecall.activityid.Value, assignedToId);
			}
 
			foreach (letter letter in letterList)
			{
				UpdateLetter(letter.activityid.Value, assignedToId);
			}
 
			foreach (email email in emailList)
			{
				UpdateEmail(email.activityid.Value, assignedToId);
			}
 
			foreach (fax fax in faxList)
			{
				UpdateEmail(fax.activityid.Value, assignedToId);
			}
		}
		
		/// <summary>
		/// Clears ArrayLists
		/// </summary>
		private void ClearLists()
		{
			phonecallList.Clear();
			letterList.Clear();
			emailList.Clear();
			faxList.Clear();
		}
 
		/// <summary>
		/// Updates a Phonecall's From/Sender field to a new UserId
		/// </summary>
		/// <param name="phonecallId"></param>
		private void UpdatePhonecall(Guid phonecallId, Guid systemUserId)
		{		
			phonecall phonecall					= new phonecall();
			activityparty from				= new activityparty();
			from.partyid						= new Lookup();
			from.partyid.type					= EntityName.systemuser.ToString();
			from.partyid.Value					= systemUserId;
			phonecall.from						= new activityparty[] {from};
			phonecall.activityid				= new Key();
			phonecall.activityid.Value			= phonecallId; 
			service.Update(phonecall);
		}
 
		/// <summary>
		/// Updates a Letter's From/Sender field to a new UserId
		/// </summary>
		/// <param name="letterId"></param>
		private void UpdateLetter(Guid letterId, Guid systemUserId)
		{
			letter letter						= new letter();
			letter.activityid					= new Key();
			letter.activityid.Value				= letterId;
			activityparty from					= new activityparty();
			from.partyid						= new Lookup();
			from.partyid.type					= EntityName.systemuser.ToString();
			from.partyid.Value					= systemUserId;
			letter.from							= new activityparty[] {from}; 
			service.Update(letter);
		}
 
		/// <summary>
		/// Updates an Email's From/Sender field to a new UserId
		/// </summary>
		/// <param name="emailId"></param>
		private void UpdateEmail(Guid emailId, Guid systemUserId)
		{
			email email							= new email();
			email.activityid					= new Key();
			email.activityid.Value				= emailId;
			activityparty from					= new activityparty();
			from.partyid						= new Lookup();
			from.partyid.type					= EntityName.systemuser.ToString();
			from.partyid.Value					= systemUserId;
			email.from							= new activityparty[] {from}; 
			service.Update(email);
		}
 
		/// <summary>
		/// Updates an Fax's From/Sender field to a new UserId
		/// </summary>
		/// <param name="emailId"></param>
		private void UpdateFax(Guid faxId, Guid systemUserId)
		{
			fax fax								= new fax();
			fax.activityid						= new Key();
			fax.activityid.Value				= faxId;
			activityparty from					= new activityparty();
			from.partyid						= new Lookup();
			from.partyid.type					= EntityName.systemuser.ToString();
			from.partyid.Value					= systemUserId;
			fax.from							= new activityparty[] {from}; 
			service.Update(fax);
		}
 
		/// <summary>
		/// Retrieves all Contacts for an Account
		/// </summary>
		/// <param name="accountId"></param>
		/// <returns></returns>
		private ArrayList RetrieveContactsForAccount(Guid accountId)
		{
			ColumnSet columnSet					= new ColumnSet();
			columnSet.Attributes				= new string[] {"contactid"};
							
			ConditionExpression condition1		= new ConditionExpression();
			condition1.AttributeName			= "parentcustomerid";
			condition1.Operator					= ConditionOperator.Equal;
			condition1.Values					= new string [] {accountId.ToString()};
														
			FilterExpression filter				= new FilterExpression();
			filter.FilterOperator				= LogicalOperator.And;
			filter.Conditions					= new ConditionExpression[] {condition1};
						
			QueryExpression query				= new QueryExpression();
			query.EntityName					= EntityName.contact.ToString();
			query.ColumnSet						= columnSet;
			query.Criteria						= filter;
							
			BusinessEntityCollection contacts	= service.RetrieveMultiple(query);
 
			ArrayList contactList				= new ArrayList();
						
			if (contacts != null)
			{
				if (contacts.BusinessEntities.Length > 0)
				{
					foreach (object businessEntity in contacts.BusinessEntities)
					{
						contact contact	= (contact)businessEntity;
 
						contactList.Add(contact);
					}
				}
			}
 
			return contactList;
		}
 
		/// <summary>
		/// Populates the phonecallList with Phonecalls for a Contact
		/// </summary>
		/// <param name="contactId"></param>
		/// <returns></returns>
		private ArrayList RetrievePhonecallsForContact(Guid contactId)
		{
			ColumnSet columnSet					= new ColumnSet();
			columnSet.Attributes				= new string[] {"from", "activityid"};
							
			ConditionExpression condition1		= new ConditionExpression();
			condition1.AttributeName			= "regardingobjectid";
			condition1.Operator					= ConditionOperator.Equal;
			condition1.Values					= new string [] {contactId.ToString()};
														
			FilterExpression filter				= new FilterExpression();
			filter.FilterOperator				= LogicalOperator.And;
			filter.Conditions					= new ConditionExpression[] {condition1};
						
			QueryExpression query				= new QueryExpression();
			query.EntityName					= EntityName.phonecall.ToString();
			query.ColumnSet						= columnSet;
			query.Criteria						= filter;
							
			BusinessEntityCollection phonecalls	= service.RetrieveMultiple(query);
			
			ArrayList phonecallList				= new ArrayList();	
		
			if (phonecalls != null)
			{
				if (phonecalls.BusinessEntities.Length > 0)
				{
					foreach (object businessEntity in phonecalls.BusinessEntities)
					{
						phonecall phonecall	= (phonecall)businessEntity;
 
						phonecallList.Add(phonecall);
					}
				}
			}
 
			return phonecallList;
		}
 
		/// <summary>
		/// Populates the letterList with Letters for a Contact
		/// </summary>
		/// <param name="contactId"></param>
		/// <returns></returns>
		private ArrayList RetrieveLettersForContact(Guid contactId)
		{
			ColumnSet columnSet					= new ColumnSet();
			columnSet.Attributes				= new string[] {"from", "activityid"};
							
			ConditionExpression condition1		= new ConditionExpression();
			condition1.AttributeName			= "regardingobjectid";
			condition1.Operator					= ConditionOperator.Equal;
			condition1.Values					= new string [] {contactId.ToString()};
														
			FilterExpression filter				= new FilterExpression();
			filter.FilterOperator				= LogicalOperator.And;
			filter.Conditions					= new ConditionExpression[] {condition1};
						
			QueryExpression query				= new QueryExpression();
			query.EntityName					= EntityName.letter.ToString();
			query.ColumnSet						= columnSet;
			query.Criteria						= filter;
							
			BusinessEntityCollection letters	= service.RetrieveMultiple(query);
					
			ArrayList letterList				= new ArrayList();	
 
			if (letters != null)
			{
				if (letters.BusinessEntities.Length > 0)
				{
					foreach (object businessEntity in letters.BusinessEntities)
					{
						letter letter = (letter)businessEntity;
 
						letterList.Add(letter);
					}
				}
			}
 
			return letterList;
		}
 
		/// <summary>
		/// Populates the emailList with Emails for a Contact
		/// </summary>
		/// <param name="contactId"></param>
		/// <returns></returns>
		private ArrayList RetrieveEmailsForContact(Guid contactId)
		{
			ColumnSet columnSet					= new ColumnSet();
			columnSet.Attributes				= new string[] {"from", "activityid"};
							
			ConditionExpression condition1		= new ConditionExpression();
			condition1.AttributeName			= "regardingobjectid";
			condition1.Operator					= ConditionOperator.Equal;
			condition1.Values					= new string [] {contactId.ToString()};
														
			FilterExpression filter				= new FilterExpression();
			filter.FilterOperator				= LogicalOperator.And;
			filter.Conditions					= new ConditionExpression[] {condition1};
						
			QueryExpression query				= new QueryExpression();
			query.EntityName					= EntityName.email.ToString();
			query.ColumnSet						= columnSet;
			query.Criteria						= filter;
							
			BusinessEntityCollection emails		= service.RetrieveMultiple(query);
						
			ArrayList emailList					= new ArrayList();	
 
			if (emails != null)
			{
				if (emails.BusinessEntities.Length > 0)
				{
					foreach (object businessEntity in emails.BusinessEntities)
					{
						email email = (email)businessEntity;
 
						emailList.Add(email);
					}
				}
			}
 
			return emailList;
		}
 
		/// <summary>
		/// Populates the faxList with Faxes for a Contact
		/// </summary>
		/// <param name="contactId"></param>
		/// <returns></returns>
		private ArrayList RetrieveFaxesForContact(Guid contactId)
		{
			ColumnSet columnSet					= new ColumnSet();
			columnSet.Attributes				= new string[] {"from", "activityid"};
							
			ConditionExpression condition1		= new ConditionExpression();
			condition1.AttributeName			= "regardingobjectid";
			condition1.Operator					= ConditionOperator.Equal;
			condition1.Values					= new string [] {contactId.ToString()};
														
			FilterExpression filter				= new FilterExpression();
			filter.FilterOperator				= LogicalOperator.And;
			filter.Conditions					= new ConditionExpression[] {condition1};
						
			QueryExpression query				= new QueryExpression();
			query.EntityName					= EntityName.fax.ToString();
			query.ColumnSet						= columnSet;
			query.Criteria						= filter;
							
			BusinessEntityCollection faxes		= service.RetrieveMultiple(query);
						
			ArrayList faxList					= new ArrayList();	
 
			if (faxes != null)
			{
				if (faxes.BusinessEntities.Length > 0)
				{
					foreach (object businessEntity in faxes.BusinessEntities)
					{
						fax fax = (fax)businessEntity;
 
						faxList.Add(fax);
					}
				}
			}
 
			return faxList;
		}
 
		#endregion
	}
}
[+][-]06.18.2008 at 02:16PM PDT, ID: 21817320

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.18.2008 at 07:34PM PDT, ID: 21818827

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.18.2008 at 07:37PM PDT, ID: 21818837

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 09:49AM PDT, ID: 21824026

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.19.2008 at 01:58PM PDT, ID: 21826399

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Dynamics, C# Programming Language
Tags: Microsoft, Dynamics CRM, 3.0, PostAssign Callout, C#
Sign Up Now!
Solution Provided By: Biffster007
Participating Experts: 1
Solution Grade: B
 
 
[+][-]06.25.2008 at 12:07PM PDT, ID: 21868867

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.12.2008 at 03:14PM PDT, ID: 22217587

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628