+ Start a Discussion
BobBob 

Quote Trigger Help

I am trying to figure out how to write  a trigger/apex class that would look through a list of quotes. There is a checkbox (Primary_Quote__c) I need the trigger or class to look through the list of quote. There is a custom field Entitled (Oracle_Quote__c). I need the trigger to look through the list of quotes with the same Oracle Quote Number and automatically check the Primary Quote checkbox on the record with the newest created date, and also uncheck the older quote records. Is this possible? How would I accomplish? Any helpwuold be greatly appreicated. 
Best Answer chosen by Bob
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Bob,

Here, is one time script to update the existing records. You can run this script in the developer console. Try, to run this is sandbox first (to avoid any issues), though I have tested it and it seems to be working fine.

You may have to adjust the code a bit, I have assumed 'Oracle_Quote__c' is a decimal field, change accordingly.
List<Quote> quoteList = [SELECT Primary_Quote__c,Oracle_Quote__c FROM Quote ORDER BY Oracle_Quote__c ASC, Createddate DESC LIMIT 50000];
List<Quote> quoteListToUpdate = new List<Quote>();
Decimal OracleQuote = null; 
for (Quote qRec : quoteList) {
    if(qRec.Oracle_Quote__c != OracleQuote) {
        qRec.Primary_Quote__c = true;
    	quoteListToUpdate.add(qRec);		
    } 
    OracleQuote = qRec.Oracle_Quote__c;
}
    
if(quoteListToUpdate != null && quoteListToUpdate.size() > 0) {
    update quoteListToUpdate;
}
You can cut and paste this trigger on Quote, it will handle the future records automatically.
trigger MarkPrimaryQuote on Quote (before Insert) {
    List<Decimal> oracleQuoteList = new List<Decimal>();
    for(Quote qRec : Trigger.new) {
        qRec.Primary_Quote__c = true;
        oracleQuoteList.add(qRec.Oracle_Quote__c);      
    }
    
    List<Quote> quoteListToUpdate = new List<Quote>();
    for(Quote qRec : [SELECT id,Primary_Quote__c  from Quote WHERE Oracle_Quote__c IN : oracleQuoteList]) {
        qRec.Primary_Quote__c =false;
        quoteListToUpdate.add(qRec);    
    }
    
    if(quoteListToUpdate != null && quoteListToUpdate .size() > 0) {
        update quoteListToUpdate;
    }
}
I advise you to test above both in Sanbox first, then deply to production.
Please, let me know if you need any help.

Thanks, 
Sumit Kumar Singh
 

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Bob, 

How many Quote records are ther in Org? Is it more than 50K ?
Yes, we can achieve this. Pls, let me know the number of quotes records, then I can provide you the appropriate solution.

Thanks,
Sumit Kumar Singh
BobBob
Between January 2015 and December 2015 there were 32,820 records created. Thank you
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Bob,

Here, is one time script to update the existing records. You can run this script in the developer console. Try, to run this is sandbox first (to avoid any issues), though I have tested it and it seems to be working fine.

You may have to adjust the code a bit, I have assumed 'Oracle_Quote__c' is a decimal field, change accordingly.
List<Quote> quoteList = [SELECT Primary_Quote__c,Oracle_Quote__c FROM Quote ORDER BY Oracle_Quote__c ASC, Createddate DESC LIMIT 50000];
List<Quote> quoteListToUpdate = new List<Quote>();
Decimal OracleQuote = null; 
for (Quote qRec : quoteList) {
    if(qRec.Oracle_Quote__c != OracleQuote) {
        qRec.Primary_Quote__c = true;
    	quoteListToUpdate.add(qRec);		
    } 
    OracleQuote = qRec.Oracle_Quote__c;
}
    
if(quoteListToUpdate != null && quoteListToUpdate.size() > 0) {
    update quoteListToUpdate;
}
You can cut and paste this trigger on Quote, it will handle the future records automatically.
trigger MarkPrimaryQuote on Quote (before Insert) {
    List<Decimal> oracleQuoteList = new List<Decimal>();
    for(Quote qRec : Trigger.new) {
        qRec.Primary_Quote__c = true;
        oracleQuoteList.add(qRec.Oracle_Quote__c);      
    }
    
    List<Quote> quoteListToUpdate = new List<Quote>();
    for(Quote qRec : [SELECT id,Primary_Quote__c  from Quote WHERE Oracle_Quote__c IN : oracleQuoteList]) {
        qRec.Primary_Quote__c =false;
        quoteListToUpdate.add(qRec);    
    }
    
    if(quoteListToUpdate != null && quoteListToUpdate .size() > 0) {
        update quoteListToUpdate;
    }
}
I advise you to test above both in Sanbox first, then deply to production.
Please, let me know if you need any help.

Thanks, 
Sumit Kumar Singh
 
This was selected as the best answer
BobBob
I am getting the follwoing error

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Trigger_PrimaryQuoteCheckbox caused an unexpected exception, contact your administrator: Trigger_PrimaryQuoteCheckbox: execution of BeforeInsert caused by: line 17, column 1: Illegal empty right operand for operator '>=' in query 'null'.
BobBob
This is what the trigger looks like now. This is the error. Apex trigger Trigger_PrimaryQuoteCheckbox caused an unexpected exception, contact your administrator: Trigger_PrimaryQuoteCheckbox: execution of BeforeInsert caused by: line 17, column 1: Illegal empty right operand for operator '>=' in query 'null'.

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

  
  
  
  
  
  
  }

 
BobBob
Disregard the last post, I had another trigger active that was giving the error.
BobBob
Sumit Kumar  How would I remove the Oracle Quote field from this trigger and have this trigger only look for the newest quote created? It also needs to have the ability to allow a user to unckeck and check any quotes in the related list.  Any help would be greatly appreciated.