[SalesForce] AfterUpdate trigger operation fails; Record is currently being modified by another user

ERROR 1 :

Subject: Developer script exception from Nicomatic : quotepotential :
quotepotential: execution of AfterUpdate 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.
Trigger.quotepotential: line 14, column 1

Apex script unhandled trigger exception by user/organization:
00590000001A6jh/00D90000000bovq

quotepotential: execution of AfterUpdate

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.

Trigger.quotepotential: line 14, column 1

ERROR 2: same

Subject: Developer script exception from Nicomatic :
batchtoquotelineitem : batchtoquotelineitem: execution of AfterUpdate
caused by: System.DmlException: Update failed. First exception on row
0 with id a0L90000004mgNmEAI; first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, quotepotential: execution of
AfterUpdate 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. Trigger.quotepotential: line 14, column 1: []
Trigger.batchtoquotelineitem: line 13, column 1

Apex script unhandled trigger exception by user/organization:
00590000001A6jh/00D90000000bovq

batchtoquotelineitem: execution of AfterUpdate

caused by: System.DmlException: Update failed. First exception on row
0 with id a0L90000004mgNmEAI; first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, quotepotential: execution of
AfterUpdate

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.

Trigger.quotepotential: line 14, column 1: []

Trigger.batchtoquotelineitem: line 13, column 1

can you correct this program to get rid of these two exceptions :

trigger quotepotential on Quote_Line_Item__c (after insert,after update) {
    Set<Id> quoteIds = new Set<Id>();
    List<Quote__c> quotes = new List<Quote__c>();
    for(Quote_Line_Item__c record: Trigger.new) {
    if (record.Quote1__c != null){
        quoteIds.add(record.Quote1__c);
    }
    }
    for(AggregateResult ar:[SELECT Quote1__c , SUM(Max_Batch__c)sumMax FROM Quote_Line_Item__c WHERE Quote1__c IN :quoteIds GROUP BY Quote1__c]) {
      Quote__C qu=new quote__c();
      qu.id=(Id)ar.get('Quote1__c');
      qu.Potential__c=(Decimal)ar.get('sumMax');
       quotes.add(qu);
       list<quote__c> quo = [select id,potential__c from quote__c where id IN :quotes limit 1 for update]; 
       update quo;
}
if(quotes.isempty() == false)
    {
  update quotes;
  }
}

Best Answer

Since your trigger code isn't commented, it is unclear exactly what your intent is but I'll take a shot at correcting and commenting it.

trigger quotepotential on Quote_Line_Item__c (after insert, after update) {

    // the IDs of quotes which we are going to populate the potential amount on
    Set<Id> quoteIds = new Set<Id>();

    // the list of Quote__c records which will be updated with the amount
    List<Quote__c> quotesToUpdate = new List<Quote__c>();

    // collect the ID value of the related Quote
    for (Quote_Line_Item__c record: Trigger.new) {
        if (record.Quote1__c != null) {
            quoteIds.add(record.Quote1__c);
        }
    }

    // use the quoteIDs set to get the aggregate sum per Quote1__c
    for (AggregateResult ar : [SELECT Quote1__c
                                    , SUM(Max_Batch__c)sumMax 
                                FROM Quote_Line_Item__c
                                WHERE Quote1__c IN :quoteIds 
                                GROUP BY Quote1__c]) {

        // create an sObject to be used for the update operation
        Quote__c qu = new Quote__c(Id = (Id)ar.get('Quote1__c'));
        qu.Potential__c = (Decimal)ar.get('sumMax');

        // add this sObject to the updates list
        quotesToUpdate.add(qu);

        /* Not sure why this code is here - it appears to not belong */
        /* P.S. avoid DML operations in loops in all cases */
        // list<quote__c> quo = [select id,potential__c from quote__c where id IN :quotes limit 1 for update]; 
        // update quo;
    }

    // if there's work to be done, do it.
    if (!quotesToUpdate.isEmpty()) {
        update quotesToUpdate;
    }
}
Related Topic