Quantcast
Channel: CRM Development Forum
Viewing all articles
Browse latest Browse all 888

Error on setting reference CRM 2011 plugin

$
0
0

Im using the code bellow. Everything is OK until I want to set the reference of the customer (account), on the workReportItem in the last line of "var workReportItem". The code is:

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();
            }
        }
    }

The error is: Value can not be null. Parameter name: source. It is appreciated if anyone could help.


Viewing all articles
Browse latest Browse all 888

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>