Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin Registered on update of look up field

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin Registered on update of look up field

    Hi ben, thanks it was helpfull

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Plugin Registered on update of look up field

    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.

  • Verified answer
    Ben Thompson Profile Picture
    Ben Thompson 6,350 on at
    RE: Plugin Registered on update of look up field

    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; 


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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans