function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC#NewbieSFDC#Newbie 

Apex Rest Api create duplicate leads

I have written a custom Apex rest api, that will create and update lead in salesforce. Whenever the api is called from external system, salesforce will get a json file which will have lead deatails and email id. If the email id already exists in salesforce it will update the lead. Else create a new one.
Whenever there is some 2 or more requests call at same time and it is lead creation requests, both requests are inserting the leads. This creates the duplicate lead.
We can enable duplicate rule, but it will not process another request. I want to process all requests without skipping any requests.
Do you have solution for this issue

Thanks
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/66050/duplicate-matching-rules-via-rest-api

This might help you.

Check it once.
Dushyant srivastava 8Dushyant srivastava 8
Assuming JSON is in the format given below
[
	{
		"Name" : "Lead1",
        "Email" : "xyz@gmail.com"
	},
	{
		"Name" : "Lead2",
        "Email" : "abc@gmail.com"
	}
]

So what we need to do here is deserialize this and create a list of leads code would look something like this
 
List<Lead> lstLead = (List<Lead>)JSON.deserialize(*JSON*, List<Lead>.class);  // Deserialization

// Code to collect all email id.
Map<String, Lead> mapEmails = new map<String, Lead>();

for(Lead objLead : lstLead)
{
    mapEmails.put(objLead.Email , objLead);
}

for(Lead objLead : [Select id, Name from Lead Where Email IN: mapEmails.keySet()])
{
	objLead.Name = mapEmails.get(objLead.Email).Name;
	mapEmails.put(objLead.Email , objLead);           // Code to replace the lead instance with the one we need to update.
}

upsert mapEmails.values();

Basically, you have to replace the object instance you got from JSON with the one which already exists in the system(which have ids)

Best Regards
Dushyant Srivastava
SFDC#NewbieSFDC#Newbie

Thanks @Abhishek,

I will try on duplicate rule, if duplicate detected I will try to process the request again. so that the another request update on same record. Let you know how its work

SFDC#NewbieSFDC#Newbie
Hi @Dushyant srivastava 8, Thanks for answering,
I wanted to clarify that json would be like below, one lead object will send on one time.
{
	"Name" : "Lead1",
    "Email" : "xyz@gmail.com"
}

And some times the requests are landing on same time. I have shared the screenshot of requests time here. The issue is while one request is processing another request also processing. At this time the query is not fetching the inserted records in that too fast. There is some delay in database comit and fetching the results. Here the duplicate records creating.
 Requests landing timestamps

zaid abu lawizaid abu lawi
 hey SFDC#Newbie ,can you please tell me how to create a leads using a rest api from an external system
Thanks