I have below piece of code in web service apex class. If account is found with given emailId then I am updating the account, if account is not found with emailId then I am creating new account record.
global class MyWebServiceClass {
global MyWebServiceClass () {
}
@TestVisible
global class GetAccInformation{
webservice String accNum{get;set;}
webservice String accName{get;set;}
webservice String SerialNo{get;set;}
webservice String Email{get;set;}
}
@TestVisible
global class resultclass {
webservice string Active{get;set;}
webservice string ERROR_CODE{get;set;}
}
webservice static resultclass accountDML(GetAccInformation RetInfo) {
resultclass re = new resultclass();
list<Account> personAccounts = new list<Account>();
personAccounts = [select id,Name,AccountNumber,SLASerialNumber__c,Email__c from Account where Email__c = :RetInfo.Email limit 1];
Account acc = new Account();
if(personAccounts.size() > 0){
acc.Id = personAccounts[0].Id;
acc.Name = RetInfo.accName;
acc.AccountNumber = RetInfo.accNum;
acc.SLASerialNumber__c = RetInfo.SerialNo;
update acc;
}
else{
acc.Name = RetInfo.accName;
acc.AccountNumber = RetInfo.accNum;
acc.SLASerialNumber__c = RetInfo.SerialNo;
acc.Email__c = RetInfo.Email;
insert acc;
}
re.Active = 'TRUE';
return re;
}
}
The problem is - Within 20 seconds if 2 requests are received with same email id then its creating 2 accounts with same email id, as per logic written it should not create new account when second request received with same email id.
When its analysed in depth, its found that - due to some connectivity issue requester(External system) is not getting response hence external system will again attempt by sending same request within 20 seconds. Hence at salesforce end within a span of 20 sec we are getting 2 requests for same inputs and for both requests our webservice is creating 2 account records.
Can somebody help me to understand why my webservice is creating duplicate account for same email id?