0

除特定用户外,不应允许所有用户编辑或更新不满足特定条件的报价详情,但如果他们愿意,他们必须能够修改报价。

问题是,修改报价(即用户单击活动表单记录中的“修改”按钮)会触发报价详细信息的更新,我无法弄清楚如何识别发生了什么。

我目前的尝试是基于一个插件,其代码如下所示:

public class PreQuoteProductUpdate : Plugin
{
// I work with CRM Developer Tools to build plugins 
// This goes in Update Message, Pre-Operation, Server Only, pre-image called "preImage"

protected void ExecutePreQuoteProductUpdate(LocalPluginContext localContext)
{
    if (localContext == null)
    {
        throw new ArgumentNullException("localContext");
    }

    IPluginExecutionContext context = localContext.PluginExecutionContext;
    IOrganizationService srv = localContext.OrganizationService;

    Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;

    try
    {
        PluginBody(context, srv, preImageEntity);
    }
    catch (Exception ex)
    {
        throw new InvalidPluginExecutionException("Quote Details Pre-Update", ex);
    }
}

protected void PluginBody(IPluginExecutionContext context, IOrganizationService srv, Entity preImage)
{
    if(IsRevising()) return;

    CheckSomeCondition(context, srv);

    if (preImage.Attributes.ContainsKey("ica_daconfigurazione") && preImage.GetAttributeValue<bool>("ica_daconfigurazione"))
    {
        CheckUser(context, srv);
    }
}

protected void IsRevising()
{
    // I have no clue about the logic to put here: see below.
}

protected void CheckSomeCondition(IPluginExecutionContext context, IOrganizationService srv)
{
    var entity = (Entity)context.InputParameters["Target"];
    // if some fields of entity contain some specific data, throw
    // this always happens
}

protected void CheckUser(IPluginExecutionContext context, IOrganizationService srv)
{
    //allowedUser is read from a configuration entity
    var allowedUser = new Guid();
    if (context.InitiatingUserId.Equals(serviceUser.Id) == false)
        throw new InvalidPluginExecutionException("Can't edit quote details");
}

}

我知道(在Quote插件中)我可以通过检查来知道正在进行修订,插件ParentContext中是否有类似的可用QuoteDetail?我试了一下,但我得到的都是NullReferenceException扔给我的。

我应该期望有State/Status可以检查吗?

有关我可能忽略的更多信息,请询问。

4

1 回答 1

1

注册Pre Create消息(第 20 阶段)QuoteDetail并过滤父上下文不是 for Quote。如果是,则返回(实际上什么都不做)。这同样适用UpdateQuoteDetail. 两条消息都在消息
的上下文中运行。ReviseQuoteQuote

var parentContext = context.ParentContext;

// While there is a parent context...
while (parentContext != null) {
    // When parent context is for "quote", return;
    if (parentContext.PrimaryEntityName == "quote")
    {
        return;
    }
    // Assign parent's parent context to loop parent context.
    parentContext = parentContext.ParentContext;
}
于 2013-09-06T11:17:21.937 回答