I have a usecase where users need to create Lead via Salesforce API. The use case is when user tries to create lead, the system checks if the lead or contact already exists with the same information. If it does not exist already, only then it should create a lead in salesforce.

I can think of writing an apex code if it was web to lead form, but I am trying to understand how to achieve it using an API.

  • 1
    see duplicate management feature
    – cropredy
    Commented Mar 29, 2019 at 17:16

1 Answer 1


I suggest defining your duplicate criteria using the Setup Menu's "Duplicate Rule" and "Matching Rule" options. Are your users using a visualforce page to create the Lead? If so, your apex controller could use Database.SaveResult for the DML which would allow you to check and handle duplicate errors by checking for Database.DuplicateError as shown in https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Datacloud_DuplicateResult.htm#apex_class_Datacloud_DuplicateResult

Update: You mentioned not using visualforce, therefore it seems appropriate to do this within a separate Apex Class and call that somewhere within your execution stack, before insert

  • no they won't be using visualforce to create the lead. But the Salesforce API, maybe using POSTMAN or curl.
    – Student
    Commented Mar 29, 2019 at 15:37
  • I looked into duplicate rule but it only allows me to choose one object. Is there a way I can choose both lead and contact objects because it needs to search in both objects for duplicates.
    – Student
    Commented Apr 2, 2019 at 18:09
  • You would need two duplicate rules. One for Lead. One for Contact. Commented Apr 2, 2019 at 21:37
  • Sometimes the standard Duplicate Rules are 'good-enough', more on those here: help.salesforce.com/… and here is a helpful topic as well: salesforce.stackexchange.com/questions/199477/… (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…) Commented Apr 2, 2019 at 21:47
  • Please help me understand how can I write two duplicate rules? I want when a new lead is created, it checks in the system if the same name exists in Lead or contact and based on that detects the duplicates. How do I make it work with two different conditions?
    – Student
    Commented Apr 8, 2019 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .