简体   繁体   中英

c# - dynamics crm online plugin - use field value to populate attribute of related entity

I'm new to c# and plugins for dynamics. In order to learn and test, I've successfully created a couple very simple plugins that worked. Now, I'm trying to get more into what I'll actually need to do with plugins -- I'm trying to get the value of a field on a custom entity, and use that value to update an attribute on a related custom entity.

My plugin is registered on the Update message of a custom entity (called new_registration). It runs post-operation, asynchronously. The field that updates and triggers the plugin (an option set "Status" field) is not used in the code anywhere.

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;
using Microsoft.Xrm.Sdk;

namespace PlugInTests
{
    public class AdjustTimeSlots: IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            if (context.InputParameters != null)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                Guid id = entity.Id;
                tracingService.Trace("got input parameters");

                //get time slot
                string slot = (string)entity.Attributes["new_yourtimeslot"];
                EntityReference eventclass = (EntityReference)entity.Attributes["new_eventregistrationrelationshipid"];
                tracingService.Trace("got time slot");

                //set updated entity (event/class)
                Entity parentevent = new Entity("new_eventclass");
                parentevent.Id = eventclass.Id;
                parentevent.Attributes["new_timeslotsfordelete"] = slot;


                // Obtain the organization service reference.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                //update event record
                tracingService.Trace("Update time slot plugin");
                service.Update(parentevent);
            }
        }
    }
}

Through testing, I've narrowed down that this is failing (at least initially) on this line:

string slot = (string)entity.Attributes["new_yourtimeslot"];

The error I'm getting in the plugin trace logs is:

The given key was not present in the dictionary.

I've checked and double-checked, and I know that I'm getting the field name correct. Am I doing something wrong in how I'm getting the value from the input parameters? Or am I messing something up I don't even realize I might be messing up? Any help is appreciated, thanks.

Always try to get the attribute value in a safe way (either check for the attribute in the attribute collection or use the SDK method like below). If an attribute value is null, the attribute is not returned as part of the attribute collection.

var slot = entity.GetAttributeValue<string>("new_yourtimeslot");

Following code snippet doesn't look correct

EntityReference eventclass = (EntityReference)entity.Attributes["new_eventregistrationrelationshipid"];

Attribute names and relationships names are seldom the same. Relationship names often include target entity and related entity and most often the attributes that end up being the lookups are named differently new_eventregistrationid maybe? Double check the name by looking at Customization - Field Properties .

Also, safely get the related attribute:

var eventclass = entity.GetAttributeValue<EntityReference>("new_eventregistrationid");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM