You need to sign in to do that
Don't have an account?
Nagaraju Mogili
how can we show the number of contacts of an account using apex class
I want to show the number of contacts of an Account in the vf page using Apex code and Vf page..
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
Thanks for your code.
I am getting the error at 13 and 14 lines in the controller,could you please explain me why..?
Error: NewController_cntrlr Compile Error: Loop variable must be a generic SObject or List or a concrete SObject or List of: Contact at line 12 column 12,,
public class NewController_cntrlr {
public Map<Id ,List< Contact>> mapToshowContactCount{get;set;}
public NewController_cntrlr () {
mapToshowContactCount = new Map<Id , List< Contact>>();
List<Account> accList = [Select Id , (Select Name FROM Contacts) FROM Account ];
for(Account acc : accLIst) {
for(Contact con : acc.Contacts) {
if(!mapToshowContactCount.containsKey(acc.Id)) {
mapToshowContactCount.put(acc.Id, new List<Contact>{con});
}
else {
mapToshowContactCount.get(acc.Id).add(con);
}
}
}
System.debug(mapToshowContactCount);
}
}
Regards,
Nagaraju Mogili
All Answers
May I suggest you please check with below link from the stack exchange community which will point you further in the right direction.
- https://salesforce.stackexchange.com/questions/13156/displaying-only-contacts-from-account
Let us know if this helps.Kindly mark this as solved if it's resolved.
Thanks,
Nagendra
You just query on account(Parent object) with inner query(child object) and take thier size and show in account field.
Like ->
setid = Current Record id
Account objacc = [select id, Name, (Select id, LastName from Account.Contacts) from Account where id in: setid ];
Account Acc = new Account();
Acc .fieldcoun__c = objacc.Contacts.size();
Acc .id= objacc.id;
update Acc ;
it will show no. of contact for particuler account.
way to access your vf page
https://varun007-dev-ed--c.ap5.visual.force.com/apex/getrelatedcon?id=0017F00000ExzeM
here getrelatedcon is my vf page
0017F00000ExzeM id of a accout which have 5 contacts
output is --> The total number of contacts on Account is: 5
Controller Page
I hope this is helpful for you.
Hi Nagaraju,
I have something similar answer to your problem. I have created an inline VF page where contact is the parent of the custom object Transaction Entries and this VF page is showing the count of the child records under the contact in the inline VF page
VF Page
<apex:page standardController="Contact" extensions="countTotalChilds" >
<apex:form>
<apex:pageBlock>
<apex:outputText value="{!countRecords}" ></apex:outputText>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller
public class countTotalChilds {
//ApexPages.standardController instance
ApexPages.StandardController stcon;
//Variable to get the Current Page Id
Id accId{get;set;}
//Variable to hold the count of the number of the records
public Integer countRecords{get;set;}
//Class constuctor
public countTotalChilds(ApexPages.StandardController st) {
//Setting the values
stcon = st;
accId = ApexPages.currentPage().getparameters().get('Id');
List<AggregateResult> trans = [Select count(Id) custId FROM Transaction_Entries__c WHERE Contact_Name__c =: accId];
countRecords = (Integer)trans[0].get('custId');
}
}
Please Mark it best answer if you find this helpful.
Thanks
Thanks for your Reply.
Do we have any chances to do this task by using Map, I want to show all of the Accounts and related contacts in VF page.
Regards,
Nagaraju
Thanks for your code.
I am getting the error at 13 and 14 lines in the controller,could you please explain me why..?
Error: NewController_cntrlr Compile Error: Loop variable must be a generic SObject or List or a concrete SObject or List of: Contact at line 12 column 12,,
public class NewController_cntrlr {
public Map<Id ,List< Contact>> mapToshowContactCount{get;set;}
public NewController_cntrlr () {
mapToshowContactCount = new Map<Id , List< Contact>>();
List<Account> accList = [Select Id , (Select Name FROM Contacts) FROM Account ];
for(Account acc : accLIst) {
for(Contact con : acc.Contacts) {
if(!mapToshowContactCount.containsKey(acc.Id)) {
mapToshowContactCount.put(acc.Id, new List<Contact>{con});
}
else {
mapToshowContactCount.get(acc.Id).add(con);
}
}
}
System.debug(mapToshowContactCount);
}
}
Regards,
Nagaraju Mogili
I don't know why you are getting this error, I've created the same code in my org and it is running smoothly. Here is the snap of the VF page output. Also is the VF page is same as I sent??
Still I am getting the same Error, can you text me your Skype Id.
I have tried in another Org as well.
Regards,
Nagaraju Mogili
Can you please ping me on skype, I have already sent you my skype Id.
Here is your code
Mark, it as best answer if you find it useful so that other developers can follow.
Thanks.
Regards Sharad.