Senario:
Price is a field in opportunity product. we need to update the Opportunity entity Est. Revenue field
with the price:
this is for create a record in opportunity product entity.
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 Estrevenueoncreate:IPlugin
{
public Money totalestrevenue;
public Entity entity1;
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(
new String[] { "estimatedvalue" });
var contact1 = service.Retrieve("opportunity", var1.Id, cols);
if (contact1.Attributes.Keys.Contains("estimatedvalue") == false)
{
totalestrevenue = priceamt;
}
else
{
// int timeofitems = Convert.ToInt32(contact1["new_price"]);
Money estimatedamt = (Money)contact1["estimatedvalue"]; //
// totaltime = timeofitems + timeonitems;
totalestrevenue = new Money() { Value = (estimatedamt.Value + priceamt.Value) };//estimatedamt + priceamt;
}
contact1["estimatedvalue"] = totalestrevenue;
service.Update(contact1);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}
Price is a field in opportunity product. we need to update the Opportunity entity Est. Revenue field
with the price:
this is for create a record in opportunity product entity.
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 Estrevenueoncreate:IPlugin
{
public Money totalestrevenue;
public Entity entity1;
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(
new String[] { "estimatedvalue" });
var contact1 = service.Retrieve("opportunity", var1.Id, cols);
if (contact1.Attributes.Keys.Contains("estimatedvalue") == false)
{
totalestrevenue = priceamt;
}
else
{
// int timeofitems = Convert.ToInt32(contact1["new_price"]);
Money estimatedamt = (Money)contact1["estimatedvalue"]; //
// totaltime = timeofitems + timeonitems;
totalestrevenue = new Money() { Value = (estimatedamt.Value + priceamt.Value) };//estimatedamt + priceamt;
}
contact1["estimatedvalue"] = totalestrevenue;
service.Update(contact1);
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}
No comments:
Post a Comment