You need to sign in to do that
Don't have an account?
S_Batman
How to count checked field on related list?
I have a checkbox field called "Primary Contact" on the account level.
This field also shows up on the Contact Related list on the Account Object.
I want to create a custom field on the Account object, which counts and tells me how many contacts have "Primary Contact" box checked.
Could someone help me with the trigger to create that please?
This field also shows up on the Contact Related list on the Account Object.
I want to create a custom field on the Account object, which counts and tells me how many contacts have "Primary Contact" box checked.
Could someone help me with the trigger to create that please?
Also take the below latest code:
Regards,
Mahesh
All Answers
I just created Call_ME__c on Contact and NumberofChecks__C on Account.
would you be able to explain what each line does; so I have a better understanding?
What's the purpose of the Call Me field?
Please check the below information:
There are multiple ways:
You can do the same with Process builder. Please check below post i hope that will help you
-------------------------------------
Option 1:- Process builder
1) https://developer.salesforce.com/forums/?id=906F0000000BHvJIAW
2) https://developer.salesforce.com/forums/?id=906F0000000B2C0IAK
-------------------------------------
Option 2:- Trigger
1) https://developer.salesforce.com/forums/?id=906F00000008yWuIAI
2) https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries
3) http://www.anthonyvictorio.com/salesforce/roll-up-summary-utility/
4) http://www.iterativelogic.com/developing-custom-apex-rollup-summary-field-logic/
5) http://blog.jeffdouglas.com/2011/08/23/salesforce-trigger-when-rollups-summaries-not-possible/
------
For the option 2:
To calculate Rollup Summary using the Trigger manually, you need to follow below:
Need to consider all DML operations like insert, update, delete and undelete of child records.
Insert of Child record:
--> Need to write an after insert trigger to update the Parent record with calculated sum value.
Update of Child record:
--> Make sure that the Parent information got changed.
--> Need to write an after update trigger and get both old and new Parent information and perform the re-calculation of sum.
Delete of Child record:
--> Need to write an after delete trigger to update the Parent record with re-calculated sum value.
Undelete of Child record:
--> Need to write an after undelete trigger to update the Parent record with re-calculated sum value.
Please do let me know if this helps you.
Regards,
Mahesh
You need to have a trigger on contact on after insert, after update , before delete and after undelete
You need to query for contacts where Primary_contact__c = true and store count in field on contact.
Refer to this for more information along with trigger
https://success.salesforce.com/answers?id=90630000000h3mNAAQ
You can use aggregate function to get the count of the primary contacts,
AggregateResult[] groupedResults = [SELECT AccountId,SUM(Primary_Contact__c) FROM Contact GROUP BY accountId];
Let me know if you need any help regarding this.
Thanks,
Vijay
1 --> @VEnky There is not need to write on delete. Cox even if the record is deleted it wont matter. The query will pick only the records that have field value = true.
2--> @Mahesh - We cannt use Process builder , because it does not allow on check box field. (I may be wrong here. But this is my little knowledge.Correct me if I am wrong)
3--> @Vijay We cannot use SUM() on the field type picklist.
Here is the code, please change the Field Names according to your setup:
Please do let me know if it helps you.
Regards,
Mahesh
I recreated this trigger,
Instead of Total_Primary_Contacts__c I have Number_of_Primary_Contacts__c on the Account level;
When I try to save it I am an error -
Compile Error: No such column 'Number_of_Primary_Contacts__c' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 16 column 45
Regards,
Mahesh
Field Name - Number_of_Primary_Contacts
API Name - Number_of_Primary_Contacts__c
Contact Level:
Field Name: Primary_Contact_del
API Name: Primary_Contact_del__c
I really appreciate your assistance!
When I researched this error, one blog was talking about field level security; right now it is set to visible to just System Admin.
I am not sure if that is relevant at all?
You can try below:
Step 1: First try to save by commenting the error code.
Step 2: Now uncomment it and try to save it.
If the error still exists, then
Step 3: Enable the field to all profiles and see.
If the error still exists then
Step 4: Go to Workbench.developerforce.com --> SOQL --> try to write and execute simple SOQL query on Account object.
Step 5: Then copy our query and try to execute it in Work bench.
Step 6: Get the executed code and try to save it again.
Regards,
Mahesh
When I see your Screenshot it looks like it is from Production.
Regards,
Mahesh
Now I have another error:
Error: Compile Error: Incorrect SObject type: Contact should be Account at line 1 column 1
The Number of Primary Contact field is on the Account level; could that be causing the error?
set<Id> accIds = new set<Id>();
if(trigger.isinsert || trigger.isUpdate || trigger.Isundelete){
for(Contact con: Trigger.new){
accIds.add(con.AccountId);
}
}
if(trigger.isUpdate || trigger.isDelete){
for(Contact con: Trigger.old){
accIds.add(con.AccountId);
}
}
List<Account> accList = [select id, Number_of_Primary_Contacts__c, (Select Id, Primary_Contact_del__c from Contacts) from Account Where ID IN: accIds];
for(Account acc: accList){
system.debug('Contacts--->'+acc.contacts.size());
acc.Number_of_Primary_Contacts__c = 0;
for(Contact con : acc.Contacts) {
if(con.Primary_Contact_del__c)
acc.Number_of_Primary_Contacts__c++;
}
}
update accList;
}
I am also creating this trigger on the Account level; should be on the contact level instead?
This is literrally my first trigger in Salesforce!
I created the trigger in the Contact level;
but
I want the field Number of Primary contact on the account level to equal to how many Contacts on the related list have Primary Contact check box (the Primary Contact field is a field on the Contact level)
when I created the trigger and tried it in my sanbox, i got this error;
Error:Apex trigger PrimaryContactCount caused an unexpected exception, contact your administrator: PrimaryContactCount: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001230000040bYQAAY; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updatemytest: maximum trigger depth exceeded Contact trigger event AfterUpdate for [003230000027P24] Account trigger event AfterUpdate for [001230000040bYQ] Contact trigger event AfterUpdate for [003230000027P24, 003230000027P29] Account trigger event AfterUpdate for [001230000040bYQ] Contact trigger event AfterUpdate for [003230000027P24, 003230000027P29] Account trigger event AfterUpdate for [001230000040bYQ] Contact trigger event AfterUpdate for [003230000027P24, 003230000027P29] Account trigger event AfterUpdate for [001230000040bYQ] Contact trigger event AfterUpdate for [003230000027P24, 003230000027P29] Account trigger event AfterUpdate for [001230000040bYQ] Contact trigger event AfterUpdate for [003230000027P24, 003230000027P29] Account trigger event AfterUpdate for [001230000040bYQ] Contact trigger event AfterUpdate for [003230000027P24, 003230000027P29] Account trigger event AfterUpdate for [001230000040bYQ] Contact trigger event AfterUpdate for [003230000027P24, 003230000027P29] Account trigger event AfterUpdate for [001230000040bYQ]: []: ()
Also take the below latest code:
Regards,
Mahesh
Now just looking into how to deploy it into production :)!!
Thank you sooo much for your help!! Really appreciate it!
Below are some of the information about Test Classes:
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Please follow below salesforce Best Practice for Test Classes :-
1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process.
Regards,
Mahesh
If you have large amount of records then we have to export and update them so that it will be calculated.
If you have less amount of records then we can touch them manually.
Regards,
Mahesh
I replied to your other post:
https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=906F0000000DC11IAG
Also use the code which I provided in the above post. It considered all positive and negative scenarios.
Regards,
Mahesh
I re-posted the same above trigger again here because I just changed one line of code.
if(Trigger.isDelete || (con.Primary_Contact_del__c != Trigger.oldMap.get(con.Id).Primary_Contact_del__c))
to
if(Trigger.isDelete || (con.Primary_Contact_del__c != Trigger.newMap.get(con.Id).Primary_Contact_del__c))
Regards,
Mahesh