I have a plugin that run on update of look up field. So basically, when a look up is filled with any record, the plugin will get the GUID from the record inside the look up field and do some logic with it's GUID.
Now i want to make this plugin keep run but doing nothing when the record inside the look up field is deleted. When i delete the record inside the look up, it's show an error says "object reference not set to an instance of an object". Cos my plugin unable to get any records from the look up field.
In short, before the plugin doing some logic, i want to check wheter the look up is empty or not.
I have tying this, but still it's show the same error message:
public void Execute(IServiceProvider provider) { IPluginExecutionContext context = (IPluginExecutionContext)provider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory factory = (IOrganizationServiceFactory)provider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService _service = factory.CreateOrganizationService(context.UserId); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity entity = (Entity)context.InputParameters["Target"]; if (entity.LogicalName != "lnkt_troubleticket") return; if (entity.Attributes.Contains("lnkt_mainticketid"))//name of the look up field { Guid referal_id = Guid.Empty; referal_id = (entity.GetAttributeValue<EntityReference>("lnkt_mainticketid")).Id; //get look up data if(referal_id != null){ // Do some logic } } } }
How to avoid this ?
My plugin run on Pre-Operation with Update Message (Look up)
*This post is locked for comments
Hi ben, thanks it was helpfull
Hi Fikri,
Try changing the code as shown below.
if (entity.Attributes.Contains("lnkt_mainticketid") && entity["lnkt_mainticketid"]!=null)//name of the look up field { Guid referal_id = Guid.Empty; referal_id = (entity.GetAttributeValue<EntityReference>("lnkt_mainticketid")).Id; //get look up data if (referal_id != null) { // Do some logic } }
Hope this helps.
When you delete the link to the lookup record the value within the lnkt_mainticketid entity reference is null rather than a link to the reference. So your code needs to look like this with the check done on the entityreference itself.
if ((entity.GetAttributeValue<EntityReference>("lnkt_mainticketid"))!=null){ Guid referal_id = Guid.Empty; referal_id = (entity.GetAttributeValue<EntityReference>("lnkt_mainticketid")).Id;
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,228 Super User 2024 Season 2
Martin Dráb 230,056 Most Valuable Professional
nmaenpaa 101,156