0
votes

I've written a plugin with the following configuration:

enter image description here

I'm simply trying to set one datetime field to equal another datetime field:

 IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                try
                {
                    if (entity.LogicalName == "list" && entity.Attributes["gbs_lastusedonoriginal"] != null)
                    {
                        entity.Attributes["lastusedon"] = entity.Attributes["gbs_lastusedonoriginal"];
                        service.Update(entity);
                    }
                }
                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occured in the plug-in.", ex);
                }

            }

The exception I get is:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occured in the plug-in.Detail:
-2147220891 OperationStatus 0 SubErrorCode -2146233088 An error occured in the plug-in.
2015-01-15T05:34:00.1772929Z

[PreValidationMarketingList.Plugins: PreValidationMarketingList.Plugins.PreValidateMarketingListCreate] [5454a088-749c-e411-b3df-6c3be5a83130: PreValidateMarketingListCreate]

Entered PreValidationMarketingList.Plugins.PreValidateMarketingListCreate.Execute(), Correlation Id: 6d3ed105-f9c4-4006-9c80-08abd97c0140, Initiating User: 5e1b0493-d07b-e411-b592-f0921c199288 PreValidationMarketingList.Plugins.PreValidateMarketingListCreate is firing for Entity: list, Message: Create, Correlation Id: 6d3ed105-f9c4-4006-9c80-08abd97c0140, Initiating User: 5e1b0493-d07b-e411-b592-f0921c199288 Exiting PreValidationMarketingList.Plugins.PreValidateMarketingListCreate.Execute(), Correlation Id: 6d3ed105-f9c4-4006-9c80-08abd97c0140, Initiating User: 5e1b0493-d07b-e411-b592-f0921c199288

What am I doing wrong?In crm 2013 how do I set one field to equal another field if both of them are datetimes?

2

2 Answers

6
votes

You shouldn't be calling Update in this plugin because you haven't created and saved the record you are trying to update.

First, move this to pre-operation, not pre-validation. It's a nit but pre-op is really the appropriate place since setting lastusedon is not required for validation on Create of list.

I've reworked your code to do some additional checks:

        IPluginExecutionContext context = localContext.PluginExecutionContext;
        IOrganizationService service = localContext.OrganizationService;

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parmameters.
            Entity entity = (Entity)context.InputParameters["Target"];
            try
            {
                if (entity.LogicalName == "list" && entity.Attributes.Contains("gbs_lastusedonoriginal") && entity["gbs_lastusedonoriginal"] != null)
                {
                    if (entity.Attributes.Contains("lastusedon") )
                        entity.Attributes["lastusedon"] = entity.Attributes["gbs_lastusedonoriginal"];
                    else entity.Attributes.Add("lastusedon", entity.Attributes["gbs_lastusedonoriginal"];                        
                }
            }
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occured in the plug-in.", ex);
            }

        }
2
votes

Check this link on MSDN. In the Pre-Event, the record is not yet saved in the SQL database. You can modify the Entity object from the InputParameters. After the pre-event plugin the record will be created with your modified attributes.

The main difference between Pre-validation and Pre-operation is that the Pre-operation stage is executed within the database transaction while Pre-validation not. See MSDN for more information.