enter image description hereI am trying to count distinct records from one table and update the count in different table:

Account has relationship with table A Contact has relationship with table B.

On Table B we have columns like

ColumnA1 ColumnA2 ColumnA3 ContactLookUp Designation
R1 R12 R13 Contact Developer
R1 R12 R13 Contact Architect R12 R122 R132 Contact Architect R12 R122 R132 Contact Architect

Here ColumnA1 + ColumnA2+ ColumnA3 is the composite key

So based on the Table B data we need to create unique records in Table A with the counts of Developer or Architect.

So in Table A we will have 2 Unique records considering the Composite key of table B

Table B Columns are like this

AccountLookup ColumnB1 ColumnB2 ColumnB2 Developer Architect Contact.Account R1 R12 R13 1 1 Contact.Account R12 R122 R132 0 2

Could anyone help me in this scenario. Thanks in Advance.


Doing what you're asking is best accomplished using what's referred to as a "roll-up" trigger. Since you don't have a Master-Detail relationship, that's the only way to accomplish this. There are two ways of approaching this, one is from the Account end, the other is from the source or Contact end of things. Whenever an Account is updated, your trigger would fire, collect the data for all related contacts and then update the look-ups to the Account.

If I'm understanding your scenario correctly, alternatively, since this all begins with Contact, when a contact is updated, you could have a trigger fire that does the calculations for the related Account's look-ups. That approach could likely be done without any queries.

Which way to go would depend on when you want the look-ups to be updated.

  • Thanks for the suggestions i am using wrapper class with the trigger to calculate the role count on account and it's working the way i expected ,unique composite key is the main character here. – Cloudforce Aug 4 '15 at 19:14
  • Glad you got it worked out. If this post led you to solving your issue, please help the rest of the community by marking it as having answered your question. – crmprogdev Aug 4 '15 at 19:32

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.