• I have an object in which there are two fields i.e Phone no. & email.
  • The combination of these fields should be unique
  • Right now I can create just one unique field But I want their composite to work like a unique field.

I know that I can create composite key in SQL but I dont know how to achieve this in salesforce.


To achieve this you need to have following things in mind:

  • We can not create composite key in salesforce.
  • There is already an Idea posted about this.
  • You can achieve this by writing a trigger in which you can query old records and check unique behaviour as per your need.

You can ask me if you have any doubt.

| improve this answer | |
  • Can we use formula field to achieve this ? – GrV Aneja Feb 12 '16 at 20:11
  • 1
    we can not create formula field unique. – ashishcloud Feb 12 '16 at 20:11
  • 2
    If you are going to mention that there is an Idea posted, you should link to it... – Adrian Larson Feb 12 '16 at 20:19
  • 2
    @GrVAneja you can use workflows and do not need code. Look at Adrian's answer – Samuel De Rycke Feb 15 '16 at 7:50

You can create a unique composite by concatenating values. The steps would be:

  1. Create a unique Text field.
  2. Create a Workflow Field Update to concatenate the fields whose composite you want to be unique and populate into the above field.
  3. Create a Workflow Rule that uses the field update above if any of the source fields have changed.
| improve this answer | |
  • 1
    This process works well. If you mark the text field as an external key, it will enforce uniqueness and be indexed. In our org, we separate our values with an underscore. For example, ClientCode_EventCode_LocationCode. – Christian Anderson Feb 12 '16 at 22:52
  • @ChristianAnderson Indeed, there are many concatenation schemes. Dashes and underscores are probably the most common. Thanks for the feedback! – Adrian Larson Feb 12 '16 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.