You need to sign in to do that
Don't have an account?
Disha
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
Can anyone please tell me how to write a trigger for this.
Thanks,
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;
}
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.
-Thanks
Ashlekh Gera