分配查找值时的CRM错误属性值System.Int32的类型不正确

时间:2014-03-10 14:55:23

标签: dynamics-crm

在CRM 2011上,我正在编写一个C#,通过从.csv电子表格中读取来创建新帐户。 Account实体具有查找字段“Salesperson”,该字段是针对CRM“SystemUser”实体的查找值。 “Salesperson”字段是固定值,因此我只是将SystemUser Guid分配给它。但是,当尝试使用SystemUser Guid分配此查找值时,我收到错误“属性值System.Int32的类型不正确”。分配Guid值的正确方法是什么?

提前感谢您的帮助。

Uri organizationUri = new Uri("https://mycrm.org.com/crmtest/XRMServices/2011/Organization.svc");
            var cred = new ClientCredentials();
            OrganizationServiceProxy _serviceproxy = new OrganizationServiceProxy(organizationUri, null, cred, null);
            _service = (IOrganizationService)_serviceproxy;
Program p = new Program();

Entity account = new Entity("account");
account.Attributes["salesperson"] = "69ACA8F0-78B9-C211-B753-99E3B511A6F7";
Guid g2 =   p._service.Create(account);

2 个答案:

答案 0 :(得分:1)

我不知道你在哪里或如何获得Incorrect type of attribute value System.Int32我认为这是代码的子集,即Program p = new Program();正在做什么以及如何为_service分配salesperson 1}}?

根据CRM SDK,CRM 2011的帐户实体中没有名为xxxx_salespersonhttp://msdn.microsoft.com/en-us/library/gg328057.aspx)的字段。我假设这是您添加到实体的自定义字段,名称应该看起来像account.Attributes["salesperson"] = new EntityReference("systemuser", Guid.Parse("69ACA8F0-78B9-C211-B753-99E3B511A6F7"));。查看实体的自定义以查找字段的模式名称。

接下来,您无法将Guid直接分配给查找字段。您需要分配一个EntityReference,因此它应该是:

{{1}}

答案 1 :(得分:0)

        Guid guId = _organizationService.Create(foriegnEntity);


        //Create a collection of the entity ids that will be associated to the contact.

        EntityReferenceCollection relatedEntities = new EntityReferenceCollection();

        relatedEntities.Add(new EntityReference(foriegnEntity.LogicalName, guId));


        // Create an object that defines the relationship between entities.
    // RealationshipName is the name of the relationship found in lookup field customization screen for the entity  

       Relationship relationship = new Relationship(RelationshipName);


        //Associate 
        _organizationService.Associate(primaryEntityName, primaryEntityGuid, relationship, relatedEntities);