Plugins 在我的插件中连接到CRM服务器时出错

Plugins 在我的插件中连接到CRM服务器时出错,plugins,crm,microsoft-dynamics,Plugins,Crm,Microsoft Dynamics,请帮助我找出如何克服此错误: 我的代码 命名空间自动创建AccountandContactFromLead { 公共类CreateAccountAndContact:IPlugin { public void Execute(IServiceProvider服务提供程序) { //提取跟踪服务以用于去除沙盒插件的缺陷 ITracingService tracingService=(ITracingService)serviceProvider.GetService(类型(ITracingServ

请帮助我找出如何克服此错误:

我的代码
命名空间自动创建AccountandContactFromLead
{
公共类CreateAccountAndContact:IPlugin
{
public void Execute(IServiceProvider服务提供程序)
{
//提取跟踪服务以用于去除沙盒插件的缺陷
ITracingService tracingService=(ITracingService)serviceProvider.GetService(类型(ITracingService));
IPluginExecutionContext上下文=(IPluginExecutionContext)serviceProvider.GetService(类型为(IPluginExecutionContext));
//InputParamaters集合包含消息请求中传递的所有数据。
if(context.InputParameters.Contains(“目标”)&&context.InputParameters[“目标”]是实体)
{
//从输入参数获取目标实体
实体领导=(实体)上下文。输入参数[“目标”];
//如果实体的架构名称不是“lead”,则返回。如果是,则执行try block。
if(lead.LogicalName!=“lead”)
返回;
尝试
{
//检查插件是否在预操作阶段注册。
if(context.Stage==20)
{ 
//用于创建联系人的变量
var firstName=lead.Attributes[“firstName”];
var lastName=lead.Attributes[“lastName”];
var jobtitle=潜在客户属性[“jobtitle”];
var emailaddress1=lead.Attributes[“emailaddress1”];
var mobilephone=潜在客户属性[“mobilephone”];
var businessphone=lead.Attributes[“telephone1”];
//创建帐户的变量
var accountname=lead.Attributes[“companyname”];
var websiteurl=lead.Attributes[“websiteurl”];
var street1=潜在客户属性[“address1_line1”];
var street2=潜在客户属性[“address1_line2”];
var street3=潜在客户属性[“address1_line3];
var city=潜在客户属性[“地址1_city”];
var stateprovice=潜在客户属性[“地址1_STATEORPROVICE”];
var postalcode=潜在客户属性[“地址1_postalcode”];
var countryregion=潜在客户属性[“地址1_国家];
//创建联系人实体
实体联系人=新实体(“联系人”);
联系人[“firstname”]=firstname;
联系人[“lastname”]=lastname;
联系人[“职务”]=职务;
联系人[“emailaddress1”]=emailaddress1;
联系人[“电话1”]=商务电话;
联系人[“emailaddress1”]=emailaddress1;
联系人[“手机”]=手机;
//创建帐户实体
实体账户=新实体(“账户”);
账户[“名称”]=账户名称;
帐户[“网站URL”]=网站URL;
账户[“地址1_行1”]=街道1;
账户[“地址1_行2”]=街道2;
账户[“地址1_line3”]=街道3;
账户[“地址1_城市]]=城市;
账户[“地址1_州或省”]=州省;
账户[“地址1\u postalcode”]=postalcode;
账户[“地址1_国家]]=国家地区;
//获取orginxation服务参考。
IOOrganizationServiceFactory serviceFactory=(IOOrganizationServiceFactory)serviceProvider.GetService(类型为(IOOrganizationServiceFactory));
IOOrganizationService=serviceFactory.CreateOrganizationService(context.UserId);
//联系人和帐户
服务。创建(联系人);
服务。创建(帐户);
}
}
捕获(FaultException-ex)
{
抛出新的InvalidPluginExecutionException(“连接到CRM服务器时出错。”,例如);
}
捕获(例外情况除外)
{
tracingService.Trace(“FollowupPlugin:{0}”,例如ToString());
投掷;
}
}
}
}
}

我已在预操作阶段注册了上述插件。如果上面的代码能够创建联系人和帐户,我如何检索contactId和accountId以将它们链接在一起。i、 e帐户中有查找字段,用于链接相关联系人。Thank.service.Create将返回一个Guid,您可以将该Guid分配给变量,并在以后需要它们进一步更新任何其他记录时使用它。在您的情况下,它类似于Guid contactId=service.Create(contact);在创建account entity对象时使用此contactId填充该查找字段并创建帐户,以便在一次交易中完成所有操作。嗨,Renjith,谢谢你的建议。你知道为什么Catch block会在我的代码中抛出这个错误吗?我已经分别测试了代码create contact和account,发现错误发生在创建帐户时。有人知道为什么吗?那个错误实际上是你的自定义文本,任何来自“前”的东西吗?你们能像其他插件帐号创建一样完成删除过程吗,在创建时删除某些属性,试着在创建后注册插件,等等
An error occured while connecting to CRM server.
namespace AutoCreateAccountAndContactFromLead
{
public class CreateAccountAndContact : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        // Extract the tracing service for use in debuuging sandboxed plug-ins
        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        // The InputParamaters collection contains all the data passed in the message request.
        if(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parameter
            Entity lead = (Entity)context.InputParameters["Target"];

            // If the entity's schema name is not "lead", then return. If it is, execute try block.

            if (lead.LogicalName != "lead")
                return;

            try
            {
                // check plug-in is registerd in pre-operational stage.
                if (context.Stage == 20)
                { 
                    // vars for create contact
                    var firstName = lead.Attributes["firstname"];
                    var lastName = lead.Attributes["lastname"];
                    var jobtitle = lead.Attributes["jobtitle"];
                    var emailaddress1 = lead.Attributes["emailaddress1"];
                    var mobilephone = lead.Attributes["mobilephone"];
                    var businessphone = lead.Attributes["telephone1"];

                    // vars for create account
                    var accountname = lead.Attributes["companyname"];
                    var websiteurl = lead.Attributes["websiteurl"];
                    var street1 = lead.Attributes["address1_line1"];
                    var street2 = lead.Attributes["address1_line2"];
                    var street3 = lead.Attributes["address1_line3"];
                    var city = lead.Attributes["address1_city"];
                    var stateprovice = lead.Attributes["address1_stateorprovince"];
                    var postalcode = lead.Attributes["address1_postalcode"];
                    var countryregion = lead.Attributes["address1_country"];

                    // create a contact entity
                    Entity contact = new Entity("contact");
                    contact["firstname"] = firstName;
                    contact["lastname"] = lastName;
                    contact["jobtitle"] = jobtitle;
                    contact["emailaddress1"] = emailaddress1;
                    contact["telephone1"] = businessphone;
                    contact["emailaddress1"] = emailaddress1;
                    contact["mobilephone"] = mobilephone;

                    // create an account entity
                    Entity account = new Entity("account");
                    account["name"] = accountname;
                    account["websiteurl"] = websiteurl;
                    account["address1_line1"] = street1;
                    account["address1_line2"] = street2;
                    account["address1_line3"] = street3;
                    account["address1_city"] = city;
                    account["address1_stateorprovince"] = stateprovice;
                    account["address1_postalcode"] = postalcode;
                    account["address1_country"] = countryregion;

                    // Obtain the orginxation service reference.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    // contact and account
                    service.Create(contact);
                    service.Create(account);
                }

            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException("An error occured while connecting to CRM server.", ex);
            }
            catch (Exception ex)
            {
                tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                throw;
            }
            }
        }
    }
}