Search code examples
c#dynamics-crmmicrosoft-dynamicsdynamics-365

Update task record after "mark as complete" on update message


I need to update some fields and do some login on task entity. Actually based on my requirement, when the task record status change to complete(user click on Mark as Complete.), my logic must start. For this reason and because of no event like complete in task messages, I check status of the record in update message. I would like to this via CRM Plugin. To achieve this I use PostEntityImages of task record. I check the status of PostEntityImages in the code and then do my logic. This is what happening :

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            currentTask = (Entity)context.InputParameters["Target"];
            if (currentTask.LogicalName == "task")
            {
                afterUpdateTask = (Entity)context.PostEntityImages["updatedTask"];
                beforeUpdateTask = (Entity)context.PreEntityImages["updatedTask"];
                if (afterUpdateTask.Contains("statuscode"))
                {
                    int statuscode = ((OptionSetValue)afterUpdateTask["statuscode"]).Value;

                    if (statuscode == 5)
                    {

As you see I also get the PreEntityImages as well. After last if, the logic starts and some fields update; the fields of PostEntityImages(afterUpdateTask ). But CRM returns :

The object cannot be updated because it is read-only. If you contact support, please provide the technical details.

It seems CRM does not talk much off ! I also tried this with PreEntityImages(beforeUpdateTask), but it does not work for me. Because it updates the fields in task entity, but does not change the status of record to complete, however in my scenario I need the modifiedon and statuscode fields of PostEntityImages. Also tried with currentTask that is (Entity)context.InputParameters["Target"], but it just change the stauts of record to complete without running my logic.

What should I do to update a task record after it change to complete? Is it possible to do this via Plugin?


Solution

  • Activity entities in Dynamics 365 after being completed they become read only.

    I would register the Plug-in in PreOperation on Update message, having StatusCode as filtering attribute and perform the logic there:

    • Compare PreImage with Status Code in Plugin Execution Context and see if the status was changed to completed
    • Loggin
    • Changes to the Task Entity

    Else, CRM will not allow the modification of a completed activity.