Friday 29 June 2012

plugin for calculate the time difference in dynamic crm 2011

Senario: need to calculate the time difference in varios options.

using early bound.

Ex:


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 Kryptos.Plugin.InvoiceonBillingtype
{
    public class Dayscalculate:IPlugin
    {

        public IOrganizationService service;
        public IPluginExecutionContext context;

        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(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 != "salesorder")
                {
                    return;
                }
            }

            else
            {
                return;
            }


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

                int totalmonths=0;
                Entity entity1 = (Entity)context.InputParameters["Target"];
                // Guid id = entity1.Id;

                OptionSetValue type =(OptionSetValue) entity1["new_billingtype"];

                DateTime start = Convert.ToDateTime(entity1["new_startdate"]);
                DateTime end = Convert.ToDateTime(entity1["new_enddate"]);
                TimeSpan span = end.Subtract(start);
                if (type.Value ==100000000) //Monthly
                {
                   totalmonths = span.Days / 30;
                }
                if (type.Value == 100000003) //Bi-Monthly
                {
                    totalmonths = span.Days / 60;
                }
                if (type.Value == 100000001) //Quarter
                {
                    totalmonths = span.Days / 90;

                }
                if (type.Value == 100000004) //Half Yearly
                {
                    totalmonths = span.Days / 180;

                }
                if (type.Value == 100000002) //Yearly
                {
                    totalmonths = span.Days / 365 ;

                }
                //TimeSpan span = end.Subtract(start);
                entity1["new_days"] = totalmonths;//span.Days;
                entity1["new_nextinvoice"] = start;

                service.Update(entity1);
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.  "+ ex.Message.ToString());
            }

        }

    }
}

No comments:

Post a Comment