You need to sign in to do that
Don't have an account?
Raj Pandey 29
write a trigger for creating only one opportunity linked with an account in a single day
trigger onlyoneaccountopportunitycreate on Opportunity (before insert) {
Set<id> accId = new Set<id>(); // Always try to use set
for(opportunity opp : trigger.new)
{
if(opp.AccountId != null)
{
accId.add(opp.AccountId);
}
}
Map<Id,Account> mapAccount = new Map<Id,Account> ( [select id,(select id from opportunities) from account where id IN : accId ]) ;
for(opportunity opp:trigger.new)
{
if( opp.AccountId != null && mapAccount.containsKey(opp.AccountId) )
{
Account acc = mapAccount.get(opp.AccountId);
if(acc.opportunities.size() > 0 )
{
opp.addError('you can not add more then once opportunity for this account');
}
}
}
}
Please see the above code & help me to write a trigger that allow only one opportunity associated with an account in a day.
Set<id> accId = new Set<id>(); // Always try to use set
for(opportunity opp : trigger.new)
{
if(opp.AccountId != null)
{
accId.add(opp.AccountId);
}
}
Map<Id,Account> mapAccount = new Map<Id,Account> ( [select id,(select id from opportunities) from account where id IN : accId ]) ;
for(opportunity opp:trigger.new)
{
if( opp.AccountId != null && mapAccount.containsKey(opp.AccountId) )
{
Account acc = mapAccount.get(opp.AccountId);
if(acc.opportunities.size() > 0 )
{
opp.addError('you can not add more then once opportunity for this account');
}
}
}
}
Please see the above code & help me to write a trigger that allow only one opportunity associated with an account in a day.
All Answers
Just add the created date filter in the opportunity inner query so that it will check if the opportunity is created today only. Please find the updaed code below:
Let me know if you need any further help on this.
Thanks,
Abhishek Bansal.
sir