【问题标题】:Add link to the newly created entity record using Notify.js - CRM使用 Notify.js - CRM 添加指向新创建的实体记录的链接
【发布时间】:2016-10-11 18:33:49
【问题描述】:

我有一个自定义工作流活动,它从我的自定义工作流活动中创造了新的机会。 我将该活动作为行动步骤。我正在使用动作,因为它有一个输出。我需要获得我创造的机会的 ID。 我使用 Process.js 从 JS Web 资源调用该操作。之后,我使用 Notify.js 通知用户机会已创建。在那个通知上,我需要有一个链接到新创建的机会的按钮。

以下是与输出参数相关的部分 C# 代码。只是要注意用于创造机会和执行更多任务的代码部分工作正常:

//define variable
[Output("Opportunity")]
[ReferenceTarget("opportunity")]
public OutArgument<EntityReference> NewOpportunity { get; set; }

//create opportunity and entity reference(i am not sure do ineed entity reference, or something else for that link)
Guid opportunityId = service.Create(opportunity);
EntityReference OppId = new EntityReference(opportunity.LogicalName, opportunityId);

//assign value to the output variable
NewOpportunity.Set(Econtext, OppId);

这是我在动作定义中创建动作参数的方式:

这里是调用action的JS代码:

function toOpportunity(){

Process.callAction("ad_opportunity",
    [{
        key: "Target",
        type: Process.Type.EntityReference,
        value: { id: Xrm.Page.data.entity.getId(), entityType: "ad_productsamplerequest" }
    }],
    function (param) {
        //Success            
         Notify.add("New Opportunity was created:", "INFO", "opportunity",
    [{
        type: "button",
        text: "Go to opportunity",
        callback: function () {

        }
    }]);
    },
    function () {
        // Error
        alert("Opportunity was not created");
    }
);

只是说,它有效,行动被调用,机会被创造,然后有通知。只是不知道如何使用操作输出参数来设置机会的链接。

【问题讨论】:

    标签: javascript c# dynamics-crm crm


    【解决方案1】:

    看起来您正在尝试处理 CodeActivity 类中的操作。这行不通。 OutArgument 属性只能在工作流中访问,它们不能返回给调用进程。

    相反,使用所需的输入和输出参数创建操作。然后创建一个插件并在此操作上注册一个同步发布更新步骤。插件类实现必须将机会 ID 添加到 OutputParameters 集合中,如下所示:

    public void Execute(IServiceProvider serviceProvider)
    {
        var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        var service = factory.CreateOrganizationService(context.UserId);
    
        Entity opportunity;
        // TODO: add code here to build the opportunity.
    
        Guid opportunityId = service.Create(opportunity);
        context.OutputParameters.Add("NewOpportunity", new EntityReference("opportunity", opportunityId));
    }
    

    另请参阅MSDN 上的此说明。

    【讨论】:

    • 是否有可能在代码活动中做到这一点?我必须从动作中调用这个代码活动,因为当我调用这个条件时我有非常具体的条件。我正在使用动作,因为它具有输出参数,并且我教过使用该参数获取 id。
    • 是否有可能只为 ID 提供新插件。要在创建 Opportunity 时注册 post create 步骤,并使用它来获取已创建 Opportunity 的 id?
    • @MilosNikolic:动作和代码活动是两个不同的组件。我扩展了我的答案以提供更多解释。这有帮助吗?
    【解决方案2】:

    Henk 很伤心,无法传递实体引用,所以我在 CodeActivity 中将 Guid 作为字符串传递:

     [Output("Opportunity")]
     public OutArgument<string> NewOpportunity { get; set; }
    

    ...

    Guid opportunityId = service.Create(opportunity);
     string guidSend = opportunityId.ToString();
     NewOpportunity.Set(Econtext, guidSend);
    

    之后我在 JS 中使用了该输出:

    var OGuid = param["NewOpportunity"];
            Opport = OGuid.replace(/ /g,'');
    

    并像这样设置与新机会的关系:

    Xrm.Utility.openEntityForm("opportunity",""+Opport);
    

    JavaScript 的第一部分在 Action 调用中,但不在通知中,最后一行在通知中,因此它们位于代码的不同部分。因此,我将Opport 定义为全局变量,然后我可以在一个函数中设置值,并在通知中从另一个函数调用它。

    还有另一个选项,没有C# 部分。可以在通知回调函数中创建 fetch 并返回最后创建的机会 id。这更容易,但不是更好:

         var contactFetchXML = "<fetch mapping='logical' version='1.0' distinct='true' count='1' >"+
                                  "<entity name='opportunity' >"+
                                    "<attribute name='opportunityid' />"+
                                    "<order attribute='createdon' descending='true' />"+
                                  "</entity>"+
                              "</fetch>";
    
    
        var Guid = XrmServiceToolkit.Soap.Fetch(contactFetchXML);
    
        Xrm.Utility.openEntityForm("opportunity",""+Guid[0].id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多