function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BobBob 

Trigger Adding if statement to check LastDateTimeModified greater than created DateTime field

I have the following code below that i'm trying to add and if statement to check to see if a record has been recently modified by the last date and time being greater than the created date.  I also tried to and it to where I can check for old values and new vlalues but nothing seems to work. I would like to remove the old field value section of the code and add a check for last modified date and time. If the record has the newest last modifiied date and time then check the Primary Quote checkbox. Any help would be greatly appreciated. 
 
trigger MarkPrimaryQuote on Quote (before insert, before update) {
    List<Quote> quoteListToUpdate = new List<Quote>();

    Set<Id> opppIds = new Set<Id>();
    for(Quote qRec : Trigger.new){
        if(qRec.OpportunityId!= null){
            opppIds.add(qRec.OpportunityId);
        }
    }
    Map<Id,Opportunity> mapOfOpps = new Map<Id,Opportunity>([Select id,(Select Primary_Quote__c from Quotes) from Opportunity where Id IN :opppIds]);
    if(trigger.isInsert){
        for(Quote qRec : Trigger.new){
            qRec.Primary_Quote__c = true;
        }
        for(Opportunity opp : mapOfOpps.values()){
            for(Quote existingQuote : opp.Quotes){
                existingQuote.Primary_Quote__c =false;
                quoteListToUpdate.add(existingQuote);
            }
        }
        if(quoteListToUpdate.size() > 0){
            update quoteListToUpdate;
        }
    }
     if(trigger.isUpdate){
        for(Quote qRec : Trigger.new){
  // I would like to remove this section and add a check for last date and time modified         
     Quote oldQuote = trigger.oldMap.get(qRec.id);
            if(oldQuote.Stage__c != qRec.Stage__c || oldQuote.Amount__c !=      qRec.Amount__c || oldQuote.Discount__c != qRec.Discount__c ){
                qRec.Primary_Quote__c =true;

                if(qRec.OpportunityId!= null && mapOfOpps.containsKey(qRec.OpportunityId)){
                    for(Quote existingQuote : mapOfOpps.get(qRec.OpportunityId).Quotes){
                        if(existingQuote.id != qRec.id){
                            quoteListToUpdate.add(existingQuote);
                        }
                    }
                }
            }
        }
        if(quoteListToUpdate.size() > 0){
            update quoteListToUpdate;
        }
    }
}

 
Sunil MadanaSunil Madana
Hello bob, please try the below code and let me know if it works.
trigger MarkPrimaryQuote on Quote (before insert, before update) {
    List<Quote> quoteListToUpdate = new List<Quote>();
    
    Set<Id> opppIds = new Set<Id>();
    for(Quote qRec : Trigger.new){
        if(qRec.OpportunityId!= null){
            opppIds.add(qRec.OpportunityId);
        }
    }
    Map<Id,Opportunity> mapOfOpps = new Map<Id,Opportunity>([Select id,(Select Primary_Quote__c from Quotes) from Opportunity where Id IN :opppIds]);
    if(trigger.isInsert){
        for(Quote qRec : Trigger.new){
            qRec.Primary_Quote__c = true;
        }
        for(Opportunity opp : mapOfOpps.values()){
            for(Quote existingQuote : opp.Quotes){
                existingQuote.Primary_Quote__c =false;
                quoteListToUpdate.add(existingQuote);
            }
        }
        if(quoteListToUpdate.size() > 0){
            update quoteListToUpdate;
        }
    }
    if(trigger.isUpdate){
        for(Quote qRec : Trigger.new){
            // I would like to remove this section and add a check for last date and time modified         
            
            if (qRec.LastModifiedDate == qRec.CreatedDate) {
            	system.debug('***Date/Time is the same***');
            } else {
                system.debug('***Date/Time is the different***');
            }
            /*Quote oldQuote = trigger.oldMap.get(qRec.id);
            if(oldQuote.Stage__c != qRec.Stage__c || oldQuote.Amount__c != qRec.Amount__c || oldQuote.Discount__c != qRec.Discount__c ){
                qRec.Primary_Quote__c =true;
                
                if(qRec.OpportunityId!= null && mapOfOpps.containsKey(qRec.OpportunityId)){
                    for(Quote existingQuote : mapOfOpps.get(qRec.OpportunityId).Quotes){
                        if(existingQuote.id != qRec.id){
                            quoteListToUpdate.add(existingQuote);
                        }
                    }
                }
            }*/
        }
        if(quoteListToUpdate.size() > 0){
            update quoteListToUpdate;
        }
    }
}
BobBob
Good afternoon, This did not work. The insert worked but the update did not change the checkbox when a quote had a new last modified date.