+ Start a Discussion
Carrie Nunemaker 10Carrie Nunemaker 10 

Create a trigger to sum a custom field on the contact and display on the account

I am completely new to Apex and could really use some help with what seems to be a simple task.  I have a formula on the contact, Persona_True__c, that returns a 1 if the custom field "persona" is populated and a 0 if it is blank.

I would like to have a roll-up summary that sums this field at the account level in a field called Related_Personas__c.  

Because we can't roll-up from contact to account I'm reading that this type of action needs to be a trigger, but I don't even know where to start.  Does anyone have any thoughts or expertise they can share to help me out? 
Best Answer chosen by Carrie Nunemaker 10
Anthony McDougaldAnthony McDougald
Good Evening Carrie,
Hope that your day is off to an amazing start. We've constructed a trigger to your specifications. Please test and report back if anything. Hope this helps and may God bless you abundantly.
trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, after update) {

Contact[] cons;
    if (Trigger.isDelete){ 
    cons = Trigger.old;
    }
    else{
    cons = Trigger.new;
    }
Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
   acctIds.add(con.AccountId);
}

Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([SELECT Id, AccountId, Persona_True__c FROM Contact WHERE AccountId IN :acctIds]);

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([SELECT Id, Related_Personas__c FROM Account WHERE Id IN :acctIds]);

for (Account acct : acctsToUpdate.values()) {
Set<Id> conIds = new Set<Id>();
Decimal totalValue = 0;
for (Contact con : contactsForAccounts.values()) {
    if (con.AccountId == acct.Id && con.Persona_True__c != NULL) {
        totalValue += con.Persona_True__c; 
    }
}
acct.Related_Personas__c = totalValue;
}
if(acctsToUpdate.values().size() > 0) {
    update acctsToUpdate.values();
}
}


Best Regards,
Anthony McDougald

All Answers

Anthony McDougaldAnthony McDougald
Good Evening Carrie,
Hope that your day is off to an amazing start. We've constructed a trigger to your specifications. Please test and report back if anything. Hope this helps and may God bless you abundantly.
trigger ContactSumTrigger on Contact (after delete, after insert, after undelete, after update) {

Contact[] cons;
    if (Trigger.isDelete){ 
    cons = Trigger.old;
    }
    else{
    cons = Trigger.new;
    }
Set<ID> acctIds = new Set<ID>();
for (Contact con : cons) {
   acctIds.add(con.AccountId);
}

Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([SELECT Id, AccountId, Persona_True__c FROM Contact WHERE AccountId IN :acctIds]);

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([SELECT Id, Related_Personas__c FROM Account WHERE Id IN :acctIds]);

for (Account acct : acctsToUpdate.values()) {
Set<Id> conIds = new Set<Id>();
Decimal totalValue = 0;
for (Contact con : contactsForAccounts.values()) {
    if (con.AccountId == acct.Id && con.Persona_True__c != NULL) {
        totalValue += con.Persona_True__c; 
    }
}
acct.Related_Personas__c = totalValue;
}
if(acctsToUpdate.values().size() > 0) {
    update acctsToUpdate.values();
}
}


Best Regards,
Anthony McDougald
This was selected as the best answer
Maharajan CMaharajan C
Hi Carie,

Please wirte the below trigger in Contact Object. This trigger will cover all your scenario's:

Use any one of the below Trigger:

Trigger 1:   Based on Formula Field  ==> Persona_True__c we are  calculating the Related Persona's in Account

trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {
     
    Set<Id> accountIds = new Set<Id>();
    Map<Id,Decimal> mapChildTotalAmount = new Map<Id,Decimal>();
    List<Account> accounttoUpdate = new List<Account>();
     
    if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete){
        for(Contact con:trigger.new){
            if(con.AccountId != null){
                accountIds.add(con.AccountId);
            }
        }
    }
     
    if(trigger.isUpdate || trigger.isDelete){
        for(Contact con:trigger.old){
            if(con.AccountId != null){
                accountIds.add(con.AccountId);
            }
        }
    }
     
    if(!accountIds.isEmpty()){
        
        for(AggregateResult agg: [Select AccountId accId, SUM(Persona_True__c) from Contact where AccountId IN : accountIds group by AccountId ]){
            mapChildTotalAmount.put((Id)agg.get('accId'),(Decimal)agg.get('expr0'));
        }
        
        for( Account acc : [Select Id, Related_Personas__c from Account where Id IN :accountIds])
        {
            Decimal totalPersona = mapChildTotalAmount.get(acc.Id);
            acc.Related_Personas__c = totalPersona ;
            accounttoUpdate.add(acc);
        }
    }
    
    if(!accounttoUpdate.IsEmpty())
        {
            update accounttoUpdate;
        }
}



