【问题标题】:How to set lookup field with id of a user record just created?如何使用刚刚创建的用户记录的 id 设置查找字段?
【发布时间】:2018-07-28 19:04:51
【问题描述】:

我收到error - no service provision,请有经验的人帮助我。

    var clinic = new new_clinic();
            clinic.new_name = "Medinet";

            Account trust = new Account();
            trust.Name = "bIRMINGHAM CITY hOSPITAL";
            Guid trustID = (Guid)Service().Create(trust);

            CrmEntityReference clinic_trust = new CrmEntityReference(trust.LogicalName, trustID);
            clinic.new_Trust = clinic_trust;
            clinic.new_ClinicDate = DateTime.Now;

            new_hospital hospital = new new_hospital();
            hospital.new_name = "Geek-guru";
            Service().Create(hospital);
            Guid hospitalID = (Guid)hospital.Id;
            clinic.new_HostingHospital = new CrmEntityReference(hospital.LogicalName, hospitalID);

            clinic.new_ClinicDate = new DateTime(2018, 07, 28);

            new_serviceprovision ser_contract = new new_serviceprovision();
            ser_contract.new_Trust = clinic_trust;
            ser_contract.new_Specialty = new OptionSetValue(100000018);
            Service().Create(ser_contract);
            Guid ser_con_id = (Guid)ser_contract.Id;
            clinic.new_ServiceContract = new CrmEntityReference(ser_contract.LogicalName, ser_con_id);

            // Account account =  (Account)GetOrgService().Retrieve(trust.LogicalName, trustID, new Microsoft.Xrm.Sdk.Query.ColumnSet("name"));

            //retreive the default business unit needed to create the user
            QueryExpression businessUnitQuery = new QueryExpression
            {
                EntityName = BusinessUnit.EntityLogicalName,
                ColumnSet = new ColumnSet("businessunitid"),
                Criteria = { Conditions = { new ConditionExpression("parentbusinessunitid", ConditionOperator.Null) } }
            };

            BusinessUnit businessUnit = Service().RetrieveMultiple(businessUnitQuery).Entities[0].ToEntity<BusinessUnit>();



            //creating a user
            SystemUser systemUser = new SystemUser();
            systemUser.DomainName = "" + "Chika";
            systemUser.FirstName = "Olabajo";
            systemUser.LastName = "Boss";
            systemUser.InternalEMailAddress = "onyebuchi@gmail.com";
            systemUser.BusinessUnitId = new EntityReference(BusinessUnit.EntityLogicalName, businessUnit.Id);
            Guid systemID = (Guid)Service().Create(systemUser);

           // systemUser = (SystemUser)Service().Retrieve(systemUser.LogicalName, systemID, new Microsoft.Xrm.Sdk.Query.ColumnSet("fullname"));

            //this field is a lookup field to User. Assigned to the user created above
            clinic.new_ClinicCoordinator = new CrmEntityReference(systemUser.LogicalName, systemID);

            clinic.new_BookedBy = new OptionSetValue(100000001);
            clinic.new_Type1 = new OptionSetValue(100000008);
            clinic.new_Type2 = new OptionSetValue(100000001);
            clinic.new_NumberofConsultants = 5;
            clinic.new_NumberofNursesSuppliedByTrust = 6;


            Service().Create(clinic);//getting error here
            MessageBox.Show("Sucessfully added a clinic record");

我已经在这些方面工作了好几个星期了,我希望能得到一点帮助。 我现在遇到“没有 serviceprivison 问题”,现在已经创建了一个用户,但仍然出现错误。

【问题讨论】:

  • 这段代码在哪里运行,控制台应用程序或插件?把try catch?你调试了吗?您标记了 2011 和在线 - 是否在线,那么您无法创建这样的用户。
  • windows窗体运行,使用早期绑定类,动态CRM上线。
  • @ArunVinoth hi Arun,感谢您的响应,但它没有被创建,因为 id 只是零。
  • @ArunVinoth 看到这里,它是在 sdk 中完成的,我做了同样的事情,但得到一个错误。 msdn.microsoft.com/en-us/library/jj602984.aspx
  • 这是本地非在线的代码示例;另请注意,您没有在请求中传递域名和业务单位值,这是强制性的。

标签: c# dynamics-crm dynamics-crm-2011 entity-relationship


【解决方案1】:
  1. 一步一步来。您有 3 件事接踵而至,例如创建用户、检索 FullName 列,然后更新 clinic.new_ClinicCoordinator 字段。注释掉这两个步骤并验证用户是否正在创建,如果创建成功然后检索下一个(这不是必需的,因为您已经有了更新所需的 systemID)

  2. 使用 try catch 更好地处理代码中的异常

编辑:无法在代码中创建 CRM 在线交互式用户。 Read more

更新:
合并以下行,因为 ser_contract 对象不会使用创建的 Guid 更新回来。对hospitalID 也这样做。

Service().Create(ser_contract);
Guid ser_con_id = (Guid)ser_contract.Id;

这应该可以工作:

Guid ser_con_id = Service().Create(ser_contract);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 2017-09-04
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多