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 

Update custom quote field checkbox checked if on specific Opportunity record type

I have a trigger that should only update quotes if the opportunity record type is 'NA Opportunity Record Type' . I've tried to add it into the trigger but nothing has worked. Any help would be greatly appreciated. 

 



trigger 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,Opportunity.RecordType.Name   from Quote WHERE Oracle_Quote__c IN : oracleQuoteList]) {
        qRec.Primary_Quote__c =false;
        qRec.Opportunity.RecordType.Name ='NA Opportunity Record Type';
        quoteListToUpdate.add(qRec);    
    }
//    
    if(quoteListToUpdate != null && quoteListToUpdate .size() > 0) {
        update quoteListToUpdate;
    }

 }

 
Deepak Kumar ShyoranDeepak Kumar Shyoran
Hi Bob,

When you actually wants to update this quote button, every time when a new record of Quote object with Opportunity type = "NA Opportunity Record Type" or there will be some other conditions.

If you can let me know this then I'll able to help you. 
BobBob
Only when a new quote is created from an opportunity with the record type of "NA Opportunity Record Type", my trigger should make the Primary_Quote__c checkbox true. If there is more than one quote created on the opportunity it checks the most recent quote's Primary_Quote__c checkbox. I hope this makes sense.
Deepak Kumar ShyoranDeepak Kumar Shyoran
trigger Trigger_MarkPrimaryQuote on Quote (before insert) { 
    List<Id> opptyList = new List<Id>();
    for(Quote qRec : Trigger.New)
        opptyList.add(qRec.OpportunityId);
        
    Map<Id,Opportunity> opptyMap = new Map<Id,Opportunity>([Select Id, RecordType.Name from Opportunity where Id IN :opptyList]) ;
    for(Quote qRec :Trigger.New ) {
        if(opptyMap.containsKey(qRec.OpportunityId) && opptyMap.get(qRec.OpportunityId).RecordType.Name == 'NA Opportunity Record Type')
            qRec.Primary_Quote__c = true;
    }

Use above code to check Primary_Quote__c checkbox on Quote every time when a new Quote whose Opportunity type is 'NA Opportunity Record Type' get created.
 
Please let me know if you need more assist on this.
BobBob
Choudhary, your trigger works good, but i've been trying to tweak it so it only checks the newest qoute create primary qoute check box and uncheck all older qoutes. I havent any success.