Solved

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

Posted on 2008-06-18
7
1,528 Views
Last Modified: 2009-07-30
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,

Matt
/*
 * 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
	}
}

Open in new window

0
Comment
Question by:mattskelton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:Biffster007
ID: 21817320
Hi,

Is this working for all the other activity types??

MH
0
 
LVL 1

Author Comment

by:mattskelton
ID: 21818827
Hi,

I have only tried Facilities that have phonecall activities. I will try others & post back.

Matt
0
 
LVL 9

Expert Comment

by:Biffster007
ID: 21818837
Cheers,
MH
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:mattskelton
ID: 21824026
@ Biffster007,

Yes it is only Phonecalls. I couldn't test faxes but emails & letters work OK.

Thanks,

Matt
0
 
LVL 9

Accepted Solution

by:
Biffster007 earned 250 total points
ID: 21826399
Mroning,

Try retrieving all columns when you retrieve the phone call and try updating something else other than the 'from' field.  Something simply typed like a string field.

The phonecall is definitely active??

Also, what happens when you try and make the same change through the interface...success??

Cheers,
MH
0
 
LVL 1

Author Comment

by:mattskelton
ID: 21868867
Hi Biffster007,

Sorry for the long delay between posts. I am in the process of setting up a CRM test server so this callout has been pushed back until I get it up & running; I am thinking the end of next week. Thanks for your help so far.

Matt
0
 
LVL 9

Expert Comment

by:Biffster007
ID: 22217587
No worries...

Let me know how you get on.

MH
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replacement for Great Plain Dynamics 22 72
C# LINQ ForEach() question 6 89
Problem to Office 1 45
insert an Jpeg/bmp image onto the visio drawing using VBA/C# 5 29
On Sep 22nd 2014 Microsoft released Update Rollup 1 for Microsoft Dynamics CRM 2013 Service Pack 1 and back in July Update Rollup 3 was released.  So we now have:   Update Rollup 1Update Rollup 2Update Rollup 3Service Pack 1Update Rollup 1 for S…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question