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

本文关键字:Int32 System 类型 不正确 属性 出错 分配 查找 CRM | 更新日期: 2023-09-27 17:49:31

在CRM 2011上,我正在编写c#来通过读取。csv电子表格来创建新帐户。Account实体有一个查找字段"销售员",它是针对CRM"SystemUser"实体的查找值。"销售人员"字段是一个固定值,所以我只是将SystemUser Guid分配给它。然而,我得到了一个错误"不正确的类型属性值系统。Int32",当尝试使用SystemUser Guid分配此查找值时。分配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);

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

我不知道你在哪里或如何获得Incorrect type of attribute value System.Int32,我有一个想法,这是代码的子集,即,这是Program p = new Program();做的,它是如何被分配一个_service的?

根据CRM SDK, CRM 2011的帐户实体上没有字段称为salesperson (http://msdn.microsoft.com/en-us/library/gg328057.aspx)。我假设这是一个自定义字段,您添加到实体和名称应该看起来像xxxx_salesperson。查看实体的自定义以查找字段的模式名称。

接下来,您不能将Guid直接分配给查找字段。你需要分配一个EntityReference,所以它应该是:

account.Attributes["salesperson"] = new EntityReference("systemuser", Guid.Parse("69ACA8F0-78B9-C211-B753-99E3B511A6F7"));

        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);
相关文章: