web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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.

  • Shahbaaz Ansari Profile Picture
    6,211 on at

    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

  • gdas Profile Picture
    50,091 Moderator on at

    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");
                        }
                    }
    
                }
    
            }

  • Community Member Profile Picture
    on at

    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.

  • Community Member Profile Picture
    on at

    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.

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    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

    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.

  • ashlega Profile Picture
    34,477 on at

    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..

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans