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)

Validation plugin (checking for a null value in an attribute) works fine during Create message/step but not during Update

(0) ShareShare
ReportReport
Posted on by
When I create an entity the plugin does the expected validation of checking if there is a null value in an attribute.

But if I take an existing entity and delete the contents of that field and click on save,
I expected that plugin to kick in and throw the error message. but it won't.

Please see the screen shots for "Update" and "Create" steps
namespace ClassLibrary1 { public class TrainingConfig_Valiation_Plugin :IPlugin { public void Execute(IServiceProvider serviceProvider) { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); Entity entity = (Entity)context.InputParameters["Target"]; /* if (entity.Contains("ita_notestallowedmessage")) { if (String.IsNullOrEmpty(entity.GetAttributeValue<string>("ita_notestallowedmessage"))) { throw new InvalidPluginExecutionException("You cannot have a null value in the notesallowed field"); } } */ if (entity.Contains("ita_notestallowedmessage")) { if (entity.GetAttributeValue<string>("ita_notestallowedmessage") == null) { throw new InvalidPluginExecutionException("You Cannot have null in notesallowed field"); } } else { throw new InvalidPluginExecutionException("You Cannot have null in notesallowed field"); } } } }


Update_2D00_Step.png

Insert_2D00_Step.png

*This post is locked for comments

I have the same question (0)
  • Ben Thompson Profile Picture
    6,350 on at

    I have to ask why are you doing the above - why haven't you just made the ita_notestallowedmessage a mandatory field?

    As for why your plugin isn't working on update -that's because it's only triggered if ita_notestallowedmessage  has been updated, otherwise it won't be triggered. 

  • Community Member Profile Picture
    on at

    Ben,

    I take an existing entity (record) and update the data in ita_notestallowedmessage  field (like deleting content of that field). after deleting the content, when save is clicked this plugin should kick and throw error message saying the content of ita_notestallowedmessage   is null.

    But it won't trigger at all. Even though the content of ita_notestallowedmessage   is empty, the entity gets saved.

  • JasonMcneil Profile Picture
    215 on at

    Do you know if the plugin is executing at all, to at least see if it's something in your code versus the plugin registration? Try just sticking a trace message at the very start of the plugin and check the plugin trace log.

  • Community Member Profile Picture
    on at

    BTW, I am new to MSD CRM. I am practicing developing plugin's. I am use we can make that field mandatory or write javascript

  • Community Member Profile Picture
    on at

    The plugin gets triggered fine (Business process error window popsup) during "Create" step if I don't put anything into ita_notestallowedmessage field (meaning when it is null).

    But when I take an existing entity and delete the content of ita_notestallowedmessage field, and hit save button, I expect the plugin to trigger again. But it saves instead.

    Where are trace logs available for "online" CRM ? I have a trial membership only. And how to put trace messages in the code?

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    I guess this is because when you are deleting the value, CRm is treating that value as empty (i.e. not null). So the below check doesn't return true-

    if (entity.GetAttributeValue<string>("ita_notestallowedmessage") == null)

    If you put an additional else for this if and throw sum message then you will see the message.

    Basically, if you replace the above if condition with below, you should get a message-

    if (string.IsNullOrEmpty(entity.GetAttributeValue<string>("ita_notestallowedmessage")))

    Hope this helps.

  • Suggested answer
    Community Member Profile Picture
    on at

    Ravi

    if (string.IsNullOrEmpty(entity.GetAttributeValue<string>("ita_notestallowedmessage"))) 

    works.

    Thank you so much

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