【问题标题】:Plugin that Concatenates Contact and Account Name on create of an Account record在创建帐户记录时连接联系人和帐户名称的插件
【发布时间】:2015-12-22 17:41:36
【问题描述】:

我不熟悉插件和 C#,我需要创建一个插件,在创建客户记录时连接联系人和客户名称的文本值。例如:ABC 公司“- John Smith”我正在使用 Visual Studio 和 CRM 2016 Online(试用版)。这是我目前所拥有的:

使用 Microsoft.Xrm.Sdk; 使用系统;

命名空间 CRMPluginProject4 { 公共类 ConcatPlugin : IPlugin { #region 安全/不安全配置设置 私有字符串 _secureConfig = null; 私有字符串_unsecureConfig = null;

    public ConcatPlugin(string unsecureConfig, string secureConfig)
    {
        _secureConfig = secureConfig;
        _unsecureConfig = unsecureConfig;
    }
    #endregion
    public void Execute(IServiceProvider serviceProvider)
    {
        ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = factory.CreateOrganizationService(context.UserId);

        try
        {
            Entity entity = (Entity)context.InputParameters["Target"];

            //TODO: Do stuff




      }
        catch (Exception e)
        {
            throw new InvalidPluginExecutionException(e.Message);
        }
    }
}

}

【问题讨论】:

    标签: c# visual-studio plugins crm


    【解决方案1】:

    PJM,您可以使用 java 脚本而不是编写插件来执行此操作。

    如果你想使用插件,请使用如下代码,

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                        {
                            //Get the target property and check whether its a contact.
                            Entity target = (Entity)context.InputParameters["Target"];
    
                            string primaryContactId = target.Attributes.Contains("primarycontactid") ? ((EntityReference)target.Attributes["primarycontactid"]).Name : string.Empty;
                            string accountName = target.Attributes.Contains("name") ? target.Attributes["name"].ToString() : string.Empty;
                            target["name"] = accountName + "-" + primaryContactId;
    
                        }
    

    请确保这不是在创建和更新帐户的预操作中。希望这对您有所帮助。如果您需要更多帮助,请告诉我们。

    【讨论】:

    • 太棒了!谢谢!此外,当我尝试在 Visual Studio 中运行插件时,我收到一条错误消息:“无法直接启动具有类库输出类型的项目。为了调试此项目,请在此解决方案中添加一个可执行项目,其中引用库项目。将可执行项目设置为启动项目”是什么意思? @Renjith
    • 类库通常用作对其他应用程序的可重用引用,因此它不能独立工作。您必须将库引用到另一个项目,如控制台、网站、窗口等。但同样可以在 CRM 中注册为插件并标记到任何事件并可以使用。确保您将 IPlugin 继承到该类,以便可以在 CRM 中访问和使用它。假设你已经这样做了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    相关资源
    最近更新 更多