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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Entity collection error

(0) ShareShare
ReportReport
Posted on by

I have done everything i can think of and dont know what else to do.

My custom workflow is falling on "Entity collection" as shown below. Any reason please ?

//Get Opportunity ID
Guid OpportunityID = this.GetOpportunity.Get(context).Id;

tracingService.Trace("Retrieve OpportunityID" + OpportunityID);

string fetchQuery = @"<fetch distinct='false' no-lock='false' mapping='logical'>
< entity name = 'opportunityproduct' >
< attribute name = 'opportunityproductid' />
< attribute name = 'opportunityproductname' />
< order descending = 'false' attribute = ''opportunityproductname' />
< filter type = 'and' >
< condition attribute = 'opportunityid' value='{zzz}' operator= 'eq' />
</ filter >
</ entity >
</ fetch >";

tracingService.Trace("Retrieved FetchXML Query");
fetchQuery = fetchQuery.Replace("zzz", OpportunityID.ToString());
tracingService.Trace("Retrieved Fetch Query Trace" + fetchQuery);

SCRIPT FAILS HERE ON ENTITY COLLECTION

EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetchQuery));
tracingService.Trace("Results counted" + results.TotalRecordCount);

345121.error.PNG

*This post is locked for comments

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Entity collection error

    Hello,

    I believe it fails because of following line:

    < order descending = 'false' attribute = ''opportunityproductname' />

    should be

    < order descending = 'false' attribute = 'opportunityproductname' />

  • Jay2014 Profile Picture
    on at
    RE: Entity collection error

    Thanks for spotting that, i have changed it but its not made a difference.

    Thanks,

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Entity collection error

    Can you please provide full code?

  • Jay2014 Profile Picture
    on at
    RE: Entity collection error

    I would really appreciate you help if you can help with this

    using System;

    using System.ServiceModel;

    using System.Activities;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Workflow;

    using System.Collections.Generic;

    using Microsoft.Crm.Sdk.Messages;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk.Query;

    using PLContext;

    using System.Linq;

    // www.youtube.com/watch

    namespace CopyOpportunityLines

    {

       /// </summary>    

       public class CopyOpportunityLines : CodeActivity

       {

           //private object _serviceProxy;

           #region Properties

           //Property for Target Entity

           // xrmtoolkit.com/.../PluginWizard

           // Required for Entity Agreement

           [RequiredArgument]

           [Input("Agreement")]

           [ReferenceTarget(msdyn_agreement.EntityLogicalName)]

           public InArgument<EntityReference> TargetAgreement { get; set; }

           //Property for Entity salesorder

           [RequiredArgument]

           [Input("Opportunity")]

           [ReferenceTarget(Opportunity.EntityLogicalName)]

           public InArgument<EntityReference> GetOpportunity { get; set; }

           #endregion

           /*

           #region Output Properties

           //Property for Entity salesorder detail

           [Output("Contract Detail")]

           [ReferenceTarget("salesorderdetail")]

           public OutArgument<EntityReference> ContractDetail { get; set; }

           #endregion

           */

           /// <summary>

           /// Executes the WorkFlow.

           /// </summary>

           /// <param name="crmWorkflowContext">The <see cref="LocalWorkflowContext"/> which contains the

           /// <param name="executionContext" > <see cref="CodeActivityContext"/>

           /// </param>      

           /// <remarks>

           /// For improved performance, Microsoft Dynamics 365 caches WorkFlow instances.

           /// The WorkFlow's Execute method should be written to be stateless as the constructor

           /// is not called for every invocation of the WorkFlow. Also, multiple system threads

           /// could execute the WorkFlow at the same time. All per invocation state information

           /// is stored in the context. This means that you should not use global variables in WorkFlows.

           /// </remarks>

           protected override void Execute(CodeActivityContext context)

           {

               if (context == null)

               {

                   throw new ArgumentNullException("context");

               }

               //IOrganizationService for communication with CRM

               try

               {

                   // TODO: Implement your custom activity handling.

                   //Retrieve the CrmService so that we can retrieve the loan application

                   //IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();

                   IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();

                   IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();

                   IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

                   ITracingService tracingService = context.GetExtension<ITracingService>();

                   // Retrieve Agreement Entity Reference

                   tracingService.Trace("Retrieved Org Context, Service and Service Factory");

                   // Get TargetEntity ID

                   Guid AgreementId = this.TargetAgreement.Get(context).Id;

                   //Get Opportunity ID

                   Guid OpportunityID = this.GetOpportunity.Get(context).Id;

                   tracingService.Trace("Retrieve OpportunityID" + OpportunityID);

                   string fetchQuery = @"<fetch distinct='false' no-lock='false' mapping='logical'>

                                        < entity name = 'opportunityproduct' >

                                         < attribute name = 'opportunityproductid' />

                                          < attribute name = 'fc_agreement' />

                                           < attribute name = 'opportunityproductname' />

                                            < order descending = 'false' attribute = 'fc_agreement' />

                                        < filter type = 'and' >

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

                                         </ filter >                                        

                                           </ entity >                                            

                                        </ fetch >";

                   tracingService.Trace("Retrieved FetchXML Query");

                   fetchQuery = fetchQuery.Replace("zzz", OpportunityID.ToString());

                   tracingService.Trace("Retrieved Fetch Query Trace" + fetchQuery);

                   EntityCollection results = service.RetrieveMultiple(new FetchExpression(fetchQuery));

                   tracingService.Trace("Results counted" + results.TotalRecordCount);

                   // Run the query with the FetchXML

                   try

                   {

                       if (results.Entities.Count > 0)

                           foreach (var opprtunitlines in results.Entities)

                       {

                                 tracingService.Trace("For each loop);

                       }

                   }

                   catch (FaultException ex)

                   {

                       throw new InvalidPluginExecutionException("An error occurred in the plug-in - Results." + results, ex);

                   }

               }

               catch (FaultException ex)

               {

                   throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);

               }        

           }

       }

    }

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Entity collection error

    My suggestion - remove all try/catch statements to see real issue.

  • Jay2014 Profile Picture
    on at
    RE: Entity collection error

    Thanks, The XML its passing is not formatted well.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans