function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
priya bhawna shettypriya bhawna shetty 

Trigger related to Account and opportunity Amount

hi guys

i want to write a trigger on opportunity object,i.e we have Amount field in opportunity,so i want to display the maximum Amount of all the amounts i get from opportunity in Account object,having custom field as max amount in Acount object...

thnks

sfdcsushilsfdcsushil

In After Insert Method of Trigger, You can write one Aggregate query to find maximum amount opportunity related to opportunity account. 

 

This is how you write aggregate queries. 

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm

 

 

Regards,

Sushil

priya bhawna shettypriya bhawna shetty

pls judge this trigger

 

 

 

trigger oppmaxamount1 on opportunity(after insert)
{
List<Account> accList = new List<Account>();
for(opportunity opp: trigger.new){
Account acc = [Select Id from Account where Id =: opp.AccountId];
List<opportunity> oppList = [Select Id,Max(Amount) from opportunity where AccountId =: acc.Id];
system.debug(oppList '--------------------'+oppList );

 

MISSING SOME CODE HERE HOW TO DISPLAY THE OPPORTUNITY LIST


}
update accList;
}

shivakrishna aithashivakrishna aitha
public class Trigger_AccountOpp_AmountCount {
    public static void afterInsert(list<opportunity> opp){
        list<Account> accList = new list<Account>();
        set<id> optyIds = new set<id>();
        for(opportunity o : opp){
            optyIds.add(o.AccountId);
        }
        accList = [select id,name,OptyAmountCount__c,(select id,amount from opportunities) from account where Id in:optyIds];
        decimal sum=0;
        For(Account a:accList){
           for(opportunity op :a.opportunities){
               sum=sum+op.Amount;
           }
            a.OptyAmountCount__c=sum;
              update a;
              }
      }
}