+ Start a Discussion
Guru Vemuru 1Guru Vemuru 1 

I want to restrict one order for quote?

Hello I am Guru , a Freasher..I want to restrict creation of order from quote if a quote name is already present in order ....I mean to avoid multiple order for same quote..Please help me out
These is my code .Please Help me.

trigger RestrictMultipleOrder on Order (before insert)
{
   List<Order> ord=[Select  Quote__c from Order];
    For(Order o:Trigger.New)
    {
          if(o.Quote__c==ord)
        {
           o.addError('Duplicate Order cannot be generated');
        }
    }
}
Best Answer chosen by Guru Vemuru 1
Deepak Maheshwari 7Deepak Maheshwari 7

Hi,

 

If the above solution works for you then please mark it as the best answer

All Answers

Deepak Maheshwari 7Deepak Maheshwari 7
trigger RestrictMultipleOrder on Order (before insert)
{
   List<Order> ord=[Select  Quote__c from Order];
   Set<Id> quoteIds = new Set<Id>();
    For(Order o:Trigger.New)
    {
		quoteIds.add(o.Quote__c);
    }
  
    Map<Id,Quote> quoteMap = new Map<Id,Quote>();
    for(Quote q : [Select Id,(Select Id,Name from Quote__c) from Quote where Id in :quoteIds]){
        quoteMap.put(q.Id,q);
    }
  
    for(Order q : Trigger.new){
        if(quoteMap.containsKey(q.Quote__c)){
            if(quoteMap.get(q.Quote__c).Order__c != null && quoteMap.get(q.Quote__c).Order__c.size()>0){
                q.addError('Cannot add Log Sheet for the Case ');
            }
        }
    }
    
}

Please try the above trigger and please modify the API name accordingly.
Deepak Maheshwari 7Deepak Maheshwari 7

Hope the given solution works for you!

If not please let me know.

Guru Vemuru 1Guru Vemuru 1
Thank  you Deepak Maheshwari 7 for your support
Deepak Maheshwari 7Deepak Maheshwari 7

Hi,

 

If the above solution works for you then please mark it as the best answer

This was selected as the best answer