0

if a contact has a custom field as amount and account have a custom field total amount roll up the amount field on account if one account associated have two contacts. 1)first contact amount is 20 2)second contact amount is 30 then total amount will display 50 in account

Below is my code. I display contact amount field in account total amount I don't know how to do the rest of the logic. how can i add both contacts amount and display it in account

trigger CopyPhoneOnContact on Contact (before insert) {

List<String> upcon =new List<String>();
List<account> up=new List<account>();

for(contact c:trigger.new)
{
    upcon.add(c.accountid);
}
List<account> acc=[select phone from account where id in:upcon];

for(account a:acc)
{
    for(contact c:trigger.new)
    {
        if((a.id).equals(c.AccountId))
        {
            a.phone=c.Phone;
            up.add(a);
        }
    }
}
update up;

}
0

I think your issue is that you are trying to do this on the wrong trigger. It will be a lot easier to keep track of which contacts are part of which account if you do the logic in the Account trigger instead:

trigger updateRollUp on Account(before update)
{
    Map<ID, List<Contact>> contacts = new Map<ID, List<Contact>>();
    for(Account accn : trigger.new)
    {
        contacts.put(accn.ID, new List<Contact>();
    }
    for(Contact cont : [select Amount__c from Contact where accountid in :trigger.new])
    {
        contacts.get(cont.accountid).add(cont);
    }
    for(Account accn : trigger.new)
    {
        if(contacts.get(accn.ID).size() > 1)
        {
            accn.Total_Amount__c = 0;
            for(Contact cont : contacts.get(accn.ID))
            {
                accn.Total_Amount__c += cont.Amount__c;
            }
        }
    }
}

Your existing code just needs to only have one copy of each Account in your DML, but that's a relatively simple change, just call update on acc instead of up. Note that you can run into a recursion issue if your account trigger updates child contacts - how exactly this should be handled depends on your business rules, but it can be managed by using static variables, which persist through the whole database transaction.

  • Hi IllusiveBrian , i used your code it displayed this error at line 5 (accn.Contacts = new List<Contact>();)Field is not writeable: Account.Contacts. and please explain the fields Custom_Number__c, Custom_Rollup__c . and I want to use custom fields in account Total_Amount__c and contact is Amount__c. – jitendra pal Aug 20 '17 at 4:44
  • @jitendrapal Sorry, for some reason I thought you could update the detail lists on an object. I've updated the code to use a map instead. Also, I just used generic names for the fields since I didn't see you had put their names in the question, I've updated to use the names you had in the question. – IllusiveBrian Aug 20 '17 at 11:43
  • Hi IllusiveBrian i have used your updated code then displayed this error at line 19. for(Contact cont : contacts.get(accn.ID).Contacts) (Error Variable does not exist: Contacts). – jitendra pal Aug 21 '17 at 15:27
  • @jitendrapal Woops, that shows me for copying and pasting. – IllusiveBrian Aug 21 '17 at 15:33
  • Hi, IllusiveBrian, I was already checked that but amount value does not show in Account objects Please help me and thanks for giving quick responce. – jitendra pal Aug 21 '17 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.