Dynamics CRM 365 Error 'The Given Key Is Not Present In Dictionary'

Introduction

As a Dynamics CRM developer, we always come across the error saying that 'The Given Key is not present in dictionary'.

The reason we receive this error message is because of the below issues,

When we try to get the attribute value from Entity object without checking the attribute.

String AccountNumber=objAccount["accountnumber"];  

The above line will throw error if ObjAccount doesn't contain this attributes. 

To fix this we should always check attributes in Entity object then get the value.

Resolution

if(objAccount.attributes.contains("accountnumber"))
String AccountNumber=objAccount["accountnumber"];

We can also use below code which will return null if attribute is not present in Entity object.

string AccountNumber=objAccount.GetAtrributeValue<String>("accountnumber");

Happy CRMing!