1

We were trying to make the notes (with file upload) field mandatory by applying form validations.

The option we tried to use is client side javascripting. So far, so good...

The problem is that under Event Handlers tab:

Event Handlers tab

Notes entity is not listed amongst the Control items

Notes entity is not listed amongst the Control items

And hence obviously, there is no client side event for the Notes entity.

How to apply REQUIRED FIELD validation on Notes entity field?

Community
  • 1
  • 1
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219

2 Answers2

2

The validation that you want to do cannot be performed in the parent record. It would have to be validated in the note's form but we already discussed that the note entity is not fully customizable and we also have to keep in mind the social pane and it's particularities.

I think the best way to validate this it's to create a plugin in the Create event of the note (pre validation stage) to check if the note being created has the required fields completed.

Quick example (I haven't tested it):

public class ValidateNote : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) {

            Entity note = (Entity)context.InputParameters["Target"];

            // you can also use "subject" instead of "description"
            if (string.IsNullOrEmpty(note.GetAttributeValue<string>("description")) || string.IsNullOrEmpty(note.GetAttributeValue<string>("filename")))
            {
                throw new InvalidPluginExecutionException("Please add an attachment and description");
            }

        }

    }
}
Federico Jousset
  • 1,661
  • 14
  • 21
  • I would have suggested on the `Create` message for the plugin, but seen as you cannot associate entities without two valid Ids this cannot be preformed unless you execute the plugin as a post operation, thus allowing a record to be created without a note anyway. Therefore if the plugin runs against the `Update` as a `Pre-Validation` or `Pre-Operation` message you can block the update unless 1 or more annotations exist for that entity instance. The difference between our approaches is that yours will run for all notes and mine will execute for only the entities have the note requirements – Stefan William-Worrall Oct 14 '16 at 11:35
  • Actually, it doesn't matter if the note is related to other record or not because what he wants to achieve is the following: "Notes should always have some text and file attachments". That's why I think in create this plugin and prevent the creation of the note if it doesn't have the required fields. – Federico Jousset Oct 15 '16 at 11:12
1

The Notes relationship is a 1:N relationship. By default you can associate a minimum of Zero notes.

To apply Javascript validation you will require to a valid lookup field. As you cannot create field for a note then you can use a plugin to enforce this validation.

The plugin logic:

        var pluginContext = localContext.PluginExecutionContext;
        if (!pluginContext.InputParameters.Contains("Target") ||
            !(pluginContext.InputParameters["Target"] is Entity)) return;

        var target = pluginContext["Target"] as Entity;

        var annotationQuery = new QueryExpression
        {
            EntityName = "annotation",
            ColumnSet = new ColumnSet(true),
            Criteria =
            {
                Conditions =
                {
                    new ConditionExpression("objectid", ConditionOperator.Equal, target.Id)
                }
            }
        };

        var response = localContext.OrganizationService.RetrieveMultiple(annotationQuery);
        if (!response.Entities.Any()) 
            throw new InvalidPluginExecutionException("No Notes were found for the entity");
         //Further checks against content...

When the exception is thrown this interrupts the operation if the message for the plugin is Pre-Validation or Pre-Operation and the user will have to associate a note to the entity

  • Does it requires that the Notes entity needs to be editable? If yes, it doesn't seems to be [editable](https://msdn.microsoft.com/en-us/library/gg334739.aspx#customizable_entity_properies). And how that lookup field can be done? – Zameer Ansari Oct 14 '16 at 09:59
  • 1
    You cannot create a note lookup field in dynamics, it is constricted. Are you trying to enforce this on the creation of the entity record or the update ? – Stefan William-Worrall Oct 14 '16 at 10:07
  • The end result we need - Notes should always have some text and file attachments. – Zameer Ansari Oct 14 '16 at 10:10
  • I don't think this will work, as the Parent entity is created before the note. By the moment the plugin is executed, no notes are attached to the record yet, and the annotationQuery won't return any records. – crisfervil Oct 17 '16 at 14:14
  • @crisfervil that is why the plugin should execute on the update message or association message for the parent entity. – Stefan William-Worrall Oct 17 '16 at 14:18