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
DishaDisha 

Restrict user from adding more than one quote at opportunity.

I have one object opportunity that is the  parent of object "BigMachines__Opportunity__c". Whenever a user insert more than one quote for a single pportunity, error should be thrown. I cant use roll up summary field.
Can anyone please tell me how to write a trigger for this.

Thanks,
Disha
Ketankumar PatelKetankumar Patel
Hi Disha, I am not sure how much you are comfortable with trigger but you should create a trigger on BigMachines__Opportunity__c object and on before trigger event check if opportunity already has quote then don't allow insertion else allow insertion. 
CloudGeekCloudGeek
Hello Disha,

Try the trigger like below, see if that helps for your scenario.


trigger oneQuotePerOppty on Quotes__c (before insert) 
{
    Set<Id>  facIDs = new Set<Id>();
    List<Quotes__c>  quotesTOCreate = new List<Quotes__c>();
    Map<Id, List<Quotes__c>> mapOptyToQuotes = new Map<Id, List<Quotes__c>>();
    
    for(Quotes__c Q : Trigger.new)        
                    {            
                         facIDs.add(Q.BMOppty__r.Id);                                                       
                    }              

    if(!facIDs.isEmpty())
     {         
         List<BigMachines_Opportunity__c> Opttys = [SELECT Id,Name,(SELECT Id FROM Quotes__r) FROM BigMachines_Opportunity__c WHERE Id IN : facIDs];
        
        for(BigMachines_Opportunity__c op : Opttys)
        {
            
               mapOptyToQuotes.put(op.Id,op.Quotes__r);  
        }
     
    }

if(!mapOptyToQuotes.isEmpty()){
for(Quotes__c Q : Trigger.new)
{
    
    if(mapOptyToQuotes.containsKey(Q.BMOppty__r.Id))
    {
        List<Quotes__c> qts = new List<Quotes__c>(mapOptyToQuotes.get(Q.BMOppty__r.Id));
        if(qts.size() > 0)
        {
            Q.addError('Cannot Have more than ONE Quote');
        }
        else
        {
            quotesTOCreate.add(Q);
        }
    
    }
}
    }
                
//insert quotes

    insert quotesTOCreate;     
     

}
AshlekhAshlekh
Hi Disha,

Please make necessary changes in code. I am considerring here is Opportunity and Quote Object. Opportunity is parent and Quote is child.

1) No one can create a Quote on opportunity if opportunity have already one quote.
 
trigger oneQuotePerOppty on Quotes__c (before insert) 
{
    Set<Id>  OpportunityId = new Set<Id>();
    Set<Id> OpptyContainsQuote = new SET<id>();
    
    for(Quotes__c Q : Trigger.new)        
    {            
        OpportunityId.add(Q.opportunityId);                                                       
    }              

    if(!OpportunityId.isEmpty())
     {         
         List<Opportunity> Opttys = [SELECT Id,Name,(SELECT Id FROM Quotes__r) FROM opportunity WHERE Id IN : OpportunityId];
        
        for(Opportunity__c op : Opttys)
		{
			if(op.Quotes__r.size()>0)
				OpptyContainsQuote.put(op.Id);  
         }
    }

	if(!OpptyContainsQuote.isEmpty())
	{
		for(Quotes__c Q : Trigger.new)
		{
			if(OpptyContainsQuote.contains(Q.OpportunityId))
			{
				Q.addError('Cannot Have more than ONE Quote');
			}
		}
	}
 }

-Thanks
Ashlekh Gera