Senario:
we need to retrieve the Opportunity Products relating to opportunity. and update the fields in opportunity product entity.
first retrieve the opportunity products relating to the particular opportunity:
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_actuvalclosedate' />" +
"<attribute name='new_estclosedate' />" +
"<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>";
here : condition attribute is the 1:N relation between opportunity and opportunity product.
reading the Fetch xml data:
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;
Ex Plugin:
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 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)
{
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_actuvalclosedate' />" +
"<attribute name='new_estclosedate' />" +
"<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)
{
}
else
{
actualdate = (DateTime)entity1["actualclosedate"];
}
if (entity1.Attributes.Keys.Contains("estimatedclosedate") == false)
{
}
else
{
estimateddate = (DateTime)entity1["estimatedclosedate"];
}
foreach (var c in col.Entities)
{
opp_product["new_actuvalclosedate"] = actualdate;
opp_product["new_estclosedate"] = estimateddate;
//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);
}
}
}
}
we need to retrieve the Opportunity Products relating to opportunity. and update the fields in opportunity product entity.
first retrieve the opportunity products relating to the particular opportunity:
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_actuvalclosedate' />" +
"<attribute name='new_estclosedate' />" +
"<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>";
here : condition attribute is the 1:N relation between opportunity and opportunity product.
reading the Fetch xml data:
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;
Ex Plugin:
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 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)
{
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_actuvalclosedate' />" +
"<attribute name='new_estclosedate' />" +
"<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)
{
}
else
{
actualdate = (DateTime)entity1["actualclosedate"];
}
if (entity1.Attributes.Keys.Contains("estimatedclosedate") == false)
{
}
else
{
estimateddate = (DateTime)entity1["estimatedclosedate"];
}
foreach (var c in col.Entities)
{
opp_product["new_actuvalclosedate"] = actualdate;
opp_product["new_estclosedate"] = estimateddate;
//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);
}
}
}
}
No comments:
Post a Comment