Developer forum

Forum » Integration » Importing addresses

Importing addresses

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I have an XML file that contains users and for each of those users one or multiple addresses. I initially built a task that would import users and addresses in one go. However, I discovered that this would not delete old address records from a user so if they moved they would end up with multiple addresses. I then separated the users and addresses in two separate tasks but now hit another roadblock: how do I tell which user ID belongs to the address? I see two options, but neither works:

1. When creating the user, assign ExternalID a value  but leave UserID alone. This caused Dynamicweb to generate the ID automatically. This doesn't work as I don't know what the user's ID is when inserting the address.
 

2. When creating the user, assign UserID a value with the external ID from my XML source. Then I can use that ID to associate the address with. That won't work either, as I could have (in fact, already do have) conflicts in IDs. The customer's XML contains a user with ID 1 as well, so I just wiped out the angel account ;-(

What are my options? Looks like this could use something we have with products where we can reference a product by something other than the ID alone.

Ideas are very welcome!

Thanks,

Imar

 


Replies

 
Dmitriy Benyuk
Reply
This post has been marked as an answer

Hi Imar,

When importing addresses the value from the column AccessUserAddressUserId is used to search users in the following way:
if it is an integer value – it looks for the AccessUserID with this value
otherwise or if the user is not found by ID it is searching by the columns: "AccessUserUserName", "AccessUserCustomerNumber", "AccessUserEmail".

The key column is not used during this(it can be implemented if needed or those fields are not enough).
So you can use some string value which may identify the user by user name, customer number or email.
Regards, Dmitrij

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Dmitrij,

Turned out I couldn't use Email (not unique) and CustomerNumber (not unique; contains a shared parent customer account used elsewhere). I'll try UserName next time I build something similar, but for now I settled with the following script task:

public class RemapUserIds : TableScript
{
  public override void ProcessInputRow(Mapping mapping, Dictionary<string, object> row)
  {
    var user = User.GetUserBySql(string.Format("SELECT * FROM AccessUser WHERE AccessUserExternalID = '{0}'", row["AccessUserAddressUserID"]));
    if (user != null)
    {
      row["AccessUserAddressUserID"] = user.ID.ToString();
    }
  }

  public override string ScriptingName
  {
    get { return "Fix user IDs"; }
  }
}

I realize this has some potential performance issues (N+1) and maybe even SQL injection possibilities but it works great against my limited set of users.

Thanks,

Imar

 
Jonas Krarup Dam
Reply

Hey Imar,

Maybe you could put the externalID in the AccessUserCustomerNumber column (as well as the ExternalUd column), and then add a custom field for the actual custumer number. That way, the automatic mapping will work, and you will still have the customer number available.

Regards

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Yeah, that sounds like it would work too. Thanks!

 

You must be logged in to post in the forum