Tuesday, 5 December 2017

CONTACT CASE COUNT PLUGIN IN DYNAMICS 365

Contact Case Count  plugin dynamics 365


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace AdvancedPlugins
{
    public class ContactcaseCount : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)
         serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // The InputParameters collection contains all the data passed in the message request.

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity contact = (Entity)context.InputParameters["Target"];

                // getting Contact fullname and its emailaddress

                string name = contact["fullname"].ToString();
                string email = contact["emailaddress1"].ToString();

                // we have to get the created contact guid in order to mpa to newly creaing case

                Guid contactid = new Guid(context.OutputParameters["id"].ToString());

                // Create a new case automatically
                Entity cse = new Entity("incident");
                cse["title"] = "User whose name is " + name + " has created new case. Please contact him on hs email: " + email;

                // lokkup --> refer to anoter table - entityname nad its record id

                cse["customerid"] = new EntityReference(contact.LogicalName, contactid);

                service.Create(cse);

                // select case of a contact where conact is contactid
                QueryExpression qr = new QueryExpression();
                qr.EntityName = "incident";
                qr.ColumnSet = new ColumnSet("customerid");

                // where conact is contactid
                ConditionExpression ce = new ConditionExpression();
                ce.AttributeName = "customerid";
                ce.Operator = ConditionOperator.Equal;
                ce.Values.Add(contactid);

                // add cond to query
                qr.Criteria.AddCondition(ce);

                // Exceute query

                EntityCollection caseCollection = service.RetrieveMultiple(qr);


                // update case count on contact - 
                Entity newlyCreatedContact =  service.Retrieve(contact.LogicalName, contactid, new ColumnSet("spousesname"));

                newlyCreatedContact["spousesname"] = caseCollection.Entities.Count.ToString();

                service.Update(newlyCreatedContact);



            }
        }
    }
}



Go ahead and check inside crm about the case count on the contact.

I hope this helps..

No comments:

Post a Comment

Note: only a member of this blog may post a comment.