Update Entity field not working RRS feed

  • Question

  • Hey Guys,
    I have been wondering about this issue for a week so far, I wouldn't have reached out for your help if it was not really needed.
    Last week I started developing CRM Dynamics 365 online. I was asked to create an auto-generated field in Work Order entity that sets its value based on its service account. I have decided to do this through plugin development and I was successfully registered the plugin in the Pre-Operation of Create Step and I was able to debug my code and do the business logic correctly. What I have found weird while debugging my code was
    the new created field was not available in the entity.Attributes Call.
    I neglected that as I thought since it doesn't contain any data yet maybe it isnot available in the attributes, so I used entity.Attributes.add . But even though the value was set in the WO create Form after saving. I have been searching for what could be causing such an issue for about a week and your help will be much appreciated. Thank You.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Discovery;
    namespace WorkOrder
        public class WorkOrder : IPlugin
            public void Execute(IServiceProvider serviceProvider)
                // Extract the tracing service for use in debugging sandboxed plug-ins.  
                // If you are not registering the plug-in in the sandbox, then you do  
                // not have to add any tracing service related code.  
                ITracingService tracingService =
                // Obtain the execution context from the service provider.  
                IPluginExecutionContext context = (IPluginExecutionContext)
                // The InputParameters collection contains all the data passed in the message request.  
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                    // Obtain the target entity from the input parameters.  
                    Entity entity = (Entity)context.InputParameters["Target"];
                    // Verify that the target entity represents an entity type you are expecting.   
                    // For example, an account. If not, the plug-in was not registered correctly.  
                    if (entity.LogicalName != "msdyn_workorder")
                    // Obtain the organization service reference which you will need for  
                    // web service calls.  
                    IOrganizationServiceFactory serviceFactory =
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                    var account = (Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["msdyn_serviceaccount"]);
                    var actualAccount = service.Retrieve(account.LogicalName, account.Id, new ColumnSet(true));
                    var nameField = actualAccount.Id;
                        QueryExpression query = new QueryExpression("msdyn_workorder");
                        query.ColumnSet.AddColumns("msdyn_serviceaccount", "new_workordernumber");
                        query.Criteria = new FilterExpression();
                        query.Criteria.AddCondition("msdyn_serviceaccount", ConditionOperator.Equal, nameField);
                        Entity wo =service.RetrieveMultiple(query).Entities[0];
                        var x = entity.GetAttributeValue<string>("new_workordernumber");
                        var y = wo.GetAttributeValue<string>("new_workordernumber");
                        if (y == null)
                            entity.Attributes.Add("new_workordernumber", "1");
                            entity.Attributes.Add("new_workordernumber", "2");
                    catch (FaultException<OrganizationServiceFault> ex)
                        throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
                    catch (Exception ex)
                        tracingService.Trace("MyPlugin: {0}", ex.ToString());

    Thursday, February 15, 2018 12:10 PM