Thursday, 26 April 2012

updating numbers in dynamic crm 2011 using plugin

need to take the early bond and add it on existing items
now we can add the references.

if you create a new record then the filed value need to take the amount of field value.

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 TimecaluculationCreate : IPlugin
        public int totaltime;

        public Entity entity1;

        public IOrganizationService service;
        public IPluginExecutionContext context;

        public void Execute(IServiceProvider serviceProvider)

            IPluginExecutionContext context = (IPluginExecutionContext)

            Entity entity;

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

                // Obtain the target business entity from the input parameters.
                entity = (Entity)context.InputParameters["Target"];

                // Verify that the entity represents a contact.
                if (entity.LogicalName != "new_item")

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

                Entity entity1 = (Entity)context.InputParameters["Target"];
                if (context.MessageName == "Create")
                    int timeonitems = Convert.ToInt32(entity1["new_time"]);
                    EntityReference var1 = (EntityReference)entity1["new_lid"];

                    ColumnSet cols = new ColumnSet(
                                         new String[] { "new_maxtime", "new_name" });

                    var contact1 = service.Retrieve("new_la", var1.Id, cols);

                    if (contact1.Attributes.Keys.Contains("new_maxtime") == false)
                        totaltime = timeonitems;

                        int timeofitems = Convert.ToInt32(contact1["new_maxtime"]);

                        totaltime = timeofitems + timeonitems;

                    contact1["new_maxtime"] = totaltime;

            catch (FaultException<OrganizationServiceFault> ex)
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);



it will update the new_maxtime filed with count.

No comments:

Post a Comment