Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Error in Plugin on associate of entities

(0) ShareShare
ReportReport
Posted on by

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?

*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: Error in Plugin on associate of entities

    Hello Vincent,

    That's really great, i'm glad you were able to find the solution. That's what matters. ;)

    Cheers!

    Pedro Pisco

  • Community Member Profile Picture
    on at
    RE: Error in Plugin on associate of entities

    Pedro,

    You pointed me towards the right direction by asking if it worked when opportunity was updated.

    It actually didn't. So I started to look at why it did not work on update.

    Problem was actually that after updating the opportunity, some users where pushed in a team template in a next step:

    PluginHelper.PushSmartSheetAdminsInHiddenTeam(service, tracing, oppEntity)

    And this is actually where the real error occured. I was trying to push a user that was disabled. In test all where enabled. In production one was disabled.

    After fixing this, all worked fine.

    If CRM would have pointed me to that direction on associate instead of telling me that the opportunity could not be updated, because ID non existing, I would have found this issue earlier.

    So thanks to you to make me check for update. Actually didn't think about trying that... :-(

    I will accept your answer as you pointed me to the direction to solve this issue.

  • Community Member Profile Picture
    on at
    RE: Error in Plugin on associate of entities

    Pedro,

    No global variable.

    Only 1 organization in production (but org on 2 servers).

    Test is actually 2 organizations on 1 server (test and dev).

    As people are working with CRM now I cannot activate plugin and tracing.

    I will try to do this this evening when users are no longer using the system.

    I will get back to you when this has been done.

  • Community Member Profile Picture
    on at
    RE: Error in Plugin on associate of entities

    Goutam,

    Retrieving opportunity does not work.

    I already tried it and that was causing initial error.

    So I changed not to retrieve it first, but update it immediately (I first checked if changed, and only update if changed. But changed it to always update).

    In both cases, I get the error that says that "opportunity with id x-x-x does not exist".

  • Community Member Profile Picture
    on at
    RE: Error in Plugin on associate of entities

    Mohammed, Plugin is registered on post operation as mentionned before.

  • gdas Profile Picture
    50,091 Moderator on at
    RE: Error in Plugin on associate of entities

    Hi Vincent ,

    Could you please try to replace the update part of your code like below -

    Entity opportunity = new Entity("opportunity");                                        
    ColumnSet oppattributes = new ColumnSet(new string[] { "name", "ownerid", "sec_smartsheettechvisible" });  //Replace the necessary attributes                                        
    opportunity = service.Retrieve(opportunity.LogicalName, oppEntity.Id, oppattributes);
    opportunity["sec_smartsheettechvisible"] = setVisible; // Add attributes according to your requirement                                          
    service.Update(opportunity);

    Hope this helps.

  • ARIFNIIT Profile Picture
    1,391 on at
    RE: Error in Plugin on associate of entities

    Opportunity with ID x-x-x cannot be found error because of plugin registered on Pre-operation(Create message). check with Post-Operation.

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Error in Plugin on associate of entities

    Hi Vicent,

    That's really strange.

    Verify if you do not have any global variable on the plugin that could be cached.

    Is the error also happening on the association of records on an existing opportunity?

    Since it is a on-premise organization, have you collected Dynamics CRM traces? Would be helpful to understand if the opportunity ID on the traces would be the expected.

    And also, do you have more that one organization in production environment?

    Cheers!

    Pedro Pisco

  • Community Member Profile Picture
    on at
    RE: Error in Plugin on associate of entities

    Pedro,

    I used the profiler from the plugin registration tool.

    That's how I found out the actual error was "opportunity with id does not exist". I got the "there is no active transaction ..." error in the user interface. So I user the profiler to find the actual error.

    Yes there are other plugin running on create of opportunity. But not on associate.

    They also all are active in Test, and cause no issue there.

    The error occurs on line :

    service.Update(oppUpdate);

    I use the Id that was provided from the context, but I receive an error telling me opportunity with ID does not exist. Which seems strange to me, as I got the ID from the context.

  • Community Member Profile Picture
    on at
    RE: Error in Plugin on associate of entities

    Hello Vicent,

    Have you tried to debug the plugin on production environment using Plugin Profiler?

    Might be helpful to find what is happening.

    Also, do you have any other plugin or synchronous workflow running at that time?

    Cheers!

    Pedro Pisco

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans