One of integration performance issue is to set related entity lookup field where we need to get record ID and then set the lookup field while creating/updating record from C#.
As per latest update of SDK, we can now set lookup field using Alternate Key without having to first get record ID. This will improve performance in Integration and migration of records and also in any custom developement.
First I have created two Aletrnate keys, One key in Account based on Accout Number field and another key in Contact entity based on Email Address and First Name.


Make sure you select the keys field such that you don’t have any duplicate records with that field otherwise you will get while activating the keys. You can check the the status of system job in case of any failure.

Entity objAccount = new Entity()
{
LogicalName = "account",
KeyAttributes = new KeyAttributeCollection
{
{"accountnumber","WFT2146" }
}
};
// Set new credit limit;
objAccount["creditlimit"] = new Money(100000);
//Set New Fax
objAccount["fax"] = "MyFax";
// Entity reference using alternate key (emailaddress1) on contact entity
objAccount["primarycontactid"] = new EntityReference("contact")
{
KeyAttributes = new KeyAttributeCollection
{
{"emailaddress1", "alexw@northwindtraders.com"},
{"firstname","Alex"}
}
};
UpdateRequest request = new UpdateRequest()
{
Target = objAccount
};
UpdateResponse response = (UpdateResponse)crmSvc.Execute(request);
As you can see in above code snippet first Alternate key used in Entity definition where I am using Account Number field to update entity record. No need to pass GUID of the record.
And to set Primary Contact lookup on Account I have used Contact Entity ALternate key which is combination of Email Address and FirstName.

Exceptions when using alternate keys
You have to be aware of the following conditions and possible exceptions when using alternate keys:
- The primary ID is used if it is provided. If it is not provided, it will examine the KeyAttributeCollection. If the KeyAttributeCollection is not provided, it will throw an error.
- If the provided KeyAttributeCollection includes one attribute that is the primary key of the entity and the value is valid, it populates the ID property of the Entity or EntityReference with the provided value.
- If the key attributes are provided, the system attempts to match the set of attributes provided with the keys defined for the Entity. If it does not find a match, it will throw an error. If it does find a match, it will validate the provided values for those attributes. If valid, it will retrieve the ID of the record that matched the provided key values, and populate the ID value of the Entity or EntityReference with this value.
- If you specify an attribute set that is not defined as a unique key, an error will be thrown indicating that use of unique key attributes is required.
Hope this helps!
One thought on “{CDS Developer Quick Tip} Set Lookup field using Altername key during Create and Update record from C#”