I using bulk upload address method, the below code is address upload using excel file.
My requirement is how to update the customer If already address available means no need to update, if not available the address means need to update.
I using bulk upload address method, the below code is address upload using excel file.
My requirement is how to update the customer If already address available means no need to update, if not available the address means need to update.
Looks like your problem might be that you're creating new objects when you should not be. When you look for the address by AddressCustomerID
, if one is found the primary key, AddressID
, will be populated. So when the SetAddressInfo()
method is called it already knows the AddressID
has a value so it will perform an update with those values you populate in those fields. If there is no AddressID
, it will perform an insert.
So try something like this:
// see if address exists
AddressInfo address = AddressInfoProvider.GetAddresses() .WhereEquals("AddressCustomerID", customerID) .FirstOrDefault();
if (address == null)
{
// set the values
address = new AddressInfo();
address.AddressName = "Address1";
address.customerID = customerID;
address.AddressLine1 = userDto.AddressLine;
...
}
else
{
// assuming the address was found
address.AddressLine1 = userDto.AddressLine;
address.SetValue("Email", userDto.Email);
address.SetValue("CompanyName", userDto.Company);
...
}
AddressInfoProvider.SetAddressInfo(address);
Continue to use the same address
object throughout the process vs. creating new ones.
I want to be able to use this upload new addresses' in Kentico, but also to modify/update Addresses' and their information at the same time.
In the below code existing address working fine, but new address I upload means not working.
AddressInfo address = AddressInfoProvider.GetAddresses()
.WhereEquals("AddressCustomerID", customerID )
.FirstOrDefault();
if (address != null)
{
//address = new AddressInfo();
address.AddressName = string.Join(", ", addressNameFields);
address.AddressCustomerID = customerID;
address.AddressLine1 = userDto.AddressLine;
address.AddressLine2 = userDto.AddressLine2;
address.AddressZip = userDto.PostalCode;
address.AddressCity = userDto.City;
address.AddressCountryID = country.CountryID;
address.AddressStateID = state.StateID;
address.AddressPhone = userDto.PhoneNumber;
address.AddressPersonalName = userDto.ContactName ?? $"{userDto.FirstName} {userDto.LastName}";
address.SetValue("Email", userDto.Email);
address.SetValue("CompanyName", userDto.Company);
address.SetValue("Status", "1");
address.SetValue("AddressType", AddressType.Shipping.Code);
}
else
{
address.AddressName = string.Join(", ", addressNameFields);
address.AddressCustomerID = customerID;
address.AddressLine1 = userDto.AddressLine;
address.AddressLine2 = userDto.AddressLine2;
address.AddressZip = userDto.PostalCode;
address.AddressCity = userDto.City;
address.AddressCountryID = country.CountryID;
address.AddressStateID = state.StateID;
address.AddressPhone = userDto.PhoneNumber;
address.AddressPersonalName = userDto.ContactName ?? $"{userDto.FirstName} {userDto.LastName}";
address.SetValue("Email", userDto.Email);
address.SetValue("CompanyName", userDto.Company);
address.SetValue("Status", "1");
address.SetValue("AddressType", AddressType.Shipping.Code);
}
AddressInfoProvider.SetAddressInfo(address);
Did you look at E-Commerce API, specifically at Customer API Updating address