I am facing error in one of future method (calling from trigger handler). This error occurs when integration event submit these record in bulk but when i am doing this from UI i do not get this error. I am trying to recreate scenario but it work fine when i do from UI. Can anyone please help me on this.
Failed to invoke future method 'public static void getOpportunity(Set, Set)' on class 'OpportunityCloning_Handler' for job id '7070e00004gqK5G'
caused by: System.QueryException: Record Currently Unavailable: The record you are attempting to edit, or one of its related records, is currently being modified by another user. Please try again.
Class.OpportunityCloning_Handler.getValidOpportunity: line 498, column 1
Class.OpportunityCloning_Handler.CreateOpportunity: line 553, column 1
My Future method is
@future (callout=true)
public static void getOpportunity(set<id> oppId,set<String> quoteIds){
CreateOpportunity(oppId,quoteIds);
}
public static void CreateOpportunity(set<id> oppId,set<String> quoteIds){
list<Opportunity> Opportunities1 = getValidOpportunity(oppId, quoteIds); // This has query on Opp and related Object with FOR UPDATE
}
public static list<Opportunity> getValidOpportunity(set<id> oppId,set<String> quoteIds)
{
list<Opportunity> Opportunities1 = null;
if(quoteIds == null || quoteIds.isEmpty())
{
Opportunities1 =
[select id,Account__c,Account__r.Unique_Identifier__c,Account__r.bitLink__c,Account__r.eEnrollment__c,Account__r.segment__c,
(Select P_Location_Contact__c,P_Location_Contact__r.P_Contact__c, P_Location__c,
From Q_Opportunity_Producer_Filters__r)
(SELECT Id, Comment__c, Type__c FROM Comments__r WHERE Type__c = 'Comments for ASS')
from Opportunity where (id IN: oppId) AND RecordType.Name != 'Pre Sale'
AND (Quote_Status_changed_by__c ='Manual' OR Quote_Status_changed_by__c ='Integration Event')AND IsASSCreated__c = false FOR UPDATE];
}else{
Opportunities1 =
[select id,Account__c,Account__r.Unique_Identifier__c,Account__r.bitLink__c,Account__r.eEnrollment__c,Account__r.segment__c,
(Select P_Location_Contact__c,P_Location_Contact__r.P_Contact__c, P_Location__c,
From Q_Opportunity_Producer_Filters__r) ,
(SELECT Id, Comment__c, Type__c FROM Comments__r WHERE Type__c = 'Comments for ASS')
from Opportunity where (id IN: oppId OR QuoteID1__c IN:quoteIds) AND RecordType.Name != 'Pre Sale'
AND (Quote_Status_changed_by__c ='Manual' OR Quote_Status_changed_by__c ='Integration Event' ) AND IsPSSCreated__c = false FOR UPDATE];
}
return Opportunities1 ;
}
Trigger calling getopportunity method :
try{
list<Integ_Exceptions__c > lstErros = new list<Integ_Exceptions__c >();
set<Id> OppId = new set<id>();
set<String> QuoteIds = new set<String>();
for(Opportunity opp : newRecords){
//Code-Logic
}
if((OppId!=null && OppId.size()>0) && sObj_Utility.isopptyCreated == false) {
OpportunityCloning_Handler.getOpportunity(OppId,QuoteIds);
}
if(lstErros.size() > 0)
Database.insert(lstErros ,false);
}
catch(Exception e){
Integ_Exceptions__c err = new Integ_Exceptions__c( Area__c = 'AMM Creation',
Message__c = e.getMessage() +'\n' + e.getStackTraceString() );
insert err ;
}
}
}
getValidOpportunity()
it looks like that's where the error is originating from!