locked
Linq query not returning all fields for an entity (most are null) RRS feed

  • Question

  • I have an issue where if I query for an Opportunity after it was used in a join in a linq statement, the Opportunity is always returned with no data. Here is an example:

     var matchingActivities =
         (from a in _context.CreateQuery(activityType)
         join o in _context.CreateQuery<Opportunity>() on ((EntityReference)a["regardingobjectid"]).Id equals o.OpportunityId
         where
              (o.ContactId.Id == stakeholder || o.glh_SecondaryContact.Id == stakeholder) &&
              o.glh_SalesOffice.Id == salesOfficeId &&
              a.GetAttributeValue<DateTime?>("actualstart") >= startDate
         select a).ToList();

    If I subsequently query the same opportunity that was used in the join, its field are always blank.

    var opportunity = _context.CreateQuery<Opportunity>().FirstOrDefault(o => o.Id == opportunityId);

    I have been banging my head against the wall for hours to try and figure this out. Any help would be greatly appreciated.

    Monday, June 1, 2015 9:13 PM

Answers

  • So I finally figured out what the issue was. It turns out it has to do with how CRM stores the contact associated with an opportunity. I was incorrectly trying to read the ContactId field on the opportunity, when in fact the SDK and ODATA interfaces do not return that field at all. When the opportunity was created, it's CustomerId field was populated, which populates the ContactId field behind the scenes. You can query opportunities by it, but I could not get it to return that field in any query. It was populated in the database, but the field was always returned as null. I switched to using the CustomerId instead and my problem was solved. Its another case of me learning the little nuances of CRM development.
    • Marked as answer by JRodman Tuesday, June 2, 2015 1:36 PM
    Tuesday, June 2, 2015 1:36 PM