function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KeerthigeeKeerthigee 

Issue related to lead

Issue related to lead

Hi All,

I want to display total no of leads  created  in account object.Here,Account is parent object and lead -child object.

Through report ,we can get number of leads ina particular period for particular object.But i want to update no of leads in parent object.How?

Can anyone help me?

Kindly support and suggest.

Thanks.
Best Answer chosen by Keerthigee
KeerthigeeKeerthigee
Hi,

I got this requirement by creating trigger on lead.

First, I created fields i.e noofleads__c in account and leadaccount__c is a lookup field in lead object.

I wrote code as

trigger ledcunt on Lead(after insert) {
set<id> acc=new set<id>();
for(lead l:trigger.new)
{
    if(l.leadcount__c!=NULL)
        acc.add(l.leadcount__c);
}

List<account> acclist=[Select id,name,noofleads__c,(select id,name from Leads1__r) from account where ID in :acc];
for(Account a:acclist)
{
a.noofleads__c =a.Leads1__r.size();
}
update acclist;
}

Leads1__r is a child relationship name in lookup field.


It is best one.

All Answers

Anoop yadavAnoop yadav
Hi Gita,

You can do this by using Trigger on Child Object.

Check the below link and change According to your need.
http://rakeshistom.wordpress.com/2013/02/13/count-records-in-a-related-list/
KeerthigeeKeerthigee
Hi Anoop,

Thank  you for  Immediate Response.

In this link, parentlookup__c and Rollupcounter__c are the  custom fields in child and parent object.is it right?

Awaiting for your response.
Anoop yadavAnoop yadav
parentlookup__c is on Clild.
Rollupcounter__c is on Parent.

In This (Rollupcounter__c) Field field you will get the total Leads related to that records
KeerthigeeKeerthigee
Hi Anoop,

I had created a fields i.e parentlookup__c on lead  and Rollupcounter__c on account field.

and I wrote trigger as

trigger count on lead (after insert, after update, after delete, after undelete) {
  Map<Id,account> m = new Map<Id,account>();
  if(Trigger.new<>null)
    for(lead l:Trigger.new)
      if(l.ParentLookup__c<>null)
       m.put(l.ParentLookup__c,new Account(id=l.ParentLookup__c));
  if(Trigger.old<>null)
    for(lead l:Trigger.old)
      if(l.ParentLookup__c<>null)     
        m.put(l.ParentLookup__c,new Account(id=l.ParentLookup__c));
  update m.values();
}

I got an error as "Invalid Constructor syntax,name=value pairs can only be used for Sobjects" on line 6


trigger noofleads on Account (before insert,before update) {
    for(Account a:Trigger.new)
    a.RollupCounter__c = 0;
   for(lead l:[select id,ParentLookup__c from lead where ParentLookup__c in :Trigger.new])
    Trigger.newMap.get(l.ParentLookup__c).RollupCounter__c++;
}


I am getting an error as " Variable doesnot exist :Rollupcounter__c" on line 3

Awaiting for Response.




Anoop yadavAnoop yadav
Hi,

ParentLookup__c Field should be your Account look up Field.
I think u have created this as a Text Field.
KeerthigeeKeerthigee
Hi,

Thank you for response.

No,I had created Lookup field only.

Kindly suggest and support.


Anoop yadavAnoop yadav
I am not sure why u are getting this Error.
is ParentLookup__c Field your API Name?
KeerthigeeKeerthigee
Hi,

Thank you for response.

Yes,parentlookup__c is API name in Lead object.


Anoop yadavAnoop yadav
Try the below new Link .
http://www.infallibletechie.com/2013/09/trigger-to-count-number-of-contacts.html

This can also help you.
Here you have to write only one trigger on Child.
KeerthigeeKeerthigee
Hi,

I got this requirement by creating trigger on lead.

First, I created fields i.e noofleads__c in account and leadaccount__c is a lookup field in lead object.

I wrote code as

trigger ledcunt on Lead(after insert) {
set<id> acc=new set<id>();
for(lead l:trigger.new)
{
    if(l.leadcount__c!=NULL)
        acc.add(l.leadcount__c);
}

List<account> acclist=[Select id,name,noofleads__c,(select id,name from Leads1__r) from account where ID in :acc];
for(Account a:acclist)
{
a.noofleads__c =a.Leads1__r.size();
}
update acclist;
}

Leads1__r is a child relationship name in lookup field.


It is best one.
This was selected as the best answer