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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin Gives No Error But Does Not Work

(0) ShareShare
ReportReport
Posted on by

I have an entity called new_manufacturer and its display name is Manufacturer. I am trying to create a plugin where the input field called new_userinput on the main form of the new_manufacturer entity is checked to see if it contains the text "Test" when the user clicks the Save button. 

If it does contain the text "Test" then another field called new_viewuserinput on the form will be populated with the text "Test Successful".  Below is my code:

using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace Dynamics365CrmPlugin
{
    public class CrmPluginIPlugin:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName != "new_manufacturer")
                {
                    return;
                }

                IOrganizationServiceFactory serviceFactory = null;
                serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                
                try
                {
                     // Check UserInput field in a form in Manufacturer entity for the phrase Test.
                     if (entity.LogicalName == "new_manufacturer")
                        {
                            
                            if (entity.Attributes["new_userinput"].ToString().Trim() == "Test")
                            {
                                entity.Attributes["new_viewuserinput"] = "Test Successful";
                            }
                            
                        }
                }

                catch
                {

                }
            }
        }
    }
}


I registered the plugin and created the steps. I set Message to Update, set Primary Entity to new_manufacturer, chose Post Operation, set Execution Mode to Synchronous, and set Deployment to Server.

Please point out any thing you see that could be causing my code not to do what it was written to do, thanks.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    BadrinathB Profile Picture
    970 on at

    I can't see the update command for the entity. It's a post operation so the plugin will be fired after the data is saved in the DB.

                         // Check UserInput field in a form in Manufacturer entity for the phrase Test.
                         if (entity.LogicalName == "new_manufacturer")
                            {                            
                                if (entity.Attributes["new_userinput"].ToString().Trim() == "Test")
                                {
                                    entity.Attributes["new_viewuserinput"] = "Test Successful";

                                    service.Update(entity);
                                }                            
                            }

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    In addition to the response above, I would add code to your catch block to figure out what you want to do. In case you have an error this will help you diagnose.

    First, add the ITracing Service to your code so that you can trace your code whether success or failure and output results of Plugin execution to the Plugin Trace Log.

    Second, in your catch block, add the following (don't have to use FaultException<OrganizationServiceFault>, but have Exception in there):

    catch (FaultException<OrganizationServiceFault> ex)

    {

    // You can also add parameters or values to the tracing that will help you debug

      tracingService.Trace("Execute", "CrmPluginIPlugin", ex.Message, ex.Detail.ToString());

      throw new InvalidPluginExecutionException(String.Format("An error occurred in the {0} function of the {1} plug-in. The error is: {2}", "Execute", "CrmPluginIPlugin", ex));

    }

    Hope this helps.

  • Verified answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Oangodd,

    As you are updating the same entity, you don't need to call   service.Update(entity); method, you can just go to plugin registration tool and change the method to "PRE OPERATION" so it will automatically update your field values without making any extra call for update.

    You can also learn the difference between PRE and POST operation from below link,

    crmbook.powerobjects.com/.../plug-in-images-pre-vs-post

    if you find it helpful, please mark answer as verified.

    Best Regards,

    Shahbaaz

  • Verified answer
    Community Member Profile Picture
    on at

    Register your plugin on pre-operation. And before you get and set field. Just check if the field exist in the form, cause sometimes it's need to be checked or your script will not fired.

                         // Check UserInput field in a form in Manufacturer entity for the phrase Test.
                         if (entity.Attribute.Contains("new_userinput"))
                            {
                                try 
    { if (entity.Attributes["new_userinput"].ToString().Trim() == "Test") { entity.Attributes["new_viewuserinput"] = "Test Successful"; }
    } catch(Exception e)
    {
    throw new InvalidPluginExecutionException("my error : " + e);
    } }
  • Community Member Profile Picture
    on at

    Thank you all for your response. Each of you gave me a lot to think about. I've learned a lot from this exercise.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans