0

我使用下面的代码。一切正常,直到我想在“var workReportItem”最后一行的 workReportItem 上设置客户(帐户)的引用。代码是:

private static void AddWRItoServiceActivity(IOrganizationService service, Guid id)
    {
        using (var crm = new XrmServiceContext(service))
        {

            var serviceactivity = crm.ServiceAppointmentSet.Where(c => c.Id == id).First();
            var serviceitem = crm.brd_serviceitemSet.Where( c => c.brd_RegardingServiceId.Id == serviceactivity.ServiceId.Id);

            foreach (var S in serviceitem)
            {
                var workReportItem = new brd_workreportitem
                   {
                       brd_name = S.brd_name,
                       brd_serviceappointment_brd_workreportitem = serviceactivity,
                       brd_brd_serviceitem_brd_workreportitem_ServiceItem = S,
                       brd_brd_servicereportitem_brd_workreportitem_ServiceReportItem = S.brd_brd_servicereportitem_brd_serviceitem_ServiceReportItem,
                       brd_Customer = serviceactivity.Customers.First().ToEntityReference(),
                    };

                // Setting the optionset value "type"
                OptionSetValue myOptionSet = new OptionSetValue();
                myOptionSet.Value = S.brd_brd_servicereportitem_brd_serviceitem_ServiceReportItem.brd_Type.Value;
                workReportItem.Attributes["brd_type"] = myOptionSet;

                crm.AddObject(workReportItem);
                crm.SaveChanges();
            }
        }
    }

错误是:值不能为空。参数名称:来源。如果有人可以提供帮助,我们将不胜感激。

4

2 回答 2

0

看起来像客户为空。您必须对客户集执行另一个查询,以便为服务活动拉回客户。

于 2013-01-07T21:29:37.027 回答
0

我使用以下代码在派对列表上设置参考:

ActivityParty activityParty = new ActivityParty { PartyId = new EntityReference(MissionAccount.LogicalName, MissionAccount.Id) };
var customer = new List<ActivityParty>();
customer.Add(activityParty);
serviceactivity.Customers = customer;
于 2013-01-14T10:36:14.503 回答