+ Start a Discussion
bhanu_prakashbhanu_prakash 

update highest amount on account from contact acheive it using trigger

Hi Team,

I have amount__c in both contact and account . if account has many contacts  and i need to update highest amount of contacts and update that into account amount field ?

Ex : ABC account   Amount__c  =

       Contact1 :  T -- Amount__c = 500$
       Contact2 :  A -- Amount__c = 700$
       Contact3 :  B -- Amount__c =300$

now account amount__c need to update with 700$ . how can i acheive it using trigger
Best Answer chosen by bhanu_prakash
GauravendraGauravendra
Hi Bhanu,

Yes, you are right. I didn't cover the delete scenario. Try this new code.
trigger HighestAmountContact on Contact (after insert,after update, after delete) {
    List<Account> updAcc = new List<Account>();
    Map<id,account> mapacc = new Map<id,account>( [select id,name,Amount__c,(select id,Amount__c from contacts) from account]);
    if(Trigger.isDelete) {
        List<Contact> oldContatcs = Trigger.Old;
        for(Contact con: oldContatcs) {
        	Id accID = con.AccountID;
            Decimal highAmnt = 0;
            Account acc = mapacc.get(accID);
            for(Contact c: acc.Contacts) {
                if(highAmnt < c.Amount__c ) {
                    highAmnt = c.Amount__c;
                }
            }
            acc.Amount__c = highAmnt;
            updAcc.add(acc);
        }
        update updAcc;
    }
    if(Trigger.isInsert || Trigger.isUpdate) {
        List<Contact> newContacts = Trigger.New;
        for(Contact con: newContacts) {
            Id accID = con.AccountID;
            Decimal highAmnt = con.Amount__c;
            if(highAmnt == null) {
                highAmnt = 0;
            }
            Account acc = mapacc.get(accID);
            for(Contact c: acc.Contacts) {
                if(highAmnt < c.Amount__c ) {
                    highAmnt = c.Amount__c;
                }
            }
            acc.Amount__c = highAmnt;
            updAcc.add(acc);
        }    
        update updAcc;
    }
    
}
Let me know, if this solves your problem.
Hope this helps.

All Answers

GauravendraGauravendra
Hi Bhanu,

Try the below code:
trigger HighestAmountContact on Contact (after insert,after update) {
    List<Contact> newContacts = Trigger.New;
    List<Account> updAcc = new List<Account>();
    Map<id,account> mapacc = new Map<id,account>( [select id,name,Amount__c,(select id,Amount__c from contacts) from account]);
    for(Contact con: newContacts) {
        Id accID = con.AccountID;
        Decimal highAmnt = con.Amount__c;
        if(highAmnt == null) break;
        Account acc = mapacc.get(accID);
        for(Contact c: acc.Contacts) {
            if(highAmnt < c.Amount__c ) {
                highAmnt = c.Amount__c;
            }
        }
        acc.Amount__c = highAmnt;
        updAcc.add(acc);
    }
    update updAcc;
}

Hope this helps.
bhanu_prakashbhanu_prakash
Thanks Gauravendra, It working but issue 
       Contact1 :  T -- Amount__c = 500$
       Contact2 :  A -- Amount__c = 700$
       Contact3 :  B -- Amount__c =300$

if iam trying to delete Contact2 : A then price need to update as  500$ .Iam stuck here please help :) :) 
GauravendraGauravendra
Hi Bhanu,

Yes, you are right. I didn't cover the delete scenario. Try this new code.
trigger HighestAmountContact on Contact (after insert,after update, after delete) {
    List<Account> updAcc = new List<Account>();
    Map<id,account> mapacc = new Map<id,account>( [select id,name,Amount__c,(select id,Amount__c from contacts) from account]);
    if(Trigger.isDelete) {
        List<Contact> oldContatcs = Trigger.Old;
        for(Contact con: oldContatcs) {
        	Id accID = con.AccountID;
            Decimal highAmnt = 0;
            Account acc = mapacc.get(accID);
            for(Contact c: acc.Contacts) {
                if(highAmnt < c.Amount__c ) {
                    highAmnt = c.Amount__c;
                }
            }
            acc.Amount__c = highAmnt;
            updAcc.add(acc);
        }
        update updAcc;
    }
    if(Trigger.isInsert || Trigger.isUpdate) {
        List<Contact> newContacts = Trigger.New;
        for(Contact con: newContacts) {
            Id accID = con.AccountID;
            Decimal highAmnt = con.Amount__c;
            if(highAmnt == null) {
                highAmnt = 0;
            }
            Account acc = mapacc.get(accID);
            for(Contact c: acc.Contacts) {
                if(highAmnt < c.Amount__c ) {
                    highAmnt = c.Amount__c;
                }
            }
            acc.Amount__c = highAmnt;
            updAcc.add(acc);
        }    
        update updAcc;
    }
    
}
Let me know, if this solves your problem.
Hope this helps.
This was selected as the best answer
bhanu_prakashbhanu_prakash
Thanks Gauravendra. issue got resloved  :) :)