You need to sign in to do that
Don't have an account?
VICKY_SFDC
trigger on new opportunity and Old account
Assume In an account opportunity is there then 2nd opportunity its pop up an An error else if no opportunity is there then automatic an opportunity will be created?
Can you try the below code once
trigger opptri on Opportunity (before insert) {
List<Id> accID = new List<Id>();
for(Opportunity c : trigger.new){
if(c.accountId != null){
accID.add(c.accountId);
}
}
AggregateResult[] groupedResults
= [SELECT Accountid,count(id) FROM Opportunity where AccountId in :accID Group BY AccountId];
map<id,integer> accoountmap= new map<id,integer>();
for (AggregateResult ar : groupedResults) {
accoountmap.put((id)ar.get('Accountid'),(integer)ar.get('expr0'));
}
for(Opportunity o: trigger.new)
{
system.debug(accoountmap.get(o.AccountId));
if(accoountmap.get(o.AccountId)>=1)
{
system.debug('inside check');
o.adderror('cannot have more opportunities under this account');
}
}}
and in case if this helps please choose this as the best answer so that it can be used by others in the future.
Regards,
Anutej