========================   OR  =======================


Trigger 2 : Without using the Formula Field. So no need to create the Formula Field. we can use the below trigger directly with the help of persona__c custom field to calculate the related persona's in Account.

trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {
     
    Set<Id> accountIds = new Set<Id>();
    Map<Id,Decimal> mapChildTotalAmount = new Map<Id,Decimal>();
    List<Account> accounttoUpdate = new List<Account>();
     
    if(trigger.isInsert || trigger.isUpdate || trigger.isUndelete){
        for(Contact con:trigger.new){
            if(con.AccountId != null){
                accountIds.add(con.AccountId);
            }
        }
    }
     
    if(trigger.isUpdate || trigger.isDelete){
        for(Contact con:trigger.old){
            if(con.AccountId != null){
                accountIds.add(con.AccountId);
            }
        }
    }
     
    if(!accountIds.isEmpty()){
        
        for(AggregateResult agg: [Select AccountId accId, Count(Id) from Contact where AccountId IN : accountIds AND persona__c!=null group by AccountId ]){
            mapChildTotalAmount.put((Id)agg.get('accId'),(Decimal)agg.get('expr0'));
        }
        
        for( Account acc : [Select Id, Related_Personas__c from Account where Id IN :accountIds])
        {
            Decimal totalPersona = mapChildTotalAmount.get(acc.Id);
            acc.Related_Personas__c = totalPersona ;
            accounttoUpdate.add(acc);
        }
    }
    
    if(!accounttoUpdate.IsEmpty())
        {
            update accounttoUpdate;
        }
}
 
 

Thanks,
Maharajan.C
Carrie Nunemaker 10Carrie Nunemaker 10
This worked perfectly!! Thank you both! 
Carrie Nunemaker 10Carrie Nunemaker 10
@Anthony McDougald - do you happen to have a test class that could get me past the code coverage error?
Anthony McDougaldAnthony McDougald
Good Morning Carrie,
Hope that your day is off to an amazing start. Below is a test class that you can use to get code coverage. Please test and feel free to report back if you have any questions or problems. Hope this helps and may God bless you abundantly.
@isTest
public class ContactSumTriggerTest {
    testmethod static void sumFields(){
        Account a = new Account(
        Name ='testAccount'
        );
        insert a;
        Contact c = new Contact(
        FirstName = 'test',
        LastName = 'contact',
        AccountId = a.Id,
        Persona_True__c= 0 
        );
        insert c;
        
        test.startTest();
        System.debug('Persona initial value is: ' + c.Persona_True__c);
        c.Persona_True__c = 1;
        update c;
        System.debug('Persona new value is: ' + c.Persona_True__c);
        System.debug('Account\'s Persona value is: ' + 
                     [SELECT Id, Related_Personas__c FROM Account WHERE Id IN :a.Id]);
        test.stopTest();
        
    }
    
}
Best Regards,
Anthony McDougald
 
Carrie Nunemaker 10Carrie Nunemaker 10
Thank you, thank you! 100% coverage.  I really appreciate the help. I've decided it's time to learn this side of the system after this experience. lol
Marco GasserMarco Gasser
We have a direct provider of fresh cut bank instrument for lease/sale, such as BG,SBLC, MTN, Bank Bonds,specifically for lease. If you are a potential Investor or Principle looking to raise capital, we will be happy to answer any questions that you have about this opportunity and to provide you with all the details regarding this services.

Regards
 
Marco Gasser

AA Group Financial Consulting AG

Email: MarcoGasser@Consultant.com

