You need to sign in to do that
Don't have an account?
Abby Douglas 9
Cound Leads Associated to Account
Hi - I'm trying to create a trigger to update the lead count on the Account page. I have a lookup relationship between Leads and Accounts (accounts__c) and a custom field on the Account (lead_count__c). I've created the below by tweaking a contact count trigger, but I'm running into the below errors on line 21 and 23.
I'm by no means a developer and any help and guidance would be greatly appreciated. Thank you and Happy Holidays!
I'm by no means a developer and any help and guidance would be greatly appreciated. Thank you and Happy Holidays!
Line 23 - Lead_Count__c, (Select id, Name From Lead) from Account Where id in:parentIdsSet ^ ERROR at Row:1:Column:55 Didn't understand relationship 'Lead' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. Line 21 - Variable does not exist: Leads
trigger leadSumTrigger on Lead (After insert, After delete, After update, After undelete) { Set<Id> parentIdsSet = new Set<Id>(); List<Account> accountListToUpdate = new List<Account>(); IF(Trigger.IsAfter){ IF(Trigger.IsInsert || Trigger.IsUndelete|| Trigger.IsUpdate){ FOR(Lead l : Trigger.new){ if(l.Account__c!=null){ parentIdsSet.add(l.Account__c); } } } IF(Trigger.IsDelete|| Trigger.IsUpdate){ FOR(Lead l : Trigger.Old){ if(l.Account__c!=null){ parentIdsSet.add(l.Account__c); } } } } System.debug('#### parentIdsSet = '+parentIdsSet); List<Account> accountList = new List<Account>([Select id ,Name, Lead_Count__c, (Select id, Name From Lead) from Account Where id in:parentIdsSet]); FOR(Account acc : accountList){ List<Lead> leadList = acc.Leads; acc.Lead_Count__c = leadList.size(); accountListToUpdate.add(acc); } try{ update accountListToUpdate; }catch(System.Exception e){ }}
All Answers
Use the Child relationship name in query don't use the Lead.
In Account and Contact the child relationship name is Contacts so in the query we will use as below
Select Id,ParentId, (Select id from Contacts) from Account
similarly in your query line 21 you have to use child relationship name not Lead may be it should be Leads.
List<Account> accountList = new List<Account>([Select id ,Name, Lead_Count__c, (Select id, Name From Leads) from Account Where id in:parentIdsSet]);
to check the child relation goto that custom Lookup field in Lead:
Setup --> Lead --> Account__c --> open the Field --> Child Relationship Name (In Lookup Options Section) --> use this name apex.
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
I have a lookup relationship between Accounts and Leads (lookup field on lead object account__c) and then a custom number field on Accounts (field name lead_count__c).
How would you update my trigger to work?
Any help you can provide would be greatly appreciated!
Please answer the below questions:
1. Now the trigger is saved or not? still facing issue?
2. Did you have checked the Child Relation Name in Lead object for custom Account Lookup field account__c as like in below screenshot:
Here you have the Name as Leads then use query like below:
List<Account> accountList = new List<Account>([Select id ,Name, Lead_Count__c, (Select id, Name From Leads) from Account Where id in:parentIdsSet]);
for(Account acc : accountList){
acc.Lead_Count__c = acc.Leads.size();
accountListToUpdate.add(acc);
}
otherwise use the child relation name as per your field in Lead object in the above two bold areas.
Remaining things in your looks OK.
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C