+ Start a Discussion
BobBob 

Trigger to add before update gets error

I have a trigger that I am trying to add a before update to a before insert trigger but i keep getting the following error.  What can I change on this code to make it allow insert and updates?

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger MarkPrimaryQuote caused an unexpected exception, contact your administrator: MarkPrimaryQuote: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0Q0n0000000DRrLCAW; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0Q0n0000000DRrL) is currently in trigger MarkPrimaryQuote, therefore it cannot recursively update itself: []: Trigger.MarkPrimaryQuote: line 19, column 1
 
//Created by Bob Poliquin 5/12/2016
// Updated the soql query to identify last modified date. 5/12/2016

trigger MarkPrimaryQuote on Quote (before insert,before update) {
    List<Datetime> oracleQuoteList = new List<Datetime>();
    for(Quote qRec : Trigger.new) {
   
        qRec.Primary_Quote__c = true;
        oracleQuoteList.add(qRec.LastModifiedDate);      
    }
    
    
    List<Quote> quoteListToUpdate = new List<Quote>();
    for(Quote qRec : [SELECT id,Primary_Quote__c,CreatedDate from Quote WHERE LastModifiedDate = TODAY AND HOUR_IN_DAY(LastModifiedDate) > 1]) {
        qRec.Primary_Quote__c =false;
        quoteListToUpdate.add(qRec);    
    }
         if(quoteListToUpdate != null && quoteListToUpdate .size() > 0) {
        update quoteListToUpdate;
    }
    
}

 
SotosSotos
Hi Bob,

I am trying to understand the logic in the trigger.
Do you want the Primary_Quote__c field to be set to 'true' for any new Quotes and 'false' to any Quotes that get updated?
 
BobBob
If an opportunity only has one quote then the primary quote checkbox should remain checked on all updates. If there are more then one quote in the list then the most recent quote should have the checkbox checked. I hope this makes sense.