Email: marcoga@naver.com
Mister LeeMister Lee
Thanks For Posting. Thats what actually i need (https://wofcheatsanswer.com)
manpreet Singh 193manpreet Singh 193
Teji Infotech is an awarded Digital marketing company in Patiala (https://tejiinfotech.com) We are able to build attractive websites that help in boosting sales and conversations. we Are also institute in Patiala who provides Digital marketing course in Patiala (https://tejiinfotech.com/digital-marketing-course-in-patiala)
Noah MillerNoah Miller
Thank you so much for sharing this valuable information. You can also check out my blog. (https://thereviewhawk.com/)
Luke StocksLuke Stocks
This worked perfectly!! Thank you both! Buy Puppies Online (https://onebarkplaza.com)
Luke StocksLuke Stocks
Thank you so much for sharing this valuable information. Cat Collars (https://www.zacalcatcollars.co.uk/)
kamal kamilikamal kamili
great responses here !
kik sexting forum (https://kiksexting.co)
dasiy allmandasiy allman
The substance you scattered here is to an extraordinary degree stunning. It helped me a ton to liven me up.
Norton Coupon Codes (https://www.couponstechie.com/promo-codes/norton)
Lakudra BledeLakudra Blede
Around entertainers privileges, assent, and security, has become an undeniably examined issue in the business. There is actually nothing of the sort as ensured sex cams club (https://www.sexcams.club/) in the manner that cylinder destinations give it.

Beside most propagating similar issues portrayed over, xvideos tube (https://www.xvideos.tube/) likewise little industry straightforwardness, responsibility, or dependable data accessible.

It is essentially industry informants feeling just as xnxx top (https://www.xnxx.top/) penance their professions to shout out or writers revealing insight into manhandles like those asserted against it.

However actually free xhamster llc (https://www.xhamster.llc/) going anyplace (for the time being), and some cylinder destinations can be more moral than others. New plans of action are developing that furnish free clasps.

This entertainer arranged stage is tied in with making a space where free chaturbate porn (https://www.chaturbates.net/) helps rather than harms content makers. With a configuration that spotlights entertainers who are generally simply beginning.

The sort of grounded, home video, virtuous stuff that by one way or another leaves you feeling like possibly the subject of tranny (https://www.shemalecamspro.com/) can really be a net positive for society.
Lakudra BledeLakudra Blede
On the off chance that you know hotfallingdevil porn (https://www.sexcams.club/cam/hotfallingdevil/) , you realize the best arbitrators will in general pay attention to their standards.

Robbery is essentially a non-issue since littlesubgirl porn (https://www.sexcams.club/cam/littlesubgirl/) is prohibited from posting. Realness is esteemed to the exclusion of everything else, so you get significantly less.

This one was on the line for us, at the end of the day naughtyelle porn (https://www.sexcams.club/cam/naughtyelle/) to stand firm on disputable issues pushed it over the edge.

It can be a home for siswet19 porn (https://www.sexcams.club/cam/siswet19/) , and survivors have communicated that it is so hard to get them expelled. A long way from invulnerable to robbery, a siswet19 representative highlighted the site.

They was likewise up to speed in the anabel054 porn (https://www.sexcams.club/cam/anabel054/) debate. It impaired its anabel054 channel and started effectively bringing down the organization recordings.

At whatever point migurtt porn (https://www.sexcams.club/cam/migurtt/) is looked on the site now, it cautions clients about the studio misuses and urges watchers to report any of its recordings.
dasiy allmandasiy allman
A brilliantly written article, if only all bloggers offered your similar content, then the internet would be a better place.
27 Good things (https://www.27goodthings.com/)
Razi Haider 5Razi Haider 5
The graphic is being triggered in the PUBG mobile here you can find the latest info about PUBG (https://www.currentweek.net/pubg-mobile-new-version/)
Sarkari ResultSarkari Result
Thanks to the author for sharing valuable content with us.i am happy to share limbo mod apk - Attitude Status Hindi (https://bit.ly/2N3TreY), How Much do Public School Teachers Make (https://bit.ly/2AFeQs9), LCPS GO (https://bit.ly/3hr8ey2)