Thursday 26 April 2012

update automatic numbers when the record is updated in dyanmic crm 2011 with plugin

this is the plugin for the record updating.

for this take the preimage and post image.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xrm;
using System.Diagnostics;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Windows.Browser;
using System.Net;
using System.IO;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
namespace super.Plugin.TimeCaluculation
{
   public class TimeCaluculationUpdate:IPlugin
    {
        public int totaltime;

        //public Entity entity1;

        public IOrganizationService service;
        public IPluginExecutionContext context;
        public int time1;
        public int time2;
        public EntityReference slaid;

        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);

            //Entity entity;

            //EntityReference

            // Check if the input parameters property bag contains a target
            // of the create operation and that target is of type Entity.
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {

                if (context.PreEntityImages.Contains("new_item") && context.PreEntityImages["new_item "] is Entity)
                {

                    Entity slaitem = (Entity)context.PreEntityImages[" new_item "];
                    time1 = (int)slaitem.Attributes["new_time"];
                    slaid = (EntityReference)slaitem.Attributes["new_aid"];
                }
                if (context.PostEntityImages.Contains(" new_item ") && context.PostEntityImages[" new_item "] is Entity)
                {
                    Entity slaitems = (Entity)context.PostEntityImages[" new_item "];
                    time2 = (int)slaitems.Attributes["new_time"];

                }


                ColumnSet cols = new ColumnSet(new String[] { "new_maxtime", "new_name" });
                var contact1 = service.Retrieve("new_sss", aid.Id, cols);

                int timeofitems = Convert.ToInt32(contact1["new_maxtime"]);
                totaltime = timeofitems + (time2 - time1);
                contact1["new_maxtime"] = totaltime;
                service.Update(contact1);

            }

            else return;
        }
           

    }
}

No comments:

Post a Comment