5

I need to change the owner of an appointment record when creating a new appointment. I'm using a plugin for the create message and i've found this code to assign a new owner to the appointment:

entity = context.PostEntityImages["PostImage"];

......

AssignRequest request = new AssignRequest();

//request.RequestName
request.Assignee = new EntityReference("systemuser", owners.ToList()[0].Id);
request.Target = new EntityReference(Appointment.EntityLogicalName, entity.Id);
service.Execute(request);

But when I test this i get the following error: Invalid Argument: There should be only one owner party for an activity

I guess i have to remove the current owner first and then assign the new owner. But how can i do this?

Thanks for any help!

Greg Owens
  • 3,878
  • 1
  • 18
  • 42
ThdK
  • 9,916
  • 23
  • 74
  • 101

3 Answers3

5

I was doing something similar with tasks (reassigning them in a plugin). As a "Update" plugin it didn't have any problems, but as "Create" it would fail with the message "There should be only one owner party for an activity"

To fix this change the "Create" plugin to simply set the ownerid (instead of executing the AssignRequest).

targetEntity.Attributes["ownerid"] = new EntityReference(SystemUser.EntityLogicalName, assignTo.Id);

This code goes in the Pre-operation stage.

Eric Labashosky
  • 29,484
  • 14
  • 39
  • 32
  • I guess i'll still need this in the future. Thanks for replying, even if the question was asked a while ago. Extra information is always welcome :) – ThdK Nov 16 '11 at 08:36
  • Does this work with a custom workflow activity? I'm having a similar problem but it's in a WF http://stackoverflow.com/questions/8529535/setting-ownerid-of-a-custom-activity – Abhijeet Patel Dec 16 '11 at 04:07
1

It looks like it is possible that some of the data may be corrupted. In this thread people are pushing the person to use SQL to directly delete some of the owners off of the activity - http://social.microsoft.com/Forums/en/crmdeployment/thread/d82cedee-e24e-4abc-9ec6-41306b89ed3b

This is only a possibility if you are using the on-premise model of Dynamics CRM 2011.

cchamberlain
  • 17,444
  • 7
  • 59
  • 72
  • Thank your for your response, but we're running IFD. Is there a way to do the trick without modify directly the database? – ThdK Aug 25 '11 at 12:28
  • I would recommend opening a support ticket with Microsoft. If you are setup as IFD, it depends on who owns the server. If you have a partner hosting your IFD solution in a shared environment, they will likely not want to run unsupported SQL directly against the database. If you are hosting your IFD solution, it will be up to your IT department whether they will go for it. I'd definitely get Microsoft involved either way. – cchamberlain Aug 27 '11 at 19:13
  • I'll inform our team on Monday. Thanks :) – ThdK Aug 28 '11 at 08:28
0
Guid id= new Guid("{33011A68-D311-E211-A429-005056820002}");    
 switch (context.MessageName)
                {
                    case "Update":
                        {
                            try
                            {
                                if (ent.Contains("fieldname") == true)
                                {

                                    AssignRequest assign = new AssignRequest
                                    {
                                        Assignee = new EntityReference("systemuser", id),
                                        Target = new EntityReference(ent.LogicalName, ent.Id)
                                    };
                                    _service.Execute(assign);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new InvalidPluginExecutionException("Error" + Environment.NewLine + "Details: " + ex.Message);
                            }
                        }
                        break;
                }