Tuesday 10 July 2012

calculate a field based on child entity field values on update

this is for update message:

here we need to take pre and post images



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.Est.Revenue.Calculation
{
   public class Estrevenueonupdate : IPlugin
    {
        public Money totalestimatedamt;

        //public Entity entity1;

        public IOrganizationService service;
        public IPluginExecutionContext context;
        public Money preprice;
        public Money postprice;
        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_opportunityitproduct") && context.PreEntityImages["new_opportunityitproduct"] is Entity)
                {

                    Entity slaitem = (Entity)context.PreEntityImages["new_opportunityitproduct"];
                 preprice = (Money)slaitem.Attributes["new_price"];
                    slaid = (EntityReference)slaitem.Attributes["new_opportunityid"];
                }
                if (context.PostEntityImages.Contains("new_opportunityitproduct") && context.PostEntityImages["new_opportunityitproduct"] is Entity)
                {
                    Entity slaitems = (Entity)context.PostEntityImages["new_opportunityitproduct"];
                    postprice = (Money)slaitems.Attributes["new_price"];

                }


                ColumnSet cols = new ColumnSet(new String[] { "estimatedvalue" });
                var contact1 = service.Retrieve("opportunity", slaid.Id, cols);

                Money estimatedamt = (Money)contact1["estimatedvalue"];

                Money updatedamt = new Money() { Value = (postprice.Value - preprice.Value) };

                totalestimatedamt = new Money() { Value = (estimatedamt.Value + updatedamt.Value ) };
                //int timeofitems = Convert.ToInt32(contact1["slk_maxtime"]);
                //totaltime = timeofitems + (time2 - time1);
                contact1["estimatedvalue"] = totalestimatedamt;//totaltime;
                service.Update(contact1);

            }

            else return;
        }
          


    }
}

No comments:

Post a Comment