使用 MSCRM 服务保存数据

本文关键字:数据 保存 服务 MSCRM 使用 | 更新日期: 2023-09-27 18:33:07

我正在做一个打算使用crmservice的项目,以便在现有数据库中保存数据。该数据库是由mscrm创建的,我必须创建一个使用crmservice的应用程序来将相关信息存储在数据库中。

到目前为止,我正在从视图模型接收数据并尝试使用 crmservice 保存。

保存方法如下:

private void Initiate()
        {
            var serverConnect = new CrmConnector();
            CrmConnector.pubpassword = ConfigurationManager.AppSettings["crmpassword"].ToString();
            CrmConnector.pubuserName = ConfigurationManager.AppSettings["username"].ToString();
            CrmConnector.pubdomain = ConfigurationManager.AppSettings["domain"].ToString();
            CrmConnector.serveraddr = ConfigurationManager.AppSettings["server"].ToString();
            serverConfig = serverConnect.GetServerConfiguration();
        }
        public void SaveTimesheetLine(TimesheetViewModel timesheetLineVm)
        {
                string payrollId = Convert.ToString(Session["payroll"]);
                Initiate();
                using (_serviceProxy = CrmConnector.GetOrganizationProxy(serverConfig))
                {
                    // This statement is required to enable early-bound type support.
                    //_serviceProxy.EnableProxyTypes();
                    _service = (IOrganizationService) _serviceProxy;
                    var timesheetLineEntity = new Entity("new_timesheetlineitem");
                    timesheetLineEntity["new_billtoid"] = timesheetLineVm.TimesheetLineViewModels.BillToId;
                    timesheetLineEntity["new_timesheettimesheetlineitemid"] = timesheetLineVm.TimesheetId ;
                    timesheetLineEntity["new_slatimesheetlineitemid"] = timesheetLineVm.TimesheetLineViewModels.SlaId;
                    timesheetLineEntity["new_billratetimesheetlineitemid"] = timesheetLineVm.TimesheetLineViewModels.BillRateId;
                    timesheetLineEntity["new_stream3timesheetlineitemid"] = timesheetLineVm.TimesheetLineViewModels.Stream3Id;
                    timesheetLineEntity["new_contracttypetimesheetlineitemid"] = timesheetLineVm.TimesheetLineViewModels.ContractTypeId;
                    timesheetLineEntity["new_firstname"] = timesheetLineVm.TimesheetLineViewModels.EmployeeFirstName;
                    timesheetLineEntity["new_lastname"] = timesheetLineVm.TimesheetLineViewModels.EmployeeLastName;
                    timesheetLineEntity["new_accounttimesheetlineitemid"] = timesheetLineVm.TimesheetLineViewModels.EmployerId;
                    timesheetLineEntity["new_payrollreference"] = payrollId;
                    timesheetLineEntity["new_timesheetdate"] = timesheetLineVm.TimesheetDate;
                    timesheetLineEntity["new_candidatetimesheetlineitemid"] = timesheetLineVm.TimesheetLineViewModels.CandidateId ;
                    // taken from user input 
                    timesheetLineEntity["new_startdatetime"] = timesheetLineVm.TimesheetLineViewModels.StartDate;
                    timesheetLineEntity["new_enddatetime"] = timesheetLineVm.TimesheetLineViewModels.EndDate;
                    timesheetLineEntity["new_paytypetimesheetlineitemidname"] = Convert.ToString(timesheetLineVm.TimesheetLineViewModels.PayType);
                    //timesheetLineEntity["new_lunchtime"] = 2;
                    timesheetLineEntity["new_submittedhours"] = timesheetLineVm.TimesheetLineViewModels.SubmittedHours;
                    _timesheetlineId = _service.Create(timesheetLineEntity);
                }
        }

我收到的错误:

