Try our conversational search powered by Generative AI!

Can't save new CustomerContact after having set PreferredBillingAddress.

Vote:
 

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:

customerContact.AddContactAddress(invoiceAddress);
 
 customerContact.PreferredBillingAddress = invoiceAddress;
 
 customerContact.SaveChanges();

The address is created with the following statement:
CustomerAddress.CreateForApplication(AppContext.Current.ApplicationId);

Does anyone encountered this error before?

PS: Not sure about the Commerce version, the MediaChase.Commerce.dll says version 5.2.628.0.

Thanks in advance!

Regards,

Robert.
 
#81340
Feb 14, 2014 12:23
Vote:
 

Hi, 5.2.628 version is for Commerce R2 SP2. Can you post the full stacktrace?

Regards.

/Q

#81367
Feb 15, 2014 3:59
Vote:
 

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.

#81400
Feb 17, 2014 8:29
Vote:
 

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
#81401
Feb 17, 2014 8:41
Vote:
 

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.

#81404
Feb 17, 2014 9:30
Vote:
 

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

#81407
Edited, Feb 17, 2014 9:43
Vote:
 

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.

#81410
Edited, Feb 17, 2014 10:23
Vote:
 

My bad, you don't need this check:

if (customerContact.PrimaryKeyId == null)

Regards.

/Q

#81412
Feb 17, 2014 10:35
Vote:
 

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.

#81416
Feb 17, 2014 10:55
Vote:
 

The original exception seems to originate from Mediachase.Commerce.Core.RecentReferenceHistory.RecentReferenceDetectPlugin.

What does this plugin do and is it really required?

Regards,

Robert.

#81420
Feb 17, 2014 10:57
Vote:
 

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.

#90932
Sep 22, 2014 15:12
Vote:
 

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

#90935
Sep 22, 2014 15:54
Vote:
 

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.

#91161
Edited, Sep 27, 2014 0:21
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.