You need to sign in to do that
Don't have an account?
Mandapaka Raghu 9
one account record should not contain more than three opportunity records
Hi
i have a scenario like one account record does not contain more than three opportunity records, and i wrote a following trigger, but it is not working for inserting the records one by one , but this trigger is working for inserting the bulk records through list.
trigger Opportunity_Validation_Trigger2 on Opportunity (before insert) {
list<Opportunity> opportunities=trigger.new;
map<id,list<Opportunity>> AccOppMap=new map<id,list<Opportunity>>();
for(Opportunity opps:opportunities){
if(opps.AccountId!=null){
if(AccOppMap.containsKey(opps.AccountId)){
list<Opportunity> oppslist= AccOppMap.get(opps.AccountId);
oppslist.add(opps);
}
else
{
list<Opportunity> oppslist=new list<Opportunity>();
oppslist.add(opps);
AccOppMap.put(opps.AccountId, oppslist);
}
}
}
for(opportunity op:trigger.new){
if(op.AccountId!=null){
integer count=AccOppMap.get(op.AccountId).size();
if(count>=3){
op.addError('an account should not contain more than 3 opportunities');
}
}
}
}
can any one please help me, the scenerio is it should work for inserting the records one after one and through list using execute anonomous window.
i have a scenario like one account record does not contain more than three opportunity records, and i wrote a following trigger, but it is not working for inserting the records one by one , but this trigger is working for inserting the bulk records through list.
trigger Opportunity_Validation_Trigger2 on Opportunity (before insert) {
list<Opportunity> opportunities=trigger.new;
map<id,list<Opportunity>> AccOppMap=new map<id,list<Opportunity>>();
for(Opportunity opps:opportunities){
if(opps.AccountId!=null){
if(AccOppMap.containsKey(opps.AccountId)){
list<Opportunity> oppslist= AccOppMap.get(opps.AccountId);
oppslist.add(opps);
}
else
{
list<Opportunity> oppslist=new list<Opportunity>();
oppslist.add(opps);
AccOppMap.put(opps.AccountId, oppslist);
}
}
}
for(opportunity op:trigger.new){
if(op.AccountId!=null){
integer count=AccOppMap.get(op.AccountId).size();
if(count>=3){
op.addError('an account should not contain more than 3 opportunities');
}
}
}
}
can any one please help me, the scenerio is it should work for inserting the records one after one and through list using execute anonomous window.
I solved this scenerio, The below code is working properly.
trigger Opportunity_Validation_Trigger2 on Opportunity (before insert) {
Map<ID,Integer> dbAccountIdToCountOfOpps = new Map<id,integer>();
for(Opportunity opp:trigger.new){
if(opp.AccountId!=null){
dbAccountIdToCountOfOpps.put( opp.AccountId, NULL );
}
}
for( Account dbAccount : [SELECT ID, ( SELECT ID FROM Opportunities ) FROM Account WHERE ID IN : dbAccountIdToCountOfOpps.keySet() ] )
{
dbAccountIdToCountOfOpps.put( dbAccount.Id, dbAccount.Opportunities.size() );
}
for( opportunity op:trigger.new ) {
if(op.AccountId!=null){
integer existingCount = dbAccountIdToCountOfOpps.get(op.AccountId); // 3
if( existingCount > 2 ) {
op.addError('an account should not contain more than 3 opportunities');
}
else {
dbAccountIdToCountOfOpps.put( op.AccountId, existingCount + 1 );
}
}
}
}
Thanks and Regards,
Raghu M
All Answers
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Thnaks for your reponse, but it is not working for bulk of records.
i inserted list of records for same account. it is not working in that case.
List<Opportunity> opps=new List<Opportunity>();
for (integer i=0;i<10;i++){
Opportunity op=new Opportunity();
op.Name='Testing nov 22'+i;
op.accountId='0012v00002RGlhtAAD';
op.stageName='Prospecting';
op.closeDate= system.today()+15;
opps.add(op);
}
insert opps;
i inserted this list of records for the same account through execute anonomous window. just check it one you get an idea.
Regards,
Raghu M.
I have gone through your question as well as your code and created a trigger. Please give it a try -
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Greetings to you!
I have gone through your query please try below code:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Thanks for your reponse, but it is not working for bulk of records.
Please check it once by inserting a list of opportinities for new account record.
Thanks and Regards,
Raghu M.
I solved this scenerio, The below code is working properly.
trigger Opportunity_Validation_Trigger2 on Opportunity (before insert) {
Map<ID,Integer> dbAccountIdToCountOfOpps = new Map<id,integer>();
for(Opportunity opp:trigger.new){
if(opp.AccountId!=null){
dbAccountIdToCountOfOpps.put( opp.AccountId, NULL );
}
}
for( Account dbAccount : [SELECT ID, ( SELECT ID FROM Opportunities ) FROM Account WHERE ID IN : dbAccountIdToCountOfOpps.keySet() ] )
{
dbAccountIdToCountOfOpps.put( dbAccount.Id, dbAccount.Opportunities.size() );
}
for( opportunity op:trigger.new ) {
if(op.AccountId!=null){
integer existingCount = dbAccountIdToCountOfOpps.get(op.AccountId); // 3
if( existingCount > 2 ) {
op.addError('an account should not contain more than 3 opportunities');
}
else {
dbAccountIdToCountOfOpps.put( op.AccountId, existingCount + 1 );
}
}
}
}
Thanks and Regards,
Raghu M