You need to sign in to do that
Don't have an account?
Elsa Cordonnier
Trigger count custom object on Leads
Hello everyone,
I'm looking to create a trigger on a Lead level to count how many custom object are associated to this Lead. The lead and the custom object(Box) are associate through a lookup filed in the Box object.
I create a custom filed call Count__c to collect the result.
I get inspiration from a trigger which count the number of tasks assiciate to a Lead but this doesn't work.
Can you provide me advise ?
Thank you
Best
Elsa
I'm looking to create a trigger on a Lead level to count how many custom object are associated to this Lead. The lead and the custom object(Box) are associate through a lookup filed in the Box object.
I create a custom filed call Count__c to collect the result.
I get inspiration from a trigger which count the number of tasks assiciate to a Lead but this doesn't work.
Can you provide me advise ?
Thank you
Best
Elsa
I tried the code its working for me...check this out..
Trigger:::::
trigger CountNatterboxOnLead on shobithapp__NatterboxCallReporting__c (after update,after insert) {
list<id> leadids=new list<id>();
list<Lead> leadnames=new list<Lead>();
if(Trigger.isUpdate|| Trigger.isInsert)
{
for(shobithapp__NatterboxCallReporting__c n:trigger.new)
{
leadids.add(n.shobithapp__lead_name__c );
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),shobithapp__lead_name__c FROM shobithapp__NatterboxCallReporting__c where shobithapp__lead_name__c IN :leadids Group By shobithapp__lead_name__c];
for(AggregateResult ar:groupedResults) {
Id accidd=(ID)ar.get('shobithapp__lead_name__c');
Integer count = (INTEGER)ar.get('expr0');
lead l= new lead(Id=accidd);
l.shobithapp__Total_No_of_boxes__c= count;
leadnames.add(l);
}
update leadnames;
}
mark this as best answer if your issue is resolved
thanks
Vishnu R
All Answers
Write trigger on task or custom object of chaild to lead .
You have to write a trigger on child not on parent .
If any query ask me .
Regards,
Harish.R.
I tested and it doesnt seems to work
Error: Compile Error: sObject type 'Leads' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 40 column 39
trigger UpdateOrder on Natterbox_Call_Reporting_Object__c (after insert, after update, after delete, after undelete) {
List<Lead> ct = new List<Lead>();
Set<Id> custord = new Set<Id>();
if(Trigger.isDelete) {
for(Natterbox_Call_Reporting_Object__c test:Trigger.Old) {
custord.add(test.Lead);
}
}
else
if(Trigger.isUpdate) {
for(Natterbox_Call_Reporting_Object__c test:Trigger.New) {
custord.add(test.Lead);
}
for(Natterbox_Call_Reporting_Object__c test:Trigger.Old) {
custord.add(test.Lead);
}
}
else
{
for(Natterbox_Call_Reporting_Object__c test:Trigger.New) {
custord.add(test.Lead);
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id), Lead FROM Lead where Natterbox_Call_Reporting_Object__c IN :custord GROUP BY Lead ];
for(AggregateResult ar:groupedResults) {
Id custid = (ID)ar.get('Lead');
Integer count = (INTEGER)ar.get('expr0');
Lead cust1 = new Lead(Id=custid);
cust1.count__c = count;
ct.add(cust1);
}
update ct;
}
i just tried with Accounts and Contacts.it is working for me..so try with this code.....
Trigger::
trigger ContactsCount on Contact (after update,after insert) {
list<id> accids=new list<id>();
list<Account> accnames=new list<Account>();
if(Trigger.isUpdate)
{
for(Contact c:trigger.new)
{
accids.add(c.AccountID);
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),AccountId FROM contact where AccountId IN :accids GROUP BY AccountId ];
for(AggregateResult ar:groupedResults) {
Id accidd=(ID)ar.get('AccountId');
Integer count = (INTEGER)ar.get('expr0');
account a = new account(Id=accidd);
a.shobithapp__Total_No_of_boxes__c= count;
accnames.add(a);
}
update accnames;
}
Mark solved if it resolved your issue.
thanks
Vishnu R
list<id> leadsids=new list<id>();
list<Lead> leadsnames=new list<Lead>();
if(Trigger.isUpdate)
{
for(Natterbox_Call_Reporting_Object__c c:trigger.new)
{
leadsids.add(c.LeadID);
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),LeadsId FROM Natterbox_Call_Reporting_Object__c where LeadsId IN :leadsids GROUP BY LeadsId ];
for(AggregateResult ar:groupedResults) {
Id accidd=(ID)ar.get('LeadId');
Integer count = (INTEGER)ar.get('expr0');
account a = new lead(Id=accidd);
a.shobithapp__TNatterbox_activity_count__c= count;
accnames.add(a);
}
update accnames;
}
It's still dont work, the errors tell me that the LeadID doesnt exist
Account and contact are having a relation so I have used the comman thing i.e AccountId.for your case check whether
- the list of IDs is not empty (if it is empty change the line 8 as leadsids.add(c.Lead);
- if the list is not empty then in the aggregate query you should see the below mentioned points carefully and modify the query using lead not leadsId because you should use a field which is common in both the objects.if you use a relational field you won't get the error "LeadsId does not exist".
- use lead instead of account in line 15 .
- COUNT() must be the only element in the SELECT list.
- You can use COUNT() with a LIMIT clause.
- You can't use COUNT() with an ORDER BY clause. Use COUNT(fieldName) instead.You can't use COUNT() with a GROUP BY clause for API version 19.0 and later. Use COUNT(fieldName) instead.
hope this helps you.....thanks
vishnu R
I tried the code its working for me...check this out..
Trigger:::::
trigger CountNatterboxOnLead on shobithapp__NatterboxCallReporting__c (after update,after insert) {
list<id> leadids=new list<id>();
list<Lead> leadnames=new list<Lead>();
if(Trigger.isUpdate|| Trigger.isInsert)
{
for(shobithapp__NatterboxCallReporting__c n:trigger.new)
{
leadids.add(n.shobithapp__lead_name__c );
}
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),shobithapp__lead_name__c FROM shobithapp__NatterboxCallReporting__c where shobithapp__lead_name__c IN :leadids Group By shobithapp__lead_name__c];
for(AggregateResult ar:groupedResults) {
Id accidd=(ID)ar.get('shobithapp__lead_name__c');
Integer count = (INTEGER)ar.get('expr0');
lead l= new lead(Id=accidd);
l.shobithapp__Total_No_of_boxes__c= count;
leadnames.add(l);
}
update leadnames;
}
mark this as best answer if your issue is resolved
thanks
Vishnu R