Search code examples
pluginsdynamics-crm-4accountowner

Trying to change owner of account on CRM 4.0 using a plugin


I am creating a plugin for Microsoft Dynamics CRM 4 that will change the owner of the account entity according to the value of another lookup field. Now I have managed to get the GUID of the user that will be acting as the 'Owner' of the account. So far so good. The problem arises when I try to change the owner. I am trying to use AssignRequest but it is not working. When I try to execute the request I get a SoapException on the C# Debugger, and the webservice outputs a dialog stating: "The requested record was not found or you do not have sufficient permissions to view it"

Below is the code I am using:

                    TargetOwnedAccount target = new TargetOwnedAccount();

                    SecurityPrincipal assignee = new SecurityPrincipal();
                    assignee.Type = SecurityPrincipalType.User;
                    assignee.PrincipalId = context.InitiatingUserId;

                    target.EntityId = ownerGuid; //this is the GUID I am retrieving from the other lookup field

                    AssignRequest assign = new AssignRequest();
                    assign.Assignee = assignee;
                    assign.Target = target;

                    AssignResponse res = (AssignResponse)crmService.Execute(assign); //this is where i get the exception

I hope I haven't missed anything. Any help would be much appreciated :) Thanks


Solution

  • Ok i managed to solve this finally. It had been staring directly at my face :P I was entering the wrong ID's at the wrong place. I needed to set the 'assignee.PrincipalId' to the 'ownerGuid' and then set the 'target.EntityId' to the current account id. The new code is as follows:

                    TargetOwnedAccount target = new TargetOwnedAccount();
    
                    SecurityPrincipal assignee = new SecurityPrincipal();
                    assignee.Type = SecurityPrincipalType.User;
                    assignee.PrincipalId = ownerGuid; //this is the GUID I am retrieving from the other lookup field
    
                    target.EntityId = ((Key)entity.Properties["accountid"]).Value;
    
                    AssignRequest assign = new AssignRequest();
                    assign.Assignee = assignee;
                    assign.Target = target;
    
                    AssignResponse res = (AssignResponse)crmService.Execute(assign);
    

    Cant believe i spent 8 hours yesterday looking at it and then today I realised immediately :P