Hi,
I am trying to copy products from opportunity products to quote quote products
I tried this code using plugin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace Products
{
public class MyProducts : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the organization service reference.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "quote")
{
if (entity.Attributes.Contains("opportunityid"))
{
var optyRef = ((EntityReference)(entity.Attributes["opportunityid"]));
var fetchData = new
{
OPTYGUID = optyRef.Id.ToString()
};
var fetchXml = $@"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='product'>
<attribute name='name' />
<attribute name='productid' />
<attribute name='productnumber' />
<attribute name='description' />
<attribute name='statecode' />
<attribute name='productstructure' />
<attribute name='defaultuomid' alias='defaultUOMID' />
<attribute name='transactioncurrencyid' alias='defaultCurrency' />
<order attribute='productnumber' descending='false' />
<link-entity name='opportunityproduct' from='productid' to='productid' link-type='inner' alias='ai'>
<attribute name='priceperunit' />
<attribute name='quantity' />
<attribute name='extendedamount' />
<attribute name='opportunityproductid' />
<filter type='and'>
<condition attribute='opportunityid' operator='eq' value='{fetchData.OPTYGUID}' />
</filter>
</link-entity>
</entity>
</fetch>";
EntityCollection optyProdCollection = service.RetrieveMultiple(new FetchExpression(fetchXml.ToString()));
if (null == optyProdCollection || null == optyProdCollection.Entities || optyProdCollection.Entities.Count == 0)
{
return;
}
else
{
foreach (Entity optyProds in optyProdCollection.Entities)
{
Guid uomtoUse = ((EntityReference)optyProds.GetAttributeValue<AliasedValue>("defaultUOMID").Value).Id;
Entity quoteProdCreate = new Entity("quotedetail");
quoteProdCreate["quoteid"] = entity.Id;
//quoteProdCreate["quoteproductname"] = optyProds.GetAttributeValue<string>("name");
quoteProdCreate["productid"] = optyProds.GetAttributeValue<Guid>("productid");
quoteProdCreate["uomid"] = new EntityReference("uom", uomtoUse);
quoteProdCreate["quantity"] = optyProds.GetAttributeValue<double>("quantity");
quoteProdCreate["extendedamount"] = optyProds.GetAttributeValue<double>("extendedamount");
quoteProdCreate["ispriceoverridden"] = true;
service.Create(quoteProdCreate);
}
}
}
}
}
else
{
throw new InvalidPluginExecutionException("Exception occured");
}
}
}
}
But getting this error
{Exception details: ErrorCode: 0x80040216 Message: An unexpected error occurred. TimeStamp: 2021-01-03T12:32:35.4921720Z -- Exception details: ErrorCode: 0x80040216 Message: System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'Microsoft.Xrm.Sdk.EntityReference'. at Microsoft.Crm.ObjectModel.DefaultCurrency.TryGetCurrencyFromParent(EntityDataInformation entityDataInformation, EntityReference& transactionCurrencyEntityReference) +0x0 at Microsoft.Crm.ObjectModel.DefaultCurrency.SetDefaultCurrency(EntityDataInformation entityDataInformation) +0x14 at Microsoft.Crm.ObjectModel.DoubleAttributeValidator.GetRecordCurrency(MoneyAttributeMetadata moneyMetadata, Entity entity, ExecutionContext platformContext) +0x78 at Microsoft.Crm.ObjectModel.DoubleAttributeValidator.UpdatePrecision(Nullable`1 decimalValue, Nullable`1 floatValue, Money moneyValue, DoubleAttributeMetadata doubleMetadata, Entity entity, String propertyName, ExecutionContext platformContext) +0xd3 at Microsoft.Crm.ObjectModel.DoubleAttributeValidator.Validate(Entity entity, KeyValuePair`2 property, AttributeMetadata attributeMetadata, ExecutionContext platformContext) +0x13d at Microsoft.Crm.ObjectModel.AttributeValidationPlugin.PerformValidation(Entity entity, EntityMetadata entityMetadata, ExecutionContext platformContext, Entity parentEntity) +0x3d at Microsoft.Crm.ObjectModel.AttributeValidationPlugin.Execute(IServiceProvider serviceProvider) +0x68 at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context) +0x399 at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context) +0x7: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #46906C30 TimeStamp: 2021-01-03T12:32:35.4921720Z -- }
Can any one please help me regarding this