Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Entity collection error

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

  • Jay2014 Profile Picture
    Jay2014 on at
    RE: Entity collection error

    Thanks, The XML its passing is not formatted well.

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

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

  • Jay2014 Profile Picture
    Jay2014 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);

               }        

           }

       }

    }

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

    Can you please provide full code?

  • Jay2014 Profile Picture
    Jay2014 on at
    RE: Entity collection error

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

    Thanks,

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 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' />

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans