Tuesday 10 July 2012

convert lead product to opportunity product

Senario:

we need to convert the lead product as opportunity product:

Getting Fetch XML code:  goto Advanced find:select the query. and get the fetch xml.


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.ITProduct.leadtooppotunity
{
    public class leadtoopportunity : 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 != "opportunity")
                {
                    return;
                }
            }

            else
            {
                return;
            }

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

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



                ColumnSet cols = new ColumnSet(true);

                var opportunityset = service.Retrieve("opportunity", entity.Id, cols);


                if (opportunityset.Attributes.Keys.Contains("originatingleadid") == false)
                {
                    return;

                }

                else
                {

                    EntityReference var1 = (EntityReference)opportunityset["originatingleadid"];                

                    string fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
  "<entity name='new_leaditproduct'>" +
    "<attribute name='new_leaditproductid' />" +
    "<attribute name='new_name' />" +
    "<attribute name='new_category' />" +
    "<attribute name='new_subcategory' />" +
    "<attribute name='new_productservice' />" +
    "<attribute name='createdon' />" +
    "<order attribute='new_name' descending='false' />" +
    "<filter type='and'>" +
      "<condition attribute='new_productid' operator='eq' uitype='lead' value='" + var1.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;
                    Entity opp_product = new Entity();
                    opp_product.LogicalName = "new_opportunityitproduct";
                    EntityReference opp = new EntityReference();
                    opp.Id = entity.Id;
                    opp.LogicalName = entity.LogicalName;
                    foreach (var c in col.Entities)
                    {
                        opp_product["new_category"] = (EntityReference)c["new_category"];
                        opp_product["new_subcategory"] = (EntityReference)c["new_subcategory"];
                        opp_product["new_productservice"] = (EntityReference)c["new_productservice"];
                        opp_product["new_price"] = new Money() { Value = 0 };
                        opp_product["new_opportunityid"] = (EntityReference)opp;
                        service.Create(opp_product);
                    }
                  
                }
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.  " + ex.Message.ToString());

            }

        }




    }
}

No comments:

Post a Comment