Modify behavior of bulk address upload to allow updating existing addresses

Ramakrishnan G asked on July 25, 2019 16:39

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.

   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.

Recent Answers


Ramakrishnan G answered on July 25, 2019 16:43 (last edited on July 26, 2019 12:33)

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 26, 2019 14:52

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.

0 votesVote for this answer Mark as a Correct answer

Ramakrishnan G answered on July 26, 2019 16:39 (last edited on July 29, 2019 08:22)

AddressInfo address = AddressInfoProvider.GetAddresses() .WhereEquals("AddressCustomerID", customerID) .FirstOrDefault();

        if (address == null)
        {
            address = new AddressInfo();
            address.AddressName = "Address1";
            address.AddressCustomerID = customerID;
            address.AddressLine1 = userDto.AddressLine;
            address.AddressLine2 = userDto.AddressLine2;
            //address.AddressCity = userDto.City;
            address.AddressPhone = userDto.PhoneNumber;
            address.SetValue("Email", userDto.Email);
            address.SetValue("CompanyName", userDto.Company);
            AddressInfoProvider.SetAddressInfo(address);
        }

I updated abaove code not working. Please help.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 26, 2019 17:27

Your code is NOT updated. In the ELSE statement, you're creating a new AddressInfo object. There is NO NEED to do this because this is where the existing address is found. Simply use the existing address object and set the values of the fields you want and perform the SetAddressInfo() after. Look at the code above I posted closer.

0 votesVote for this answer Mark as a Correct answer

Ramakrishnan G answered on July 29, 2019 10:22

Hi, Updated the below code,the exixting address doesn't added its working correctly. If I upload new address means not added.

ssPersonalName = 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);
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 29, 2019 16:57

You'll need to specify more what "it doesn't work" means and what you've done.

What have you done to troubleshoot the problem?

Are you getting any events/errors in the event log?

Have you debugged the solution?

0 votesVote for this answer Mark as a Correct answer

Ramakrishnan G answered on July 29, 2019 17:01

If I existing address update function working fine. And upload new address means not working. I checked the event log nothing is there.

If I debug the code, the cursor not come the else statement.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 29, 2019 17:03

Share your full code block.

0 votesVote for this answer Mark as a Correct answer

Ramakrishnan G answered on July 29, 2019 17:11

protected void CreateCustomerAddress(int customerID, UserDto userDto) { var country = FindCountry(userDto.Country); if (country == null) { EventLogProvider.LogInformation("Import users", "INFO", $"Skipping creation of address of user { userDto.Email }. Reason - invalid country."); return; }

        var state = FindState(userDto.State, country.CountryID); 

        var addressNameFields = new[] { $"{userDto.FirstName} {userDto.LastName}", userDto.AddressLine, userDto.AddressLine2, userDto.City }

            .Where(af => !string.IsNullOrWhiteSpace(af));         

        AddressInfo address = AddressInfoProvider.GetAddresses()
        .WhereEquals("AddressCustomerID", customerID)
        .FirstOrDefault();

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);
0 votesVote for this answer Mark as a Correct answer

Ramakrishnan G answered on July 30, 2019 16:47

Any update on this?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 30, 2019 20:29

I'd suggest adding some try{}catch{} blocks in there and log events in the Kentico event log to troubleshoot more.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.