web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Invalid Argument error plugin when changing owner of a lead entity to a team

(0) ShareShare
ReportReport
Posted on by 67

Hi there,

I have a small and easy plugin, which should change the owner of a lead when creating in the PreOperation stage.

In runtime, it always shows Invalid Argument record and unfortunately cannot find the error through the profiler.

My code is something like this:

var context = new CrmServiceContext(OrganizationService);
var lead = Entity.ToEntity<Lead>();
if (PluginContext.MessageName.ToLower() == "create")
{
if (PluginContext.Depth <= 1)
      {
        var teamId = new Guid("56663CBD-3295-EA11-A811-000D3A23C48F"); // AL team in Lead assignment environment
        lead.OwnerId = new EntityReference("team", teamId);
      }
}

Entity is the target entity in the PluginContext. 

Please guide me on what is the problem.

I have the same question (0)
  • Suggested answer
    RajarajanS Profile Picture
    on at

    Hello Partner, 

    Is the error getting thrown only when you are assigning it to a team or is the error thrown even when you assign it to a user in the system?

    You can look through the sample code for a assign of record mentioned here https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/org-service/sample-assign-record-new-owner and check if the sample mentioned here will help. 

    Hope this helps. 

    Mark this as Suggested if it helps. 

    Thanks, 

    Raj

  • Mahdyar Profile Picture
    67 on at

    Hi RajarajanS,

    Thank you for your reply.

    I already tested it with a user and I got the same error. I need to explain that I do not have any problem when I assign it to the team in the PostOperation of Lead creation.

    My problem is in the PreOperation, when I just change the ownerid field to an EntityReference team. I just copy my both codes to explain it better:

    1) The following code is the main code that I need. The step would be "create lead PreOperation". It doesn't work.

           try

             {

                   var context = new CrmServiceContext(OrganizationService);

                   var lead = Entity.ToEntity<Lead>();

                   if (PluginContext.MessageName.ToLower() == "create")

                   {

                       if (PluginContext.Depth <= 1)

                       {

                           if (lead.ptvits_Country != null)

                           {

                               var countryEntity = OrganizationService.Retrieve(lead.ptvits_Country.LogicalName,

                                   lead.ptvits_Country.Id, new ColumnSet(true));

                               var country = countryEntity.ToEntity<ptvits_country>();

                               if (country != null)

                                   lead.Address1_Country = country.ptvits_name;

                           }

                           var teamId = new Guid("09AC211F-AD95-EA11-A811-000D3A23C48F"); // AL team in Lead assignment environment

                           lead.OwnerId = new EntityReference("team", teamId);

                       }

                   }

              }

    2) The following code is my second solution that is not ideal for me, because it will insert my data in two steps. The step here would be "create lead PostOperation. It actually works, and you see I am using the same team id.

             try

               {

                   var context = new CrmServiceContext(OrganizationService);

                   var lead = Entity.ToEntity<Lead>();

                   if (PluginContext.MessageName.ToLower() == "create")

                   {

                       if (PluginContext.Depth <= 1)

                       {

                           if (lead.ptvits_Country != null)

                           {

                               var countryEntity = OrganizationService.Retrieve(lead.ptvits_Country.LogicalName,

                                   lead.ptvits_Country.Id, new ColumnSet(true));

                               var country = countryEntity.ToEntity<ptvits_country>();

                               if (country != null)

                               {

                                   lead.Address1_Country = country.ptvits_name;

                                   context.Attach(lead);

                                   context.UpdateObject(lead);

                                   context.SaveChanges();

                               }

                           }

                           var teamId = new Guid("09AC211F-AD95-EA11-A811-000D3A23C48F"); // AL team in Lead assignment environment

                           var assignRequest = new AssignRequest()

                           {

                               Assignee = new EntityReference

                               {

                                   LogicalName = Team.EntityLogicalName,

                                   Id = teamId

                               },

                               Target = new EntityReference(Lead.EntityLogicalName, lead.Id)

                           };

                           OrganizationService.Execute(assignRequest);

                       }

                   }

               }

    Now, I want to know what's wrong with the no.1 code? I need it to work.

  • Verified answer
    RajarajanS Profile Picture
    on at

    Hello Partner, 

    Thank you for your response. I also did a repro of the same thing where I created both the plug-ins like mentioned above, i used update request for both. On the Post Operation its a success and on the Pre Operation it fails. The failure message that I get during the lead creation is that "Changing security attributes is not allowed in stage 20 plugins".  This error message is quite self explanatory. By design Dynamics does not allow the update of certain fields and that is why the pre operation is failing. I guess we have to continue using the PostOperation plug-in and instead of using the assign message, you can use the update message. 

    Hope this helps. 

    Thanks and Regards, 

    Raj

  • Mahdyar Profile Picture
    67 on at

    Hi RajarajanS,

    Thank you for your reply. I already marked it as the answer.

    But, my question is that why I cannot get the error message that you got? For me it just says Invalid Argument. How did you reach to that error message?

    Best Regards,

    Mahdyar

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 31 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans