BThompson
BThompson

Reputation: 350

Retrieving related entities with early bound classes returns null

Some background: I'm writing a custom workflow activity for CRM 2011 in C# and I am using early bound classes generated by CrmSvcUtil.exe. My plugin takes an opportunity as its only input and is supposed to check its related activities, then set a field on the opportunity to denote whether the opportunity needs more follow-up. My problem currently is that whenever I attempt to retrieve the related activities, the result is null. Here's the relevant part of my code:
Opportunity currentOpportunity = (Opportunity) service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));
currentOpportunity.Opportunity_ActivityPointers

I was under the impression that since there is a one-to-many relationship between Opportunity and Activity that this would grab all the relevant activities, but it doesn't seem to be doing that.
I'm still new to CRM and C#, so any insight as to what I'm doing wrong is appreciated!

Upvotes: 1

Views: 2416

Answers (1)

lazarus
lazarus

Reputation: 2017

if you are using early bound classes, first create data context (in my case it is XrmServiceContext). You can retrieve all ActivityPointers where Regarding object is your opportunity.

OrganizationServiceProxy orgserv;    
using(var xrm = new XrmServiceContext(orgserv))
    {
     //Opportunity currentOpportunity = ...

     IQueryable<ActivityPointer> activityPointers = xrm.ActivityPointerSet.Where(a =>
       a.RegardingObjectId == currentOpportunity.ToEntityReference());
    }

ActivityPointer contains ActivityId and ActivityTypeCode if you need some specific activity from this set. More details here.

Hope it helps :)

Upvotes: 2

Related Questions