Steps

  1. Create/select existing a fields for required entity.
  2. Add fields in Alternate Key
  3. Upsert using code

Steps in Detailed

  1. We will consider Lookup,Optionset and string data type fields for alternate key.

2. Create a new Alternate Key

3. Upsert using Code

For lookup, parse GUID to string format.

For Optionset, parse int value to string format.

Code snapshot

KeyAttributeCollection KeyAttributeCollection = new KeyAttributeCollection();                 KeyAttributeCollection.Add("apsm_fieldmapping_fieldid", fieldMappingId.ToString());                 KeyAttributeCollection.Add("apsm_slot_value", optionsetValueCount.ToString());                 KeyAttributeCollection.Add("apsm_os_type", "896950000");                 Entity entity = new Entity("account");                 entity.KeyAttributes = KeyAttributeCollection;                 entity["name"] = "Test Account for Alternate Key";                 UpsertRequest req = new UpsertRequest()                 {                     Target = entity                 };                 UpsertResponse upsertResponse = (UpsertResponse)service.Execute(req);

Conclusion

In most of the cases, we are required to have a unique value in the combination of lookup and Optionset. This is possible with a new release of v9.x.

Cheers !!!!!!!!!!!