I'm struggling with the following piece of code:
EntityReferenceCollection erc = new EntityReferenceCollection();
erc.Add(new EntityReference("role", new Guid(sRoleID)));
_service.Associate("systemuser", new Guid(sUserID), new Relationship("systemuserroles_association"), erc);
What's going on here is that I'm just trying to add certain role to a new CRM user in systemuser entity's Post Create plugin. Triggering/executing the plugin code above results in exception being thrown with the following message: 'Error: usersettings With Id = 0083f19f-1f6b-e011-962a-00155d6f1007 Does Not Exist'.
Seems like behind the scenes Associate method does some sort of check up routine and checks if both systemuser and usersettings instances exists. Well systemuser instance of course already exists as the code is running in post stage, but the usersettings instance hasn't been yet created (usersettings instance is created only after the db transaction has occurred?) and thus the exception is thrown.
Similar code in CRM 4 did work just fine. It appears the problem is caused by the new transaction model the CRM 2011 has introduced for the plugins? In CRM 4 usersettings instance would already exist in this case as the post stage plugin executes outside the database transaction.
Is there any workaround? Suggestions?