如何在 Notes 实体字段上应用 REQUIRED FIELD 验证?

How to apply REQUIRED FIELD validation on Notes entity field?

我们试图通过应用表单验证使 字段成为必填项。

我们尝试使用的选项是 client side javascripting。到目前为止,还不错...

问题是在“事件处理程序”选项卡下:

Notes 实体未在控制项中列出

因此很明显,Notes 实体没有客户端事件。

如何对 Notes 实体字段应用必填字段验证?

注释关系是 1:N 关系。默认情况下,您可以关联最少的零个音符。

要应用 Javascript 验证,您需要一个有效的查找字段。由于您无法为注释创建字段,因此您可以使用插件来强制执行此验证。

插件逻辑:

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

如果插件的消息是 Pre-ValidationPre-Operation 并且用户必须将注释关联到实体

,则抛出异常会中断操作

您想要执行的验证无法在父记录中执行。它必须以笔记的形式进行验证,但我们已经讨论过笔记实体不是完全可定制的,我们还必须记住社交窗格及其特殊性。

我认为验证这一点的最佳方法是在笔记的创建事件(预验证阶段)中创建一个插件,以检查正在创建的笔记是否已完成必填字段。

快速示例(我还没有测试过):

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

        }

    }
}