Hi guys. I have an entity called "order". In this entity i placed look up field which will get customer name from "Product" entity.
I tried to write some code which i found in internet.
here is the code:
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "new_order")
return;
var product = ((Microsoft.Xrm.Sdk.EntityReference)(entity.Attributes["new_productname"]));
var productname = service.Retrieve(product.LogicalName, product.Id, new ColumnSet(true));
var namefield = productname["new_productname"].ToString();
throw new InvalidPluginExecutionException(namefield);
}
But it shows me error message: " The given key was not present in the dictionary."
Can anybody helpme ?
Thanks
EDIT
Hei guys, after searching on internet. Finally i found what i want. Here is the code i've created:
if (entity.Attributes.Contains("new_productname"))
{
Entity productreference = service.Retrieve("new_product", ((EntityReference)entity["new_productname"]).Id, new ColumnSet(true));
if (productreference.Attributes.Contains("new_productquantity"))
{
string productquantity= productreference.Attributes["new_productquantity"].ToString();
entity["new_unitnumber"] = productquantity;
}
}
this is will get product quantity from product entity, and place it into a field within order entity. This code work perfectly when the plugin running on "Update". But when i change to on "Create" its shows me error message.
Correctly if im wrong, what was happen when i run this plugin on "Create" event is when this code tryng to get value from look up field. Its returning null.
Any solution for this ?
Thanks