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.
This is built in VisualStudio 2017 using the 365SDK Plugin Registration tool to create the plugin.
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
{
/// <summary>
/// PreOperationipmahr_trainerapplicationUpdate Plugin.
/// Fires when the following attributes are updated:
/// ipmahr_transferhistory
/// </summary>
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<bool>("ipmahr_transferhistory");
Guid recordID = Training.GetAttributeValue<Guid>("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 tried just populating a variable within the QueryExpression and then updating (to test) outside of the QE. But just the existence of the QE seems to be the problem.
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.
System.ServiceModel.CommunicationObjectFaultedException: The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
Been meaning to get back to this. It turns out it wasn't the code at all, but rather the profiler service and/or the sandbox service. (I'm not sure which as the hosting company hasn't gotten back to me. But the code is now working after they did something to the server over the weekend.) But thanks for answering.