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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Using Query Expression creates OrganizationServiceFault

(0) ShareShare
ReportReport
Posted on by 25

I need some help. This is the first QueryExpression I've used since i had to update my SDK's to 365.

I'm not even doing anything hard at this point, but if I use QueryExpression in the code (like I have done 100s of times before) I get an OrganizationServiceFault.

So here is the basic code (and it's truly basic at this point)

using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace TrainerAppHistory.TransferHistory
{

    /// 
    /// PreOperationipmahr_trainerapplicationUpdate Plugin.
    /// Fires when the following attributes are updated:
    /// ipmahr_transferhistory
    ///     
    public class PreOperationipmahr_trainerapplicationUpdate : PluginBase
    {

        public PreOperationipmahr_trainerapplicationUpdate(string unsecure, string secure)
            : base(typeof(PreOperationipmahr_trainerapplicationUpdate))
        {

            // TODO: Implement your custom configuration handling.
        }
        private readonly string preImageAlias = "PreImage";
        protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new InvalidPluginExecutionException("localContext");
            }

            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService server = localContext.OrganizationService;
            Entity Training = (Entity)context.InputParameters["Target"];
            Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            EntityReference Contact = (EntityReference)preImageEntity.Attributes["ipmahr_contactid"];
            
            bool transfer = Training.GetAttributeValue("ipmahr_transferhistory");
            Guid recordID = Training.GetAttributeValue("ipmahr_trainerapplicationid");
            Guid contactid = Contact.Id;
            

            if (transfer == true)
            {
                QueryExpression TopRecord = new QueryExpression("ipmahr_trainerapplication");
                TopRecord.ColumnSet = new ColumnSet("ipmahr_trainerapplicationid");
                 EntityCollection results1 = server.RetrieveMultiple(TopRecord);

                if (results1.Entities.Count > 1)
                {
                    Training.Attributes["new_comments"] = "found them all";
                   
                }

                //End Code

            }
        }
    }
}


When I remove the QueryExpression and just use a simple update to the Record like this
                    Training.Attributes["new_comments" = "This works";
Then I have no errors updating the record.

I'm at a loss on what to look at next. Nothing I've found seems to be helping.  I used the Register Plugin Tool for generating this plugin.

Plugin Tracing shows the following, which I think is the key to this. But I'm not having much luck finding answers.

<OriginalException>System.ServiceModel.CommunicationObjectFaultedException: The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
I have the same question (0)
  • Iamoop Profile Picture
    25 on at

    I went and moved the Update line outside of the QE and just used the QE to populate a variable. But the same issue happens. Just thought I'd update on what I've done so far.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 107 Super User 2026 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 64 Most Valuable Professional

#3
ParthPatel249 Profile Picture

ParthPatel249 62

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans