Monday 23 July 2012

update the child entity record

Senario

if we need to update a actual date field in Opportunityproduct entity with opportunity entity actual date field data.

here we need to give a field level mapping from opportunity to opportunityproduct.

other wise we need to write a plug in for create a record in opportunity product also.

create a record in opportunity product.


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.Actual.Est.dates
{
    public class Opportunitycreate:IPlugin
    {
        public DateTime actualdate;
        public DateTime estimateddate;

        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 != "new_opportunityitproduct")
                {
                    return;
                }
            }
            else
            {
                return;
            }

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

                Entity entity1 = (Entity)context.InputParameters["Target"];

                if (context.MessageName == "Create")
                {
                    //Money priceamt = (Money)entity1["new_price"];

                    //int timeonitems = Convert.ToInt32(entity1["estimatedvalue"]);
                    EntityReference var1 = (EntityReference)entity1["new_opportunityid"];


                    ColumnSet cols = new ColumnSet(true);

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

                    if (contact1.Attributes.Keys.Contains("actualclosedate") == false)
                    {
                        //totalestrevenue = priceamt;
                        entity1["new_actualclosedate"] = null;

                        //actualdate = new DateTime();
                    }


                    else
                    {
                        // int timeofitems = Convert.ToInt32(contact1["new_price"]);
                        //Money estimatedamt = (Money)contact1["estimatedvalue"]; //

                        actualdate = (DateTime)contact1["actualclosedate"];

                        // totaltime = timeofitems + timeonitems;

                       // totalestrevenue = new Money() { Value = (estimatedamt.Value + priceamt.Value) };//estimatedamt + priceamt;
                    }

                    if (contact1.Attributes.Keys.Contains("estimatedclosedate") == false)
                    {

                        entity1["new_estimatedclosedate"] = null;
                    }
                    else
                    {
                        estimateddate = (DateTime)contact1["estimatedclosedate"];
                    }

                    if (actualdate == null && estimateddate == null)
                    {
                        service.Update(entity1);
                    }

                   // contact1["estimatedvalue"] = totalestrevenue;
                    if (actualdate != null)
                    {
                        if (actualdate.ToShortDateString() != "1/1/0001")
                        {
                            entity1["new_actualclosedate"] = actualdate;
                        }
                    }
                    else
                    {
                        entity1["new_actualclosedate"] = null;
                    }
                    if (estimateddate != null)
                    {
                        if (estimateddate.ToShortDateString() != "1/1/0001")
                        {
                            entity1["new_estimatedclosedate"] = estimateddate;
                        }
                    }

                    else
                    {
                        entity1["new_estimatedclosedate"] = null;
                    }
                  
                    service.Update(entity1);
                }




            }

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

        }

    }
}

Update a record in Opportunity:

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.Actual.Est.dates
{
   public class dateonupdate:IPlugin
    {
        public Nullable<DateTime> actualdate1;
        public Nullable<DateTime> estimateddate1; 

        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)
            {
                if (context.Depth > 1)
                {
                    return;
                }

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

                // Verify that the entity represents a contact.
                if (entity.LogicalName != "opportunity")
                {
                    return;
                }
            }
            else
            {
                return;
            }

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

                Entity entity1 = (Entity)context.InputParameters["Target"];

                

                string fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
  "<entity name='new_opportunityitproduct'>" +
    "<attribute name='new_opportunityitproductid' />" +
    "<attribute name='new_name' />" +
    "<attribute name='new_category' />" +
    "<attribute name='new_subcategory' />" +
    "<attribute name='new_productservice' />" +
    "<attribute name='new_price' />" +
    "<attribute name='new_actualclosedate' />" +
    "<attribute name='new_estimatedclosedate' />" +
    "<attribute name='createdon' />" +
    "<order attribute='new_name' descending='false' />" +
    "<filter type='and'>" +
      "<condition attribute='new_opportunityid' operator='eq' uitype='opportunity' value='" + entity1.Id + "' />" +
    "</filter>" +
  "</entity>" +
"</fetch>";

                RetrieveMultipleRequest req = new RetrieveMultipleRequest();
                FetchExpression fetch = new FetchExpression(fetchquery);
                req.Query = fetch;
                RetrieveMultipleResponse resp = (RetrieveMultipleResponse)service.Execute(req);

                //oipt.Id = leadids;

                EntityCollection col = resp.EntityCollection;
                EntityReference opp = new EntityReference();
                opp.Id = entity.Id;
                opp.LogicalName = entity.LogicalName;

                Entity opp_product = new Entity();
                opp_product.LogicalName = "new_opportunityitproduct";

                if (entity1.Attributes.Keys.Contains("actualclosedate") == false)
                {
                    //actualdate1 = new System.DateTime();
                    //actualdate1 = DateTime.MinValue;
                    actualdate1 = null; 
                   
                }
                else
                {
                    actualdate1 = (DateTime)entity1["actualclosedate"];
                }



                if (entity1.Attributes.Keys.Contains("estimatedclosedate") == false)
                {
                    //estimateddate1 = DateTime.MinValue;

                    estimateddate1 = null;
                }
                else
                {
                    estimateddate1 = (DateTime)entity1["estimatedclosedate"];
                }
               

                foreach (var c in col.Entities)
                {
                    if (actualdate1 != null)
                    {

                        opp_product["new_actualclosedate"] = actualdate1;
                        //if (actualdate1.ToShortDateString() != "1/1/0001")
                        //{
                        //    opp_product["new_actualclosedate"] = actualdate1;
                        //}
                    }
                    if (estimateddate1 != null)
                    {

                        opp_product["new_estimatedclosedate"] = estimateddate1;
                        //if (estimateddate1.ToShortDateString() != "1/1/0001")
                        //{
                        //    opp_product["new_estimatedclosedate"] = estimateddate1;
                        //}
                    }
                    //opp_product["new_price"] = (Money)c["new_price"];//new Money() { Value = 0 };
                    //opp_product["new_opportunityid"] = 
                    opp_product.Id = c.Id;
                    opp_product.LogicalName = c.LogicalName;
                    service.Update(opp_product);
                    
                }           
                    
                
                

            }

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

        }

    }
}


Assign Date Field null:

Nullable<DateTime> actualdate1;
 actualdate1=null;
or  actualdate1.value=null;

No comments:

Post a Comment