0

I'm looking for a trigger to prevent duplicate quote at opportunity, if there is alaredy a quote at opportunity user will not be able to generate new quote in system i.e. only 1 quote per opprotunity.

I try below code but it's not working

    trigger DuplicateStudentCheck on Quote(before insert) {
     Quote[] studentsList = Trigger.new;
     Set oppSet = new Set();
     for (Quote s: quoteList) {
         oppSet.add(s.Opportunity);
     }

     List duplicateQuoteList = [Select s.Name, s.Opportunity.Name From Quote where s.Opportunity IN: oppSet];
     Set duplicateOpplIds = new Set();
     for (Quote s: duplicateQuoteList) {
         duplicateOpplIds.add(s.Opportunity.Name);
     }
     for (Quote s: quoteList) {
         if (duplicateOpplIds.contains(s.Opportunity.Name)) {
             s.Opportunity.addError('Record already exist');
         }
     }
 }
4
  • have you tried anything? this seems very simple requirement. If you are facing issue then please describe it in detail. Commented Jun 9, 2015 at 8:13
  • I try below code but it's not working Commented Jun 9, 2015 at 8:23
  • trigger DuplicateStudentCheck on Quote (before insert) { Quote[] studentsList = Trigger.new; Set oppSet = new Set(); for(Quote s : quoteList) { oppSet.add(s.Opportunity); } Commented Jun 9, 2015 at 8:23
  • List duplicateQuoteList = [Select s.Name, s.Opportunity.Name From Quote where s.Opportunity IN :oppSet]; Set duplicateOpplIds = new Set(); for(Quote s : duplicateQuoteList) { duplicateOpplIds.add(s.Opportunity.Name); } for(Quote s : quoteList) { if(duplicateOpplIds.contains(s.Opportunity.Name)) { s.Opportunity.addError('Record already exist'); } } } Commented Jun 9, 2015 at 8:23

2 Answers 2

1

Use below trigger. It will do the job.

trigger DuplicateStudentCheck on Quote (before insert){

 Quote[] quoteList = new list<Quote>();
     Set<id> oppSet = new Set<id>();
     for (Quote s: Trigger.new) {
         oppSet.add(s.Opportunity.id);
     }

     List<opportunity> oppList = [select id, name, (select id, opportunityId from quotes) from opportunity where Id IN: oppSet];
     for (opportunity op : oppList) {
         quoteList.add(op.Quotes);
  }
     for(Quote q : Trigger.new){
      if(quoteList != null){
   q.addError('Record already exist');
      }
     }
}

Please mark as best answer if it helps.

1
  • It always show "Record already exist" error Commented Jun 11, 2015 at 6:48
0

You might be able to do this without a trigger.

  • Create a rollup field on Opportunity 'No. of Quotes'.
  • Create a validation rule on Opportunity 'No. of Quotes must be <= 1'.

I haven't tested this but I have vague memories of doing similar in the past.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .