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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Invalid XML in plugin

(0) ShareShare
ReportReport
Posted on by

Hi,

Im trying to fetch a case's associated custom entity 'productset' 's subgrid record by using a fetch xml. For which i am getting exception as invalid xml. The xml query  is:

string fetch3 = @"
<fetch mapping='logical'>
<entity name='new_productset'>
<attribute name='new_productsetid' />
<attribute name='new_name' />
<attribute name='createdon' />
<filter type='and'>
<condition attribute='new_associatedcase' operator='eq' uiname='"+objContext["title"].ToString()+@"'uitype='incident' value='"+caseorderId.ToString()+@"'/>
</filter>
</entity>
</fetch>";

The plugin step is on update of case record post operation

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Shantnu Sharma Profile Picture
    655 on at

    Hi - Please try it by keeping only:

    <condition attribute='new_associatedcase' operator='eq'  value='"+caseorderId.ToString()+@"'/>

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to use following Fetch:

    string fetch3 = @"

    <fetch mapping='logical'>

    <entity name='new_productset'>

    <attribute name='new_productsetid' />

    <attribute name='new_name' />

    <attribute name='createdon' />

    <filter type='and'>

    <condition attribute='new_associatedcase' operator='eq' value='"+caseorderId.ToString()+@"'/>

    </filter>

    </entity>

    </fetch>";

    uiname and uitype are not required for a Fetch to be valid.

    Can you please provide a code where you get caseorderId value?

  • Community Member Profile Picture
    on at

    Thanks! It worked. But I am not able to update a field of a case record. Find the below full code:

    using System;

    using System.ServiceModel;

    using System.ServiceModel.Description;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Discovery;

    using System.Text;

    namespace GridToDescription

    {

       public class GridToDesc : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               try

               {

                   ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                   // Obtain the execution context from the service provider.

                   IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                   // Get a reference to the Organization service.

                   IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService service = factory.CreateOrganizationService(context.UserId);

                   if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                   {

                       Entity objContext = (Entity)context.InputParameters["Target"];

                       if (objContext.LogicalName != "incident")

                       {

                           tracingService.Trace("1");

                           return;

                       }

                       else

                       {

                           //Entity SourceCaseID = (Entity)service.Retrieve("incident", context.PrimaryEntityId, new ColumnSet(new string[] { "ticketnumber" }));

                           //Entity SourceCaseID1 = (Entity)service.Retrieve("incident", context.PrimaryEntityId, new ColumnSet(new string[] { "incidentid" }));

                           //QueryExpression query1 = new QueryExpression();

                           //tracingService.Trace("2");

                           //query1.EntityName = "new_productset";

                           //query1.ColumnSet =new ColumnSet("new_ProductNames", "new_Quantity") ;

                           //Guid caseguid = entity.Id;

                           //tracingService.Trace(SourceCaseID.ToString());

                           //tracingService.Trace(SourceCaseID1.ToString());

                           //tracingService.Trace(caseguid.ToString());

                           //query1.Criteria.AddCondition("new_associatedcase", ConditionOperator.Equal, caseguid); //replace parentcustomerid with parent entity name and guidoftheparentaccount with guid of the customer

                           //tracingService.Trace("2.3");

                           //                        string fetch2 = @"

                           //   <fetch mapping='logical'>

                           //                             <entity name='new_productset'>

                           //        <attribute name='accountid'/>

                           //        <attribute name='name'/>

                           //        <link-entity name='systemuser' to='owninguser'>

                           //           <filter type='and'>

                           //              <condition attribute='lastname' operator='ne' value='Cannon' />

                           //           </filter>

                           //        </link-entity>

                           //     </entity>

                           //   </fetch> ";

                           //tracingService.Trace(entity.LogicalName.ToString());

                           tracingService.Trace("entry");

                           tracingService.Trace("entry2");

                           if (objContext.Attributes.Contains("title"))

                           {

                               tracingService.Trace(Convert.ToString(objContext.Attributes["title"]));

                           }

                           else

                           {

                               throw new InvalidPluginExecutionException("no title");

                               tracingService.Trace("@1");

                           }

                           Guid caseorderId = objContext.Id;

                           tracingService.Trace("@2");

                           tracingService.Trace(objContext.Id.ToString());

                           tracingService.Trace(caseorderId.ToString());

                           tracingService.Trace("@3");

                           string fetch3 = @"

                                       <fetch mapping='logical'>

                                   <entity name='new_productset'>

                                   <attribute name='new_productsetid' />

                                   <attribute name='new_name' />

                                   <attribute name='createdon' />

                                   <attribute name='new_productnames' />

                                   <attribute name='new_quantity' />

                                   <filter type='and'>

                                     <condition attribute='new_associatedcase' operator='eq'  value='" + caseorderId.ToString() + @"'/></filter>

                                 </entity>

                               </fetch>";

                           tracingService.Trace(fetch3.ToString());

                           try

                           {

                               EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetch3));

                           }

                           catch (Exception vb)

                           {

                               tracingService.Trace(vb.Message);

                           }

                           EntityCollection result2 = service.RetrieveMultiple(new FetchExpression(fetch3));

                           //                        EntityCollection response;

                           //var response = service.RetrieveMultiple(query1);

                           StringBuilder builder = new StringBuilder();

                           //tracingService.Trace("3");

                           int count = 0;

                           //if (response != null && response.Entities.Count > 0)

                           //{

                           //    tracingService.Trace("4");

                           if (result2.TotalRecordCount != 0)

                           {

                               foreach (var subgridrecord in result2.Entities)

                               {

                                   tracingService.Trace("5");

                                   ++count;

                                   builder.Append(Convert.ToString(count) + ". " + " Product Name" + ": " + Convert.ToString(subgridrecord.FormattedValues["new_productnames"]) + " " + "Product Quantity: " + Convert.ToString(subgridrecord.Attributes["new_quantity"]));

                               }

                           }

                           count = 0;

                           //}

                           //else

                           //{

                           //    tracingService.Trace("6");

                           //    count = 0;

                           //    builder.Append("*End");

                           //}

                           tracingService.Trace("7");

                           // Set the required attributes. For account, only the name is required.

                           // See the Entity Metadata topic in the SDK documentatio to determine

                           // which attributes must be set for each entity.

                           tracingService.Trace("8");

                           tracingService.Trace(builder.ToString());

                           objContext.Attributes.Add("new_caseproductset", builder.ToString());

                           service.Update(objContext);

                           tracingService.Trace("9");

                       }

                   }

               }

               catch (FaultException<OrganizationServiceFault> ex)

               {

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

               }

               catch (Exception ex)

               {

                   ITracingService tracingService =

                   (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                   tracingService.Trace("FollowupPlugin: {0}", ex.ToString());

                   throw;

               }

           }

       }

    }

  • Community Member Profile Picture
    on at

    Find the below same code with unnecessary construct cleaned up:

    using System;

    using System.ServiceModel;

    using System.ServiceModel.Description;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Discovery;

    using System.Text;

    namespace GridToDescription

    {

       public class GridToDesc : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               try

               {

                   ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                   IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                   IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                   IOrganizationService service = factory.CreateOrganizationService(context.UserId);

                   if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                   {

                       Entity objContext = (Entity)context.InputParameters["Target"];

                       if (objContext.LogicalName != "incident")

                       {

                           tracingService.Trace("1");

                           return;

                       }

                       else

                       {

                           tracingService.Trace("entry");

                           Guid caseorderId = objContext.Id;

                           tracingService.Trace("@3");

                           string fetch3 = @"

                                       <fetch mapping='logical'>

                                   <entity name='new_productset'>

                                   <attribute name='new_productsetid' />

                                   <attribute name='new_name' />

                                   <attribute name='createdon' />

                                   <attribute name='new_productnames' />

                                   <attribute name='new_quantity' />

                                   <filter type='and'>

                                     <condition attribute='new_associatedcase' operator='eq'  value='" + caseorderId.ToString() + @"'/></filter>

                                 </entity>

                               </fetch>";

                           EntityCollection result2 = service.RetrieveMultiple(new FetchExpression(fetch3));

                           StringBuilder builder = new StringBuilder();

                           int count = 0;

                           if (result2.TotalRecordCount != 0)

                           {

                               foreach (var subgridrecord in result2.Entities)

                               {

                                   tracingService.Trace("5");

                                   ++count;

                                   builder.Append(Convert.ToString(count) + ". " + " Product Name" + ": " + Convert.ToString(subgridrecord.FormattedValues["new_productnames"]) + " " + "Product Quantity: " + Convert.ToString(subgridrecord.Attributes["new_quantity"]));

                               }

                           }

                           count = 0;

                           tracingService.Trace(builder.ToString());

                           tracingService.Trace("9");

                           objContext.Attributes.Add("new_caseproductset", builder.ToString());

                           service.Update(objContext);

                       }

                   }

               }

               catch (FaultException<OrganizationServiceFault> ex)

               {

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

               }

               catch (Exception ex)

               {

                   ITracingService tracingService =

                   (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                   tracingService.Trace("FollowupPlugin: {0}", ex.ToString());

                   throw;

               }

           }

       }

    }

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I'm sorry but it's a bit complicated to guess why your code doesn't work. I would suggest to learn how to troubleshoot your plugin. This video describes whole process - www.youtube.com/watch

  • abvogel Profile Picture
    150 on at

    Maybe just update the stuff that's changing? You're sending your entire context back into update, which tries to update everything in it.

    Something like this:

    Entity updateEntity = new Entity("new_productset");

    updateEntity.id = caseorderId;

    updateEntity["new_caseproductset"] = builder.ToString();

    service.Update(updateEntity);

  • Tim Dutcher Profile Picture
    2,100 on at

    As a general tip, to help determine where an application is failing, it's usually a good idea to create small single-purpose methods rather than a method that does lots of tasks.

    See the first answer in the following article...

    programmers.stackexchange.com/.../is-it-ok-to-split-long-functions-and-methods-into-smaller-ones-even-though-they

    In your case, you can start by splitting out your CRM query and update operations into their own methods. This will make it much easier to follow the logic of the code and will allow you to test each method in isolation.

  • Shantnu Sharma Profile Picture
    655 on at

    Which line throws the error here or the error occurs on service.Update(objContext);? If yes, please share the exception details. Also, I would agree to Andrew. Don't update the complete context object. Initialize a new object for Entity.

  • Community Member Profile Picture
    on at

    Hi,

    I am getting exception when isolating the entity like Andrew said with the error being in the below code:

    Entity updateEntity = new Entity("new_productset");

                           updateEntity.Id = caseorderId;

                           tracingService.Trace("10");

                           updateEntity["new_caseproductset"] = builder.ToString();

                           service.Update(updateEntity);

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    As far as I understood you want to update case field then you should use

    Entity updateEntity = new Entity("incident");

    instead of

    Entity updateEntity = new Entity("new_productset");

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans