Hi, 5.2.628 version is for Commerce R2 SP2. Can you post the full stacktrace?
Regards.
/Q
Hi,
Thanks for your reply.
Some additional info: We started from the ClickTalk sample.
The following stacktrace comes from the exception which is thrown after the SaveChanges call on the CustomerContact instance.
at Mediachase.Commerce.Core.RecentReferenceHistory.RecentReferenceDetectPlugin.LoadOrCreateUserRecentReference(String metaClassName, PrimaryKeyId primaryKeyId, Object userId)
at Mediachase.Commerce.Core.RecentReferenceHistory.RecentReferenceDetectPlugin.SaveSelectedReference(MetaField reference, PrimaryKeyId referenceValue)
at Mediachase.Commerce.Core.RecentReferenceHistory.RecentReferenceDetectPlugin.Mediachase.BusinessFoundation.Data.Business.IPlugin.Execute(BusinessContext context)
at Mediachase.BusinessFoundation.Data.Business.BusinessManager.ExecutePlugins(EventPipeLineStage eventPipeLineStage)
at Mediachase.BusinessFoundation.Data.Business.BusinessManager.Execute(Request request)
at Mediachase.BusinessFoundation.Data.Business.BusinessManager.Create(EntityObject target)
at Mediachase.Commerce.Customers.CustomerContact.SaveChanges()
at EPiServer.Business.Commerce.Sample.Templates.ClickTalk.Pages.MyAccount.Register.CreateUser()
Regards,
Robert.
Hi,
I suspect you're creating new CustomerContact instance (from stacktrace, CreateUser()). It looks like to save the PreferredBillingAddress property, you need to save the CustomerContact first. Please try with this:
customerContact.SaveChanges();
customerContact.AddContactAddress(invoiceAddress); customerContact.PreferredBillingAddress = invoiceAddress; customerContact.SaveChanges();
Regards.
/Q
Hi Quan Mai,
That's correct, the CustomerContact instance is a new instance.
Saving the CustomerContact instance twice does work, but there is one problem.
We have registered two plugins in baf.data.manager.config which captures the insert and update events whenever a CustomerContact instance is saved. When we save the CustomerContact instance twice we get two events, an insert event followed by an update event. We were trying to avoid that by saving the CustomerContant instance once. Unfortunatly that doesn't seem possible.
Regards,
Robert.
Hi,
Not entirely impossible. You can try this workaround:
var customerContact = CustomerContact.CreateInstance(user);
customerContact.PrimaryKeyId = BusinessManager.Create(customerContact);
customerContact.FirstName = firstName;
customerContact.LastName = lastName;
....
The idea is to call BusinessManager.Create(EntityObject), which will create CustomerContact object instead of calling SaveChanges(). Worked on 7.5, I'm not sure about R2 SP2 :).
Regards.
/Q
Hi,
Unfortunatly that gives the same error.
As an alternative I tried creating an address with:
CustomerAddress address = CustomerAddress.CreateForApplication(AppContext.Current.ApplicationId);
address.PrimaryKeyId = BusinessManager.Create(address);
But that gives error: Violation of PRIMARY KEY constraint 'PK__cls_Address__25918339'. Cannot insert duplicate key in object 'dbo.cls_Address'. After calling SaveChanges() which seems logical, I guess SaveChanges is doing something similar.
Regards,
Robert.
My bad, you don't need this check:
if (customerContact.PrimaryKeyId == null)
Regards.
/Q
This does work to a certain level, this doesn't throw any exception when calling SaveChanges.
But the downside is that the BusinessManager.Create fires the insert event, so this basically is the same as calling SaveChanges twice.
I guess we'll have to work around this somehow.
Regards,
Robert.
The original exception seems to originate from Mediachase.Commerce.Core.RecentReferenceHistory.RecentReferenceDetectPlugin.
What does this plugin do and is it really required?
Regards,
Robert.
When doing searches/updates on Organization objects I discovered lots of DB activity related to RecentReferenceHistory. My guess is that this keeps track of recent references (duh) but afaik it's not used in Commerce Manager anywhere. You can try and disable the plugin altogether in baf.data.manager.config.
IIRC then RecentReferenceHistory will be used so you get some kind of convenience when you use Business object. For example when you create an order in CM, then recent used contacts will be listed first, so you can easily choose one. But it should not have any critical functionalites, so it should be safe to turn if off.
Regards.
/Q
Might be a bit OT here but I was working on a big customer import where we updated 17K Organization entries in a batch through the BF API. With RecentReferenceHistory disabled, the job ran for just little more than half the time - as compared to having it enabled (6.42 minutes vs 11.26 minutes). So if you're doing lots of changes to BF objects and you have a client that's not a frequent visitor to CM (hence benefitting from the "recent-functionality") it might be beneficial to disable this plugin.
Hi All,
We are having some trouble saving a new CustomerContact object through CustomerContact.SaveChanges() after having set a preferred shipping/billing address through the CustomerContact.PreferredBillingAddress or CustomerContact.PreferredShippingAddress properties. It does work fine for existing customers.
We get the following exception "Could not load object '2b9cda98-f8ad-4d73-84a7-9748775b3f00', meta class 'Address'.".
The exception seems to be thrown by MediaChase.Commerce.Core.RecentReferenceHistory.LoadOrCreateUserRecentReference
We set add the address to the contact like this: