You need to sign in to do that
Don't have an account?
KimBailey
Hi,
I'm a PHP and Wordpress developer, new to SF.
I created a new custom object, called "referrals".
Every record in this object has a couple of fields and also an "email" field.
In my accounts object, I created a new field - "number of referrals".
In this field, I want to get the total number of referrals that has the same email address as the account.
What's the best way to do that?
Thanks!
How Do I Sum records from another custom object?
Hi,
I'm a PHP and Wordpress developer, new to SF.
I created a new custom object, called "referrals".
Every record in this object has a couple of fields and also an "email" field.
In my accounts object, I created a new field - "number of referrals".
In this field, I want to get the total number of referrals that has the same email address as the account.
What's the best way to do that?
Thanks!
If it's a lookup relationship, you'll need a trigger to summarize the fields. You'll find some sample triggers for this through a quick Google search.
1) Create a relationship between referrals and accounts. Depending on your model, this may be a 1 to many relationship, or a many to many relationship. For the latter, you would look into setting up what's called a "Junction object". Your relationship between Account and either the Referral or Referral Junction object would e a Master Detail. This would allow you to use a "Roll up Summary field" to count the number of referrals for that given account. This may or may not involve a trigger to establish the referral -> account relationship, depending on how your referrals are created.
2) Another option is to write a trigger on the Referrals object. Any time a referral is created, or the email address is updated, then you would query for accounts with the same email. You're going to run into a few issues here. Mainly, you'll have to support Referral deletes as well if you want an accurate representation on the account. For instance, if a referral is deleted, you'll have to decrease the Number_Of_Referral fields on the Account as well. Additionally, using the emailAddress is more of a logical relationship than defining a Master Detail or Lookup from Referral to Account, so you'll have to get creative with Maps in your trigger, for one.
Above is a simple example of how your trigger, using the second option *might* work. Note, I'm not handling the deletion case above, so you'll have to work that out too.
For more on working with Group By / Aggregate results, see this: http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm