I am trying to add a new customer record in my Sitecore Commerce through code but I can't find any docs related to it.
I tried using the OData way, but I am getting an error below.
Does anyone know how to do this? Thanks in advance
My Code:
container.AddToCustomers(new Customer()
{
FirstName = firstName,
LastName = lastName,
UserName = username,
Email = email,
Password = password
});
container.SaveChanges();
Error in Commerce Engine Logs:
00249 12:19:28 ERROR ClientCertificateValidationMiddleware: Message = No action match template 'Customers' in 'CustomersController'
System.NotSupportedException: No action match template 'Customers' in 'CustomersController'
at Microsoft.AspNetCore.OData.Routing.Conventions.DefaultODataRoutingConvention.SelectAction(RouteContext routeContext)
at Microsoft.AspNetCore.OData.Routing.ODataActionSelector.SelectCandidates(RouteContext context)
at Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler.RouteAsync(RouteContext context)
at Microsoft.AspNetCore.OData.Routing.ODataRoute.<RouteAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.<Invoke>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Sitecore.Commerce.Core.ClientCertificateValidationMiddleware.<Invoke>d__4.MoveNext()
Update 1
I took a look at the Postman AddCustomer action and tried recreating it in dotnet.
Below is my code
var data = new Dictionary<string, string>()
{
{ "FirstName", firstName },
{ "LastName", lastName },
{ "LoginName", username },
{ "Domain", "Storefront" },
{ "Email", email },
{ "AccountStatus", "ActiveAccount" },
{ "PhoneNumber", "" },
{ "Language", "en" },
};
var entityView = Proxy.GetEntityView(container, string.Empty, "Details", "AddCustomer", string.Empty);
foreach (var field in data)
{
entityView.Properties.First(x => x.Name.Equals(field.Key,StringComparison.OrdinalIgnoreCase)).Value = field.Value;
}
var doUxActionCommand = container.DoUxAction(entityView);
var response = Proxy.DoCommand(doUxActionCommand);
The strange thing is that it is throwing errors saying that the "Action" is null, when I have already set it in my entityView. Anyone has an idea on this?
{
"Id": "b97ed6f2a3394f69896b1572a7397075",
"ResponseCode": "Error",
"Messages": [
{
"MessageDate": "2019-06-21T09:47:55.757604Z",
"Code": "Error",
"Text": "Action for entity view '' cannot be null or empty.",
"CommerceTermKey": "InvalidEntityViewAction"
}
],
"Models": [
{
"EntityId": "",
"EntityVersion": 1,
"Name": "",
"Policies": []
}
],
"ActionUrl": "",
"TaskId": 0,
"Status": "",
"IsFaulted": false,
"IsCompleted": true,
"IsCanceled": false,
"StartTime": "2019-06-21T09:47:55.7566054Z",
"ExecutionTimeMilliseconds": 1
}