How to set the parent account via SDK

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

2 comments:

Niclas Pålsson said...

Hi Ronald!

This is how we want it to function, right? But, the parentaccountid is actually a readonly field via SDK for CRM 3.0 AND 4.0... That's where my problem begins.

Do you have any idea how to set the parent account in any way in CRM 4.0 SDK? I've tried the same approach as in your article as well as the AssociateEntitiesRequest message like this:

Dim myService As New CrmServiceObject(myCrmOrgDetails)
Dim myRequest As AssociateEntitiesRequest = New AssociateEntitiesRequest

myRequest.Moniker1 = New Moniker
myRequest.Moniker1.Id = New Guid(Parent_Id)
myRequest.Moniker1.Name = EntityName.account.toString

myRequest.Moniker2 = New Moniker
myRequest.Moniker2.Id = New Guid(Child_id)
myRequest.Moniker2.Name = EntityName.account.toString

myRequest.RelationshipName = RelationshipName

myService.Execute(myRequest)

___________________________________
Nice blog however!

Ronald Lemmen said...

I guess something must be wrong with your code... The parentaccountid is not read only and can be set as shown in my code example. Could you post your code?