Hi,
When I register my plugin in synchronous mode it gives error "Null Pointer Exception" but when I register the plugin on asynchronous mode and it works fine .I checked it through plugin profiler there I found that it is not getting context.PrimaryEntityId . I have also checked the depth and depth is 1.
What may be reason of plugin working fine on async mode but not in sync mode?
*This post is locked for comments
Hi,
To get the primaryEntityId you cannot do this
Guid primaryEntityId = entity.Id;
because the entity which you get from Target in InputParamets collection, for create events, does not have the Id yet
The context.PrimaryEntityId works when async because in async mode the plugin runs outside the create operation transaction, and when the context passed to the plugin is built the record is already created, so there is aready a entity Id
To work correctly for both sync (post-operation) and async you shoud get the Id this way:
Guid primaryEntityId = (Guid)context.OutputParameters["id"];
I believe your main issue is related to fact that you don't check if field is populated on entity like following:
Leadid = objorserproduct.GetAttributeValue<EntityReference>("new_leadid").Id;
If record has new_leadid field empty - you will get null pointer exception so to avoid that error you have to check if field is populated.
Also you should learn how to troubleshoot plugins using Developer Toolkit and Plugin Profiler - www.youtube.com/watch
The code is as follows:
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService serviceProxy = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
if (context.Depth > 1) { return; }
Entity entity = (Entity)context.InputParameters["Target"];
Guid primaryEntityId = entity.Id;
decimal Amount = decimal.Zero;
decimal Quantity = decimal.Zero;
Guid Product = Guid.Empty;
Guid TaxGroup = Guid.Empty;
Guid Tax = Guid.Empty;
decimal Percentage = decimal.Zero;
decimal NewAmount = decimal.Zero;
Guid ProductName = Guid.Empty;
Guid TaxGroupName = Guid.Empty;
Guid Leadid = Guid.Empty;
decimal productTaxAmount = decimal.Zero;
Entity objorserproduct = serviceProxy.Retrieve("new_leadline", primaryEntityId, new ColumnSet(true));
Leadid = objorserproduct.GetAttributeValue<EntityReference>("new_leadid").Id;
Product = objorserproduct.GetAttributeValue<EntityReference>("new_productid").Id;
Amount = objorserproduct.GetAttributeValue<Money>("new_amount").Value;
Quantity = objorserproduct.GetAttributeValue<Decimal>("new_quantity");
Entity ExistingProduct = serviceProxy.Retrieve("product", objorserproduct.GetAttributeValue<EntityReference>("new_productid").Id, new ColumnSet("new_taxgroup"));
TaxGroup = ExistingProduct.GetAttributeValue<EntityReference>("new_taxgroup").Id;
Entity TaxGroup1 = serviceProxy.Retrieve("new_taxgroup", TaxGroup, new ColumnSet(true));
if (context.MessageName == "Create")
{
Entity ObjTaxDetail = new Entity("new_taxdetails");
ObjTaxDetail.Attributes["new_product"] = new EntityReference("product", Product);
ObjTaxDetail.Attributes["new_taxgroup"] = new EntityReference("new_taxgroup", TaxGroup);
ObjTaxDetail.Attributes["new_quantity"] = Quantity;
ObjTaxDetail.Attributes["new_amountbeforetax"] = new Money(Amount);
ObjTaxDetail.Attributes["new_leadid"] = new EntityReference("lead", Leadid);
Guid taxDetailGuid = serviceProxy.Create(ObjTaxDetail);
QueryExpression TaxDetailQuery = new QueryExpression("new_taxgroupdetails");
TaxDetailQuery.ColumnSet = new ColumnSet(true);
ConditionExpression cond2 = new ConditionExpression("new_taxgroup", ConditionOperator.Equal, TaxGroup1.Id);
TaxDetailQuery.Criteria.AddCondition(cond2);
EntityCollection TaxDetailQuerycollection = serviceProxy.RetrieveMultiple(TaxDetailQuery);
if (TaxDetailQuerycollection.Entities.Count > 0)
{
foreach (Entity ObjTaxDetailColl in TaxDetailQuerycollection.Entities)
{
Tax = ObjTaxDetailColl.GetAttributeValue<EntityReference>("new_tax").Id;
Percentage = ObjTaxDetailColl.GetAttributeValue<Decimal>("new_percentage");
NewAmount = (Amount * Percentage) / 100;
Entity objTaxBreakup = new Entity("new_taxbreakupdetails");
objTaxBreakup.Attributes["new_taxdetail"] = new EntityReference("new_taxdetails", taxDetailGuid);
objTaxBreakup.Attributes["new_amount"] = new Money(NewAmount);
objTaxBreakup.Attributes["new_tax"] = new EntityReference("new_tax", Tax);
serviceProxy.Create(objTaxBreakup);
productTaxAmount += NewAmount;
}
}
QueryExpression TaxBreakupQuery = new QueryExpression("new_taxdetails");
TaxBreakupQuery.ColumnSet = new ColumnSet(true);
ConditionExpression cond1 = new ConditionExpression("new_product", ConditionOperator.Equal, Product);
ConditionExpression leadQuery = new ConditionExpression("new_leadid", ConditionOperator.Equal, Leadid);
TaxBreakupQuery.Criteria.AddCondition(cond1);
TaxBreakupQuery.Criteria.AddCondition(leadQuery);
EntityCollection taxdetailcollection = serviceProxy.RetrieveMultiple(TaxBreakupQuery);
if (taxdetailcollection.Entities.Count > 0)
{
foreach (Entity taxdetailcoll in taxdetailcollection.Entities)
{
// Entity objNewTaxDtl = serviceProxy.Retrieve("new_taxdetails", taxDetailGuid, new ColumnSet("new_amountaftertax", "new_taxamount"));
taxdetailcoll.Attributes["new_amountaftertax"] = new Money(productTaxAmount + Amount);
taxdetailcoll.Attributes["new_taxamount"] = new Money(productTaxAmount);
// objNewTaxDtl.Attributes["new_quantity"] = new Decimal(productTaxAmount);
serviceProxy.Update(taxdetailcoll);
}
}
}
Hello,
Please provide code you use.
Hi Andrew,
I have also tried this way ...still getting null pointer exception
Hi Guillaume,
Thanks for reply...I am registering my dll in sync mode and post operation on create message.But getting context.PrimaryEntityId as null
Hi,
In which stages is your plugin registered? For async it always is post-operation, i.e. the record is already created. When you are registering it in sync mode, do you change the stage, like pre-operation? If it is registered in pre-operation, it is normal that you get a null pointer exception because the record not at this time created.
Hope it helps
Hello,
I believe that reason lays in the difference of context that is passed to plugin.
Why don't you want to get identifier of record from Target of InputParameters - this approach will work without dependence on type of plugin - Sync/Async.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156