You need to sign in to do that
Don't have an account?
Venkateswarlu P
when we are inserting new opportunity for an account, that optycount field value in Account objeet should increment , when we are deleting opportunity corresponding account optycount field value should decrement.
public static void accountOptyCount(List<Opportunity> optyList){
List<Account> accList=new List<Account>();
Set<Id> optyIds = new Set<Id>();
For(Opportunity op:optyList){
optyIds.add(op.AccountId);
System.debug('optyIds List=======>: '+optyIds);
}
accList=[SELECT Id,optyCount__c,(SELECT Id from Opportunities) from Account where Id in:optyIds];
System.debug('accList List=======>: '+accList);
For(Account a:accList){
a.optyCount__c=a.Opportunities.size();
System.debug('Opportunity count=======>: '+a.optyCount__c);
/* Decimal sum=0;
For(Opportunity opp:a.Opportunities){
sum=sum+1;
System.debug('Sum=======>: '+sum);
}
a.optyCount__c=sum; */
}
update accList;
}
===========
If(Trigger.isBefore && Trigger.isInsert){
Opportunity_Trigger_handler.accountOptyCount(Trigger.new);}
List<Account> accList=new List<Account>();
Set<Id> optyIds = new Set<Id>();
For(Opportunity op:optyList){
optyIds.add(op.AccountId);
System.debug('optyIds List=======>: '+optyIds);
}
accList=[SELECT Id,optyCount__c,(SELECT Id from Opportunities) from Account where Id in:optyIds];
System.debug('accList List=======>: '+accList);
For(Account a:accList){
a.optyCount__c=a.Opportunities.size();
System.debug('Opportunity count=======>: '+a.optyCount__c);
/* Decimal sum=0;
For(Opportunity opp:a.Opportunities){
sum=sum+1;
System.debug('Sum=======>: '+sum);
}
a.optyCount__c=sum; */
}
update accList;
}
===========
If(Trigger.isBefore && Trigger.isInsert){
Opportunity_Trigger_handler.accountOptyCount(Trigger.new);}
You need to execute your trigger on "After Insert" and "After Delete" event Because "before Event" will get execute before record commit to data base. Here in your case , you are query data from data base so as per the data base you have 5 opportunity only in "before Insert", if you execute your logic in "after insert" event then your new opportunity will get commint to data base so while querying info from data base it will return 6 opporutnity.
Here are few links which will help you to understand the trigger execution and salesforce transaction.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm
Thanks,
Keyur Modi
All Answers
As per the below code your trigger will get only executed in before Insert event. you have to call your helped class in Delete event as well.
Please check and let me know if I can help you further.
Thanks,
Keyur Modi
Hope you have created trigger on Opportunity object and optyCount__c is Number field on Account object
Here, you will need after insert and delete because you have to update count on other object so before trigger won't work here. Kindly modify your trigger as above and check once.
Regards,
Sagar
Let say Account has 5 opportunities.created one more opportunity for the same account.The optyCount__c value need to 6,but it showing 5 only.
If we delete the opportunity the optyCount__c value working.
is there any issue with the code.
You need to execute your trigger on "After Insert" and "After Delete" event Because "before Event" will get execute before record commit to data base. Here in your case , you are query data from data base so as per the data base you have 5 opportunity only in "before Insert", if you execute your logic in "after insert" event then your new opportunity will get commint to data base so while querying info from data base it will return 6 opporutnity.
Here are few links which will help you to understand the trigger execution and salesforce transaction.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm
Thanks,
Keyur Modi