I am working on a project that intends to use crmservice in order to save data on existing database. The database is created by mscrm and I have to create an application that uses crmservice to store relevant information on db.
So far, I am receiving data from the viewModel and trying to save using crmservice.
method for saving below:
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);
}
}
The error I am receiving:
[FaultException`1: System.NullReferenceException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #E28869A7]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +11080899
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +336
Microsoft.Xrm.Sdk.IOrganizationService.Create(Entity entity) +0
Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.CreateCore(Entity entity) +425
Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Create(Entity entity) +13
I have researched and not yet found any similar issue. Would anyone have gone through this before?
----------------- EDITED I think this approach should be done differently, so I done some small changes. Instead of trying to add a new timesheetLine (is related to timesheet - one timesheet can have 0 or more timesheetLines), I am trying to retrieve a timesheet and instantiate a list of timesheetines to id, then update.
Below updated code:
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();
}
I am now getting the following error:
System.ServiceModel.FaultException
1 was unhandled by user code2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
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, IDictionary2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass81.b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.b__33() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.b__49() InnerException:
Thanks in advance, Dem