I'm trying to set & get the custom property values for a commerce user.
While fetching the value, there is an error:
The given key was not present in the dictionary
public void SetCommerceUserProfilePropertyValues(string usernameWithDomain, List<KeyValuePair<string, object>> keyValuePairs)
{
if (keyValuePairs != null && keyValuePairs.Count > 0)
{
Sitecore.Commerce.Entities.Customers.CommerceUser commerceUser = GetCommerceUser(usernameWithDomain);
if (commerceUser != null)
{
foreach (KeyValuePair<string, object> kvp in keyValuePairs)
{
commerceUser.SetPropertyValue(kvp.Key, kvp.Value);
}
}
}
}
public object GetCommerceUserProfilePropertyValue(string usernameWithDomain, string propertyName)
{
CommerceUser commerceUser = GetCommerceUser(usernameWithDomain);
return commerceUser != null ? commerceUser.GetPropertyValue(propertyName) : null;
}
public void MyMethod()
{
string usernameWithDomain = "Storefront\\myname@email.com";
List<KeyValuePair<string, object>> keyValuePairs = new List<KeyValuePair<string, object>> {
new KeyValuePair<string, object>("ForgotPasswordToken", Guid.NewGuid()),
new KeyValuePair<string, object>("ForgotPasswordTokenCreatedDate", DateTime.Now)
};
///save custom property values
SetCommerceUserProfilePropertyValues(usernameWithDomain, keyValuePairs);
--Exception here
var xtoken = GetCommerceUserProfilePropertyValue(usernameWithDomain, "ForgotPasswordToken") as string;
}
The commerceUser is not null and there is no error while setting the value.
Is there any "Save" action to be done just like it is done for Sitecore user profile - profile.Save()
Using XC 10.1
commerceUser.GetProperties()
, I'm able to see all the new properties.