Activity Forums Salesforce® Discussions Appex Triggers on Lookup relationships

  • Appex Triggers on Lookup relationships

    Posted by Matthew on August 13, 2019 at 1:31 pm

    Hi,

    I need to be able to do a rollup summary of the total number of Sites associated to an account. The site object is just and lookup relationship of account.

    Thanks

    Matthew replied 4 years, 7 months ago 2 Members · 2 Replies
  • 2 Replies
  • Yogesh

    Member
    August 13, 2019 at 3:20 pm

    hello, you can take help from this code below  :-

    trigger CountContactsnew on Contact (after insert, after delete, after undelete) {

    List<id> accIdList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
    For(Contact con1 : Trigger.new){
    accIdList.add(con1.accountid);
    }
    }
    if(Trigger.isDelete){
    For(Contact con1 : Trigger.old){
    accIdList.add(con1.accountid);
    }
    }
    List<Account> accUpdateList = new List<Account>();
    For(Account acc : [SELECT Contact_Recs__c,(SELECT id FROM Contacts) FROM Account WHERE id =: accIdList]){
    acc.Contact_Recs__c = acc.Contacts.size();
    accUpdateList.add(acc);
    }
    update accUpdateList;
    }
    }

  • Matthew

    Member
    August 14, 2019 at 9:12 am

    Great, I have used this and ammended to the objects I am using and it works fine.

     

    Thanks

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos