You need to sign in to do that
Don't have an account?
Adam Dillman
Calculate the number of contacts for a given account record - not working
Hi All,
I am having some trouble getting this code snippet to work. I am relatively new to Apex and DML/SOQL so it could be something simple. My goal is, given a list of accounts, determine the number of contacts belonging to each account, and then update a field called Number_of_Contacts__c with that value. Here's what I came up with:
Line: 1, Column 31 Unexpected token '('.
Any ideas where I'm going wrong? Also, I know my code is violating several "Best Practices" but I'm just trying to figure things out at the moment. Thanks!
I am having some trouble getting this code snippet to work. I am relatively new to Apex and DML/SOQL so it could be something simple. My goal is, given a list of accounts, determine the number of contacts belonging to each account, and then update a field called Number_of_Contacts__c with that value. Here's what I came up with:
public class AccountProcessor { public static void countContacts(List<ID> recordIds){ //take a list of recordids List<Account> accountlist = [Select name FROM Account WHERE ID IN :recordIDs]; //get a list of the corresponding accounts for(Account tempaccount: accountlist){ //for each account in this list... Integer contactCount = 0; contactCount = [SELECT Count() from CONTACT WHERE contact.account.ID = :tempaccount.ID]; //count the number of contacts associate with each account tempaccount.number_of_contacts__c = contactCount; update tempaccount; } } }I am trying to run this code using the following anonymous execution:
AccountProcessor.countContacts(001f400000KfIMbAAN);I'm not sure if the AccountID should be in quotes are not, but it doesn't work either way. I keep getting the following error:
Line: 1, Column 31 Unexpected token '('.
Any ideas where I'm going wrong? Also, I know my code is violating several "Best Practices" but I'm just trying to figure things out at the moment. Thanks!
Hi Adam,
The reason it is not working is because your function is expecting a list of IDs but you are passing just an ID. To achieve that you need to add that ID to a list and then pass it to your function:
Your function call should look something like this:
This should resolve your error.
Let me know if it helps.
Thanks :)
All Answers
Let me know if it helps.
Thanks.
Thanks for your suggestions. I tried replacing the body of my method with your code. However, when I try to call the method using
I get the error: "Line: 1, Column: 31 Unexpected token '('."
Any ideas?
list<id> Idlst=new list<id>();
Idlst.add('001f400000KfIMbAAN');
AccountProcessor.countContacts(Idlst);
Hi Adam,
The reason it is not working is because your function is expecting a list of IDs but you are passing just an ID. To achieve that you need to add that ID to a list and then pass it to your function:
Your function call should look something like this:
This should resolve your error.
Let me know if it helps.
Thanks :)
If you are planning to call it from a trigger, here is the one way to call it