• Mark Moore 7
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
This is a cross-post to salesforce.stackexchange.com.

I have a custom API that accepts incoming @HttpPost requests from third parties. The POST request inserts a new complex custom object that may contain one or more Contact records.

There is a race condition between the [SELECT Id FROM Contact WHERE LastName = :newContact.LastName].size() > 0 test and the insert newContact.

This seems like a pretty serious limitation. So, I'm hoping others have found a solution. I have read through the Apex reference, and I can't find anything to eliminate this race.

More Info:
This has turned out to be a pretty serious issue. I'm surprised no one else has run into this.  I have now modified the code to try 5 times to either insert the new record, or find the existing record.  If I try to upload records with 245 roughly simultaneous POSTs, 47 fail (fail to create or find the existing crew member Contact record).

Here is the modified code:
private static Id StoreNewCrewMember(Contact newCrew)
{
	if (newCrew == null)
	{
		return null;
	}

	if (String.isBlank(newCrew.EMSID__c))
	{
		return null;
	}

	Integer i = 0;
	for (i = 0; i < 5; ++i)
	{
            Contact[] existingCrew = [SELECT Id FROM Contact WHERE
                                     EMSID__c = :newCrew.EMSID__c AND MailingState = :newCrew.MailingState
                                    ];
            if (existingCrew.size() > 0)
            {
                return existingCrew[0].Id;
            }

            try
            {
        	insert newCrew;
        
	        return newCrew.Id;
            }
            catch (DmlException ex)
            {
            }
	}
	
	throw new DmlException('Failed '+i+' attempts to create Crew record');
}

What am I missing?
Hello,
I have the follwoing code being executed in a for loop and I want to remove it from the loop and bulkify it how would I do this?
A list, or a map? I preferer to use a map if possible.
 
Account_Procedure_Rate__c rate = [SELECT Amount_To_Pay__c, Procedure_Cost__c FROM Account_Procedure_Rate__c 
														 	 WHERE  CPT_Code__c = :proc.CPT_Code__c AND Main_Center__c = :centerId];
							proc.Amount_To_Pay__c = rate.Amount_To_Pay__c;
							proc.Procedure_Cost__c = rate.Procedure_Cost__c;



Thanks,
K
This is a cross-post to salesforce.stackexchange.com.

I have a custom API that accepts incoming @HttpPost requests from third parties. The POST request inserts a new complex custom object that may contain one or more Contact records.

There is a race condition between the [SELECT Id FROM Contact WHERE LastName = :newContact.LastName].size() > 0 test and the insert newContact.

This seems like a pretty serious limitation. So, I'm hoping others have found a solution. I have read through the Apex reference, and I can't find anything to eliminate this race.

More Info:
This has turned out to be a pretty serious issue. I'm surprised no one else has run into this.  I have now modified the code to try 5 times to either insert the new record, or find the existing record.  If I try to upload records with 245 roughly simultaneous POSTs, 47 fail (fail to create or find the existing crew member Contact record).

Here is the modified code:
private static Id StoreNewCrewMember(Contact newCrew)
{
	if (newCrew == null)
	{
		return null;
	}

	if (String.isBlank(newCrew.EMSID__c))
	{
		return null;
	}

	Integer i = 0;
	for (i = 0; i < 5; ++i)
	{
            Contact[] existingCrew = [SELECT Id FROM Contact WHERE
                                     EMSID__c = :newCrew.EMSID__c AND MailingState = :newCrew.MailingState
                                    ];
            if (existingCrew.size() > 0)
            {
                return existingCrew[0].Id;
            }

            try
            {
        	insert newCrew;
        
	        return newCrew.Id;
            }
            catch (DmlException ex)
            {
            }
	}
	
	throw new DmlException('Failed '+i+' attempts to create Crew record');
}

What am I missing?