[FaultException'1: System.NullReferenceException: Microsoft Dynamics CRM 遇到错误。管理员参考编号或 支持: #E28869A7]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg( +11080899
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 类型( +336
Microsoft.Xrm.Sdk.IOrganizationService.Create(Entity entity( +0
Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.CreateCore(Entity 实体( +425
Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Create(Entity 实体( +13

我已经研究了,但还没有发现任何类似的问题。以前有人经历过吗?

-----------------编辑我认为这种方法应该以不同的方式完成,所以我做了一些小的更改。我不是尝试添加新的时间表线(与时间表相关 - 一个时间表可以有 0 个或多个时间表(,而是尝试检索时间表并将时间表列表实例化为 id,然后更新。

以下更新的代码:

using (_serviceProxy = CrmConnector.GetOrganizationProxy(serverConfig))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();
                    _service = (IOrganizationService) _serviceProxy;
                    var context = new CRMService(_serviceProxy);
                    var timesheetLineEntity = new New_timesheetlineitem()
                    {
                        // taken from user input 
                        New_startdatetime = timesheetLineVm.TimesheetLineViewModels.StartDate,
                        New_EndDateTime = timesheetLineVm.TimesheetLineViewModels.EndDate,
                        //new_paytypetimesheetlineitemid = Convert.ToString(timesheetLineVm.TimesheetLineViewModels.PayType),
                        //timesheetLineEntity["new_lunchtime"] = 2;
                        New_SubmittedHours = timesheetLineVm.TimesheetLineViewModels.SubmittedHours
                        //_timesheetlineId = _service.Create(timesheetLineEntity);
                    };

                    var timesheetLineItemList = new List<New_timesheetlineitem>();
                    timesheetLineItemList.Add(timesheetLineEntity);
                    var retrievedTimesheet = _serviceProxy.Retrieve("new_timesheet", new Guid(timesheetLineVm.TimesheetId.ToString()), new ColumnSet(new string[] {"new_firstname", "new_lastname", "new_name", "new_payrollreference", "new_timesheetdate", "new_timesheetid", "new_slatimesheetid", "new_stream3timesheetid", "new_candidatetimesheetid", "new_accounttimesheetid", "new_billtoid", "new_contracttypetimesheetid", "new_billratetimesheetid", "new_status", "new_timesheet_new_approverid", "new_invoiced"})) as New_timesheet;
                    if (retrievedTimesheet != null)
                    {
                        retrievedTimesheet.new_new_timesheet_new_timesheetlineitem = timesheetLineItemList;
                        _serviceProxy.Update(retrievedTimesheet);
                    }
                    //context.AddObject(timesheetLineEntity);
                    // context.SaveChanges();
                }

我现在收到以下错误:

系统.服务模型.故障异常 1 was unhandled by user code
HResult=-2146233087 Message=Entity Id must be specified for Update
Source=mscorlib
Action=http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/UpdateOrganizationServiceFaultFault StackTrace: Server stack trace: at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.Xrm.Sdk.IOrganizationService.Update(Entity entity) at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.UpdateCore(Entity entity) at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Update(Entity entity) at TimesheetSample.Controllers.TimesheetController.SaveTimesheetLine(TimesheetViewModel timesheetLineVm) in c:'Users'Demerson.Herculano'Documents'AES Projects'TimesheetSample'TimesheetSample'Controllers'TimesheetController.cs:line 159 at TimesheetSample.Controllers.TimesheetController.New(TimesheetViewModel timesheetVm) in c:'Users'Demerson.Herculano'Documents'AES Projects'TimesheetSample'TimesheetSample'Controllers'TimesheetController.cs:line 110 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2 个参数( at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8 1.b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult'1.End(( at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult 异步结果( at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.b__33(( at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.b__49(( 内部异常:

提前感谢,德姆

使用 MSCRM 服务保存数据

问题是您没有考虑Dynamics CRM的字段类型。

让我们检查这一行:

timesheetLineEntity["new_firstname"] = timesheetLineVm.TimesheetLineViewModels.EmployeeFirstName;

可能CRM内部new_firstname是一个single line of text字段,因此如果EmployerFistName是一个字符串,则设置值没有问题。

现在检查此行:

timesheetLineEntity["new_billtoid"] = timesheetLineVm.TimesheetLineViewModels.BillToId;

CRM中可能new_billtoid字段是指向名为new_billtonew_bill的实体的Lookup。C# 中的查阅字段映射为EntityReference因此正确的方法(假设实体名称为 new_billBillToId是 GUID(为:

 timesheetLineEntity["new_billtoid"] =
 new EntityReference("new_bill", timesheetLineVm.TimesheetLineViewModels.BillToId);

底线:您需要检查需要设置的所有字段的确切类型,并在必要时将它们转换为正确的类型。

I am not clear about the approach but as it looks like you are using mvc approach or website where you are using CRM service to CRUD crm records.
So I am putting my points on that basis:

 1. First of all you have to check whether you are connected to CRM service or Not (I think you are connected to CRM service thats why are are getting those error message on create or update record but anyway you can check like this below) 
public void SaveTimesheetLine(TimesheetViewModel timesheetLineVm)
{
    string payrollId = Convert.ToString(Session["payroll"]);
    Initiate();
    using (_serviceProxy = CrmConnector.GetOrganizationProxy(serverConfig))
    {
        // This statement is required to enable early-bound type support.
        //_serviceProxy.EnableProxyTypes();
        _service = (IOrganizationService) _serviceProxy;

        WhoAmIRequest request = new WhoAmIRequest();
        WhoAmIResponse response = (WhoAmIResponse)_service.Execute(request);
        Guid userId= response.UserId;
        If(userId==null || userId==Guid.Empty)
            return; // if userid is null or empty then ur crm service is not connected.

 2. SaveTimesheetLine(TimesheetViewModel timesheetLineVm) you are passing timesheetlineVM, this indicates that you you are creating TimeSheetLine for a TimeSheet. I have reviewed you code and found some errors:
This is of EnntityReference (LookUp) Type and you are passing string value 
    timesheetLineEntity["new_payrollreference"] = new EntityReference("entitylogicalname",new Guid(payrollId));
You are not passing the TimeSheet for which this TimeSheetLine will create
    timesheetLineEntity["timesheeetid"]=new EntityReference("timesheet",new Guid(timesheetid));

3. [FaultException`1: System.NullReferenceException: Microsoft Dynamics CRM has experienced an error.
This type of error will come on those case when we are passing null value in CRM. Please check all the values in code and look which is null and required.

4. I have also looked on the Update Code ,this is wrong because in every case you don't have `timesheetLineVm.TimesheetId.ToString()` this will be null or blank.

    var timesheetLineEntity = new New_timesheetlineitem()
                        {
                            // taken from user input 
                            New_startdatetime = timesheetLineVm.TimesheetLineViewModels.StartDate,
                            New_EndDateTime = timesheetLineVm.TimesheetLineViewModels.EndDate,
                            //new_paytypetimesheetlineitemid = Convert.ToString(timesheetLineVm.TimesheetLineViewModels.PayType),
                            //timesheetLineEntity["new_lunchtime"] = 2;
                            New_SubmittedHours = timesheetLineVm.TimesheetLineViewModels.SubmittedHours
                            //_timesheetlineId = _service.Create(timesheetLineEntity);
                        };

                        var timesheetLineItemList = new List<New_timesheetlineitem>();
                        timesheetLineItemList.Add(timesheetLineEntity);
                        var retrievedTimesheet = _serviceProxy.Retrieve("new_timesheet", new Guid(timesheetLineVm.TimesheetId.ToString()), new ColumnSet(new string[] {"new_firstname", "new_lastname", "new_name", "new_payrollreference", "new_timesheetdate", "new_timesheetid", "new_slatimesheetid", "new_stream3timesheetid", "new_candidatetimesheetid", "new_accounttimesheetid", "new_billtoid", "new_contracttypetimesheetid", "new_billratetimesheetid", "new_status", "new_timesheet_new_approverid", "new_invoiced"})) as New_timesheet;
you are declaring `var timesheetLineEntity` and using `timesheetLineVm.TimesheetId.ToString()` in the next line. timesheetLineEntity will only contain these three `New_startdatetime,New_EndDateTime,New_SubmittedHours`

I think you have to review this and in case you need my help I'll glad to support you.
Please ignore if this is not helpful for you.