web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin Registered on update of look up field

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Verified answer
    Ben Thompson Profile Picture
    6,350 on at

    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; 


  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    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.

  • Community Member Profile Picture
    on at

    Hi ben, thanks it was helpfull

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
JS-09031509-0 Profile Picture

JS-09031509-0 3

#3
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans