Last week I've been working on a migration application. One requirement in the application was to set the parent account of child accounts. It could be that a parent account has a parent account itself as well. So in my application I have followed this approach:
- For each account in the source, create an account in CRM 3.0
- For each account in the source, map the parent account to a crm accout and update the account in CRM 3.0
No big deal. This worked for the first thousands of accounts, but then suddenly an error message showed up:
<detail>
<error>
<code>0x80040237</code>
<description>Operation failed due to a SQL integrity violation.</description>
<type>Platform</type>
</error>
</detail>
After a lot of research and discussing with Luis MazarÃo we found out in what situation this error occurres. It appeared that in some cases this error was thrown when the account already has subaccounts and I want to set the parent account. After even more research we found out that the source of this error was the code which I used to update the parent account. I was using a code like this:
Lookup lookup = new Lookup();
lookup.type = EntityName.account.ToString();
lookup.Value = gParentId;
account acc = service.Retrieve(EntityName.account.ToString(), gAccountId, new AllColumns());
acc.parentaccountid = lookup;
service.Update(acc);
Although it works most of the times, it is not the right code. This is the correct code:
Lookup lookup = new Lookup();
lookup.Value = id_parent;
lookup.type = EntityName.account.ToString();
account acc = new account();
acc.accountid = new Key();
acc.accountid.Value = id;
acc.parentaccountid = lookup;
service.Update(acc);
We're still digging a bit more on why the error occurred at all, but I hope this helps you!
Ronald