Hi all,
I have a problem, but cannot seem to find the answer why this is happening.
I have a plugin that works fine in our testing environment, but when we use it in our production environment, it errors.
What does this plugin do?
It is being called on associate and is being registered as post operation synchronous. It cannot be changed to asynch, because it updates a value on opportunity entity (which will cause fields to become visible on the opportunity). I tried to change it to pre operation, but also failure.
The code in associate is being fired when opportunity is being created, but could also be fired after opportunity update.
Problem is that when we run this in test, all works fine. Opportunity is being updated, and field is showing.
But when we use this Plugin in production environment, Plugin is giving an error "Opportunity with ID x-x-x cannot be found".
Message is clear, but I do not understand why it is giving this error, because I retrieve the ID of the opportunity that is present in the execution context.
Code that I have :
ITracingService tracing = (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 (tracing == null)
{
throw new InvalidPluginExecutionException("Error trying to initialize the plugin. tracing could not be found in SEC.CRM.AssociateDisassociate (Associate).");
}
else
{
tracing.Trace("Plugin fired for SEC.CRM.AssociateDisassociate (Associate).");
}
if (service == null)
{
tracing.Trace("Error trying to initialize the plugin. service could not be made in SEC.CRM.AssociateDisassociate.");
throw new InvalidPluginExecutionException("Error trying to initialize the plugin. Service could not be made.");
}
if (context.Depth == 2)
{
tracing.Trace("Context.Depth == 2");
if (context.MessageName == "Associate")
{
tracing.Trace("Context.MessageName == Associate");
if (context.InputParameters.Contains("Relationship"))
{
string relationshipName = context.InputParameters["Relationship"].ToString();
tracing.Trace("Relationship name : " + relationshipName);
if (relationshipName == "sec_opportunity_sec_solution.")
{
tracing.Trace("Relationship has been confirmed.");
if (context.InputParameters.Contains("Target"))
{
tracing.Trace("Target has been found.");
tracing.Trace("Target has type : " + context.InputParameters["Target"].GetType());
if (context.InputParameters["Target"] is EntityReference)
{
EntityReference ent1 = (EntityReference)context.InputParameters["Target"];
tracing.Trace("Entity 1 : " + ent1.LogicalName);
if (context.InputParameters.Contains("RelatedEntities") && context.InputParameters["RelatedEntities"] is EntityReferenceCollection)
{
tracing.Trace("RelatedEntities has been found.");
EntityReferenceCollection erc = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
bool hasSmartsheetItem = false;
EntityReference oppEntity = null;
EntityReference solEntity = null;
foreach (EntityReference er in erc)
{
tracing.Trace("Entity 2 : " + er.LogicalName);
if (ent1.LogicalName == "sec_solution")
{
oppEntity = er;
solEntity = ent1;
}
else if (er.LogicalName == "sec_solution")
{
oppEntity = ent1;
solEntity = er;
}
if (PluginHelper.IsSolutionForSmartsheetTechnology(solEntity, service))
{
tracing.Trace("Solution is a Smartsheet Technology Solution.");
hasSmartsheetItem = true;
break;
}
tracing.Trace("Solution is NOT a Smartsheet Technology Solution.");
}
if (hasSmartsheetItem)
{
PluginHelper.ChangeOpportunityValueForSmartsheet(service, tracing, oppEntity, true);
PluginHelper.PushSmartSheetAdminsInHiddenTeam(service, tracing, oppEntity);
}
}
}
}
else
{
tracing.Trace("Target could not be found.");
}
}
}
else { tracing.Trace("Relationship could not be found in the context."); }
}
}
}
catch (Exception)
{
throw new InvalidPluginExecutionException("An error occured while executing the Plugin. Please check the log file.");
}
Error occurs when calling PluginHelper.ChangeOpportunityValueForSmartsheet(service, tracing, oppEntity, true);
This is the code for this call :
try
{
Entity oppUpdate = new Entity(opp.LogicalName, opp.Id);
oppUpdate.Attributes.Add(new KeyValuePair<string, object>("sec_smartsheettechvisible", setVisible));
service.Update(oppUpdate);
}
catch (Exception ex)
{
tracing.Trace("Error occured while trying to update the Opportunity to show the smartsheet.");
tracing.Trace("Error message : " + ex.Message);
tracing.Trace("Error stacktrace : " + ex.StackTrace);
}
So far I can see, code seems ok.
I only try to update the opportunity if needed, with only the value that is needed. So no update on all other fields we have.
As said before, when running this in Test environment, all works fine.
But when we try this in production environment, Plugin errors telling me the opportunity with given ID could not be found.
Only difference I know between test and prod environment is that test is on 1 server and prod on 2 servers with load balancing.
I don't believe this to be important, but it is an On Premise installation.
Has anyone any idea why this is happening?