You need to sign in to do that
Don't have an account?
Bob
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; } }
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;
}
}
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;
}
}
which is at line 45