Update the Customer Address

Ramakrishnan G asked on July 25, 2019 16:14

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.

Recent Answers


Brenden Kehren answered on July 26, 2019 14:51

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

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