-2

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. enter image description here

krishnan
  • 33
  • 6

3 Answers3

0

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.

Brenden Kehren
  • 5,919
  • 16
  • 27
0

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);
krishnan
  • 33
  • 6
-1

Did you look at E-Commerce API, specifically at Customer API Updating address

  • WHEN an Admin uploads the excel file to Kentico, 1.FOR EACH row in the spreadsheet (each address), Kentico will check to see if the address already exists within the specific site that the upload is done for. This check will be done by comparing the “Address Line” and “Address Line 2” fields in Kentico with the corresponding columns in the upload spreadsheet. 2. IF the address DOES NOT already exist: Kentico will create the address on the site, populate the address' fields corresponding to the data present in the upload spreadsheet, and assign the address to the users selected by the Admin. – krishnan Jul 26 '19 at 10:31
  • I downloading the COM_address table using kentico API in C#, I used below below code for download the data form com_address table to excel file, excel file will be downloading but data is not coming. – krishnan Aug 13 '19 at 08:11