Note: You may receive a server error when attempting to run the code if you CRM user account does not have the appropriate security settings shown below. Click the “Customization” tab, and then set the “Read” permissions on “Entity”, “Attribute” and “Relationship” as shown below: