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 

Quote trigger to update checkbox on newest record created

I have a trigger below that works but i need it to check a Primary_Quote__c checkbox on the newest created quote on an opportunity with the record type of "NA Opportunity Record Type".  How would i accomplish adding a filter to only check the newest Primary_Quote__c quote while unchecking previous qoutes?


 
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;
    
    
    }
    }

 
Arvind BalijepalliArvind Balijepalli
see if below works :


trigger Trigger_MarkPrimaryQuote on Quote (before insert) { 
 

   List<Id> opptyList = new List<Id>();
   

//to capture existing Quotes 

   list<Quote> existingQuotes = new list<Quote>();
 
   Boolean markrecord ;

 

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]) ;


   //We are capturing the already existing Quotes - with the associated Opportunity , since we know OppID will not be changing .
       
       existingQuotes = [select id,Primary_Quote__c from Quote where opportunityid 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;
    
          markrecord = true;



    
    }
 

 }


if(markrecord)

{
  for(Quote q : existingQuotes)

{
   q.Primary_Quote__c = false;
}

}
BobBob
I am getting the following error. unexpected token: if at line 50 column 0. 
Arvind BalijepalliArvind Balijepalli
can you paste the recent code you have
BobBob
trigger Trigger_MarkPrimaryQuote on Quote (before insert, before update) { 
    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;
    }
    
   
    
    List<Quote> quoteListToUpdate = new List<Quote>();
    for(Quote qRec : [SELECT id,Primary_Quote__c,Name  from Quote WHERE Name IN : opptyList]) {
        qRec.Primary_Quote__c =false;
        quoteListToUpdate.add(qRec);    
    }
    
    if(quoteListToUpdate != null && quoteListToUpdate .size() > 0) {
        update quoteListToUpdate;
    }
}
Arvind BalijepalliArvind Balijepalli
when are you getting the error ? when saving it or when the trigger executes ?
BobBob
When I try to save it in salesforce. 
Arvind BalijepalliArvind Balijepalli
pls see to it that , there are no spaces or special characters in your code , iam able to save it with no issues at my end here.
BobBob
I tried saving the code below in Eclipse and it gives me Unexpected sytax EOF at if error.

which is at line 45
 
trigger Trigger_MarkPrimaryQuote on Quote (before insert) { 
 

   List<Id> opptyList = new List<Id>();
   

//to capture existing Quotes 

   list<Quote> existingQuotes = new list<Quote>();
 
   Boolean markrecord ;

 

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]) ;


   //We are capturing the already existing Quotes - with the associated Opportunity , since we know OppID will not be changing .
       
       existingQuotes = [select id,Primary_Quote__c from Quote where opportunityid 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;
    
          markrecord = true;
 }
 
}


if(markrecord)

{
  for(Quote q : existingQuotes)

{
   q.Primary_Quote__c = false;
}

}