Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin to prevent field from beeing changed

(0) ShareShare
ReportReport
Posted on by

Hi,

I wrote a pre-operation plugin where I'd like to prevent any changes to contact's jobtitle.

The Plugin itself is pretty simple:

if (targetEntity.Attributes.ContainsKey("jobtitle")) 
    targetEntity.Attributes.Remove("jobtitle");

and

protected static Entity GetCurrentEntity(IServiceProvider serviceProvider)
{
    var context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext));
    return context.InputParameters.GetEntityOf("Target");
}

For testing, I do some JS in account form and force jobtitle to be sent to the plugin:

Xrm.Page.getAttribute('jobtitle').setSubmitMode("always");

As a result, in audit log of a modified contact, jobtitle get's updated anyway.

I'd expected to not update jobtitle (as it is "foo" all the time), but audit log shows:

field old value new value
jobtitle

foo

fax 1

2

=> looks like changing targetEntity somehow modifies the "old value"??

Can anyone guide me on how to prevent specific fields from beeing updated? In real code I want to avoid updates of fields, which have not changed (but are in the attribute collection due to submit-mode "always" or for any other reason) which should be possible by comparing pre- and target image.

*This post is locked for comments

  • ashlega Profile Picture
    34,477 on at
    RE: Plugin to prevent field from beeing changed

    If it works in pre-validation, maybe just keep using that.

    As for the pre-operation, I don't know what's the execution order on the audit plugins - would need to dig a bit more.. you can probably look it up using web api.. Maybe try setting execution order on your steps to a negative number actually..

  • Community Member Profile Picture
    on at
    RE: Plugin to prevent field from beeing changed

    Hi Alex,

    thanks for your reply. I changed execution order from 999 to 1 without success. Changing to pre-validation looks good. However, I don't understand why pre-operation does not work as expected.

  • Verified answer
    ashlega Profile Picture
    34,477 on at
    RE: Plugin to prevent field from beeing changed

    Hi,

     auditing is relying on the plugins as well(you don't see those steps in the plugin registration utility, but you can use advanced find or webapi to find them), so it might be just a matter of having all those plugins started in the right order. Did you try moving yours into pre-validation?

  • Community Member Profile Picture
    on at
    RE: Plugin to prevent field from beeing changed

    In real code I want to avoid updates of fields, which have not changed (but are in the attribute collection due to submit-mode "always" or for any other reason) which should be possible by comparing pre- and target image.

    debugger was already attached. I can confirm the code was executed. If the plugin is disabled, audit log shows "foo" for both - old and new value.

  • Community Member Profile Picture
    on at
    RE: Plugin to prevent field from beeing changed

    That's quite close to what I'd like to achieve. Instead of raising the Exception, I'd like to remove "jobtitle" from the attributes-to-update-list..

    In real code I want to avoid updates of fields, which have not changed (but are in the attribute collection due to submit-mode "always" or for any other reason) which should be possible by comparing pre- and target image.

  • gdas Profile Picture
    50,089 Moderator on at
    RE: Plugin to prevent field from beeing changed

    Hello,

    First of all you can restrict this by many way using java-script , field security profile. However if you want then you can try below way .I 

    believe you have registered your plugin in update event.  So in update event you will get the pre-image of the record ,I mean you will get the jobtitle of current record before update. Here is the sample code , hope this helps

        public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider.
                // Obtain the organization service reference.
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.
                    Entity entity = (Entity)context.InputParameters["Target"];
                    // Verify that the target entity represents an contact.
                    // If not, this plug-in was not registered correctly.
                    if (entity.LogicalName == "contact")
                    {
                        Entity _preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains("PreImage")) ? context.PreEntityImages["PreImage"] : null;
                        //Get Old JobTitle
                        string _OldJobTitle = "";
                        if (_preImageEntity.Attributes.Contains("jobtitle"))
                        {
                            if (_preImageEntity.GetAttributeValue<string>("jobtitle") != "" && _preImageEntity.GetAttributeValue<string>("jobtitle") != null)
                            {
                                _OldJobTitle = _postImageEntity.GetAttributeValue<string>("jobtitle");
    
                            }
                        }
    
                        //Get New Jobtitle
                        string _OldJobTitle = "";
                        if (entity.Attributes.Contains("jobtitle"))
                        {
                            if (entity.GetAttributeValue<string>("jobtitle") != "" && entity.GetAttributeValue<string>("jobtitle") != null)
                            {
                                _OldJobTitle = entity.GetAttributeValue<string>("jobtitle");
    
                            }
                        }
    
                        if (_OldJobTitle != _OldJobTitle)
                        {
                            throw new InvalidPluginExecutionException("You can not modify job title");
                        }
                    }
    
                }
    
            }

  • Shahbaaz Ansari Profile Picture
    6,205 on at
    RE: Plugin to prevent field from beeing changed

    Hi nor-man,

    If it is always the same, than in your plugin you can set that field value like

    targetEntity.["jobtitle"] = "foo";

    inside of setting from any where else.

    Thanks,

    Shahbaaz

  • Aric Levin Profile Picture
    30,188 Moderator on at
    RE: Plugin to prevent field from beeing changed

    Why don't you just make the field as a secured field, with read access, and no write access. That would prevent this.

    Overall since you are using CRM 2015 (seems like On-Premise), why not install remote debugger on the server and see if code is executed as supposed to (attached plugin source to w3wp process on remote server?

    You can also set the value of the jobtitle in pre-operation to what it's supposed to stay like. Not sure why you remove not working.

    Hope this helps.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,041 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,856 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans