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
Post a Comment