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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

how does one test for success of IOrganization.Retrieve?

(0) ShareShare
ReportReport
Posted on by

Here's the code -- maybe I'm missing something fundamental:

Entity CfP = service.Retrieve("msdyn_contactforparty", salesorder.GetAttributeValue("msdyn_contactperson").Id, new ColumnSet("msdyn_contactid"));
                            if (null != CfP && CfP.Contains("msdyn_contactid")) {
                                Entity forUpdate = new Entity("salesorder", salesorder.Id);
                                forUpdate.Attributes.Add("new_contact", CfP.GetAttributeValue("msdyn_contactid"));
                                service.Update(forUpdate);
                            }

This is in a plugin.  The problem is, while it works fine at solving the requirement, I am being slathered with system job failures when I don't even expect it to fire, System.NullReferenceException: Object reference not set to an instance of an object.

  • I test and step filter on msdyn_contactperson, so that has to be there
  • the step is on Update of salesorder, so there has to be a salesorder
  • which leaves me questioning CfP

Microsoft doesn't document the result of the failure to find a record to retrieve.  Is my test for null wrong?  Is it necessary to trap an error on the retrieve?  (Of course, how can there be the lookup on the salesorder yet the contactperson not exist!)

Can anyone see where there might be a null object reference?

TIA

PS- sorry for the debugging question

I have the same question (0)
  • Community Member Profile Picture
    on at

    I noticed that I don't test for CfP["msdyn_contactid"] but, there are no CfP lacking such in the system AND there is no concept of a CONTACTforParty without the contact.

  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    technically you will get an exception if the retrieve fails (because the record is not found) and not a null, so the null check is wrong.

    you can make your query to a retrievemultiple and adding the condition with the primary key of the entity (that should be msdyn_contactforpartyid in your case) with the Id value, if you get a single element inside the EntityCollection the record is found otherwise EntityCollection is empty.

    This to avoid the exception, I have no way to check your code but I think maybe there is a mismatch on the Id you are passing to the retrieve

    hope it helps

  • Community Member Profile Picture
    on at

    You have answered both problems.  The failure is coming because I am pulling a contact lookup rather than the CfP lookup.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans