salesforce - To get the number of opportunities in an Account -


how count number of opportunities related account,

total number of opportunities field on account should increment/decrement when opportunity created/deleted.

how solve it, pl me sample code.

actually, don't need write code if need count of opportunities related account. create “rollup/summary” field type on account. evaluate opportunity object, , run “count” operation. that’s it!

upd: if need solve trigger looks this:

trigger countopportunitiesonaccount on opportunity (after insert, after delete){     set<id> aid = new set<id>();     if(trigger.isinsert || trigger.isdelete || trigger.isundelete){         for(opportunity opp : trigger.new){             aid.add(opp.accountid);         }         updateaccounts(aid);     }      if(trigger.isdelete){         for(opportunity opp : trigger.old){             aid.add(opp.accountid);         }         updateaccounts(aid);     }      private void updateaccounts(set<id> accids){         list<account> accs = [select id, opportunitiesamount account id in :accids];         list<opportunity> opps = [select id opportunity accountid in :accids];         for(account : accs){             a.opportunitiesamount = opps.size();         }         update accs;     } } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -