Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Early Bound Entity Collection

(0) ShareShare
ReportReport
Posted on by

Hi Devs,

I have a simple fetch that retrieves [Opportunity Lines associated with Customer Asset ]. 

Question

How do i retrieve the [Customer Asset] fields in my for each loop in early bound.

try
{
string fetchQuery = @"
<fetch mapping='logical'>
<entity name='opportunityproduct' >
<attribute name='opportunityproductid' />
<filter type='and' >
<condition attribute='opportunityid' value='{zzz}' operator= 'eq'/>
</filter>
<link-entity name='msdyn_customerasset' alias='ap' link-type='inner' to='tac_customerlift' from='msdyn_customerassetid' >
<attribute name='tac_liftno' />
<attribute name='tac_liftid' />
</link-entity>
</entity>
</fetch>";

tracingService.Trace("Retrieved FetchXML Query");
fetchQuery = fetchQuery.Replace("zzz", OpportunityID.ToString());
EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetchQuery));

// Run the query with the FetchXML
try
{
if (results.Entities.Count > 0)
foreach (var opportunitylines in results.Entities)
{
OpportunityProduct opplines = opportunitylines.ToEntity<OpportunityProduct>();
msdyn_customerasset CustAssett = opportunitylines.ToEntity<msdyn_customerasset>();    // Do i need to add Customer Asset to the entity collection just after the fetch ?

opplines.tacagreement = AgreementReference;
CustAssett.tac_flag = true;
CustAssett.tac_LiftID = "updated";

Error Message

Unhandled exception:
Exception type: Microsoft.Crm.CrmException
Message: An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute): CopyOpportunityLines.CopyOpportunityLines: System.NotSupportedException: Cannot convert entity opportunityproduct to msdyn_customerasset
at Microsoft.Crm.Sandbox.SandboxCodeUnit.ProcessException(Exception originalException, IExecutionContext context, SandboxClient client, SandboxCallTracker callTracker, Boolean isSafeToRetry, DateTime performanceExecutionStartTime, SandboxTracker tracker, Guid parentExecutionId, CrmException& crmException, String& assemblyContents)
at Microsoft.Crm.Sandbox.SandboxCodeUnit.<>c__DisplayClass24_0.<Execute>b__0()
at Microsoft.PowerApps.CoreFramework.ActivityLoggerExtensions.Execute(ILogger logger, EventId eventId, ActivityType activityType, Action action, IEnumerable`1 additionalCustomProperties)
at Microsoft.Xrm.Telemetry.XrmTelemetryExtensions.Execute(ILogger logger, XrmTelemetryActivityType activityType, Action action)
at Microsoft.Xrm.RemotePlugin.CrmProvider.RemotePlugin.Execute(IServiceProvider serviceProvider)
at Microsoft.Xrm.RemotePlugin.CrmProvider.RemotePluginContainer.Execute(IServiceProvider serviceProvider)
at Microsoft.Crm.Workflow.Services.ProxyCustomActivity.Execute(CodeActivityContext executionContext)
-- End stack trace --

*This post is locked for comments

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Early Bound Entity Collection

    Hi,

    before we use any attribute in code, we should retrieve them. add all the required attributed to fetch

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,052 on at
    RE: Early Bound Entity Collection

    Hi

    its your this line thats throwing the exception

    msdyn_customerasset CustAssett = opportunitylines.ToEntity<msdyn_customerasset>();   -- you are trying to convert an Opportunity line to Customer Asset. Comment this line out and you should not get the error.

    You are using early bound classes but you are not using the Service Context. So you can update as shown below

    if you just want to update the following two fields you can do this

                       string fetchQuery = @"

    <fetch mapping='logical'>

    <entity name='opportunityproduct' >

    <attribute name='opportunityproductid' />

    <attribute name='tac_customerlift' />

    <filter type='and' >

    <condition attribute='opportunityid' value='{zzz}' operator= 'eq'/>

    </filter>

    </entity>

    </fetch>";

    foreach (var opportunitylines in results.Entities)
    {
    OpportunityProduct opplines = opportunitylines.ToEntity<OpportunityProduct>();

    Entity ent = new Entity(msdyn_customerasset, opportunitylines.GetAttributeValue<EntityReference>("tac_customerlift").Id)

    ent["tac_flag"] = true;
    ent["tac_LiftID"] = "updated";
    service.Update(ent);


    }

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,399 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans