Quantcast
Channel: CRM Development Forum
Viewing all articles
Browse latest Browse all 888

Triggering plugin with workflow

$
0
0

Hi,

I've written a plugin on the update of the service activity. There is a field named trigger on service activity. If someone changes this trigger manually on the form, the plugin works good and do everything I want. But, in two scenarios I've got error:

1- When I change the trigger field through a workflow.

2- When other fields than the trigger field change.

In these situations I've got the "The given key was not present in the dictionary".

My code is as bellow:

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

public class Plugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)
        serviceProvider.GetService(typeof(IPluginExecutionContext));
        Entity entity;
        if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
        {
            entity = (Entity)context.InputParameters["Target"];
            if (entity.LogicalName != "serviceappointment") { return; }
        }
        else
        {
            return;
        }
        try
        {
            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(
            typeof(IOrganizationServiceFactory));
            IOrganizationService service =
            serviceFactory.CreateOrganizationService(context.UserId);
            var Trigger = ((OptionSetValue)entity.Attributes["brd_trigger"]).Value.ToString();
            if (Trigger == "172100001")
            {
                var id = (Guid)entity.Id;
                var Party = (EntityCollection)entity["customers"];
                Guid PartyID = (Party.Entities[0]).Id;
                AddWRItoServiceActivity(service, id, PartyID);
                UpdateServiceActivity(service, id);
            }
        }
        catch (FaultException<OrganizationServiceFault> ex)
        {
            throw new InvalidPluginExecutionException(
            "An error occurred in the plug-in.", ex);
        }
    }
    
    private static void AddWRItoServiceActivity(IOrganizationService service, Guid id, Guid PartyID)
    {
        using (var crm = new XrmServiceContext(service))
        {
            var serviceactivity = crm.ServiceAppointmentSet.Where(c => c.Id == id).First();
            var serviceitem = crm.brd_serviceitemSet.Where( c => c.brd_RegardingServiceId.Id == serviceactivity.ServiceId.Id);
            var CustomerID = crm.ActivityPartySet.Where(c => c.ActivityPartyId == PartyID).First().PartyId;
            foreach (var S in serviceitem)
            {
                var workReportItem = new brd_workreportitem
                   {
                       brd_name = S.brd_name,
                       brd_serviceappointment_brd_workreportitem = serviceactivity,
                       brd_brd_serviceitem_brd_workreportitem_ServiceItem = S,
                       brd_brd_servicereportitem_brd_workreportitem_ServiceReportItem = S.brd_brd_servicereportitem_brd_serviceitem_ServiceReportItem,
                    };
                OptionSetValue myOptionSet = new OptionSetValue();
                myOptionSet.Value = S.brd_brd_servicereportitem_brd_serviceitem_ServiceReportItem.brd_Type.Value;
                workReportItem.Attributes["brd_type"] = myOptionSet;
                workReportItem.Attributes["brd_customer"] = new EntityReference("account", CustomerID.Id);
                crm.AddObject(workReportItem);
                crm.SaveChanges();
            }
        }
    }
    private static void UpdateServiceActivity(IOrganizationService service, Guid id)
    {
        using (var crm = new XrmServiceContext(service))
        {
            var SerAct = crm.ServiceAppointmentSet.Where(c => c.Id == id).First();
            OptionSetValue TriggerOpt = new OptionSetValue();
            TriggerOpt.Value = 172100002;
            SerAct.Attributes["brd_trigger"] = TriggerOpt;
            crm.UpdateObject(SerAct);
            crm.SaveChanges();
        }
    }
}


Viewing all articles
Browse latest Browse all 888

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>