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 

Display number of leads associated with account.

Hi All,

I want to write trigger to count number of leads associated with account.Can anyone help me?

I created two fields as Parentlookup__c (lookup field) in lead object  and rollupcounter__c in account.

I wrote this trigger on lead object.

trigger leadcont on Lead (after insert) {
    Integer leadcnt ;
List<Account> ActData = new List<Account>();
//Account[] ActData = new Account[]{};

for (Lead ld:trigger.new)
{
if (ld.parentlookup__c <> null)
{
for(Account Act : [SELECT Id,rollupcounter__c FROM Account WHERE Id = : ld.parentlookup__c])
{
  leadcnt = integer.ValueOf(Act.rollupcounter__c)+1;
  ActData.add(new Account(Id=ld.parentlookup__c, rollupcounter__c=leadcnt));   
}
}
}
if (!ActData.isempty())
{
    update ActData;
}


}

I am getting an error "Invalid constructor syntax,name=value pairs only valid for Sobjects".

Kindly support and suggest.
Best Answer chosen by Keerthigee
Ravi NarayananRavi Narayanan
trigger leadcont on Lead (after insert) {

set<id> accountset=new set<id>();
for(lead l:trigger.new)
{
    if(l.parentlookup__c!=NULL)
        accountset.add(l.parentlookup__c);
}

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

try this code..

All Answers

Ravi NarayananRavi Narayanan
You are getting this error while saving?  if so, which line
KeerthigeeKeerthigee
Hi Ravi Narayan,

Thank you for immediate response.

I solved those error.Now I am getting another error as "Execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.count: line 12, column 1" while saving record in lead object.

Awaiting for your response.
Ravi NarayananRavi Narayanan
looks like the field "rollupcounter__c"  is not havign any value. thats wy u r getting the error..  
Ravi NarayananRavi Narayanan
make sure there are some values in the field "rollupcounter__c" 
Ravi NarayananRavi Narayanan

otherwise modify your query 

for(Account Act : [SELECT Id,rollupcounter__c FROM Account WHERE Id = : ld.parentlookup__c AND rollupcounter__c!=NULL ])

KeerthigeeKeerthigee
HI Ravi Narayan,

Thank you for immediate response.

Using your query ,solve that error.But I didn't get update to rollupcounter__c in account while saving record in lead object.

Awaiting for your response.
Ravi NarayananRavi Narayanan
rollupcounter__c  -- this field value is NULL. thats why it is not getting updated .  
KeerthigeeKeerthigee
HI,

 Thank you for response.

I too don't know.thatwhy  I am asking.

Awaiting for your response.
Ravi NarayananRavi Narayanan
what is that field ? is it a roll up summary field?  
KeerthigeeKeerthigee
No,just a number field in account object.
Ravi NarayananRavi Narayanan

trigger leadcont on Lead (after insert) {
    Integer leadcnt ;
List<Account> ActData = new List<Account>();
//Account[] ActData = new Account[]{};

for (Lead ld:trigger.new)
{
if (ld.parentlookup__c <> null)
{
for(Account Act : [SELECT Id,rollupcounter__c FROM Account WHERE Id = : ld.parentlookup__c])
{
             if(Act.rollupcounter__c ==NULL)
                         Act.rollupcounter__c=0;

  leadcnt = integer.ValueOf(Act.rollupcounter__c)+1;
  ActData.add(new Account(Id=ld.parentlookup__c, rollupcounter__c=leadcnt));  
}
}
}
if (!ActData.isempty())
{
    update ActData;
}


}

try this.. mark as best answer if it works 

KeerthigeeKeerthigee
Hi Ravi,

No,Its not working

 
Ravi NarayananRavi Narayanan
u r getting some error? 
KeerthigeeKeerthigee
Hi Ravi,

No errors.I didn't get update in that field(rollupcounter__c).

Thanks.
Ravi NarayananRavi Narayanan
try thsi code.

trigger leadcont on Lead (after insert) {

set<id> accountset=new set<id>();
for(lead l:trigger.new)
{
    if(l.parentlookup__c!=NULL)
        accountset.add(l.parentlookup__c);
}

List<account> acclist=[Select id,name,(select id,name from Leads__r) from account where ID in :accountset];
for(Account a:acclist)
{
integer count;
    for(Lead i:a.leads__r)
    {
      count=1;
      count++;
    }
a.rollupcounter__c =count;
}
update acclist;
}
KeerthigeeKeerthigee
Hi Ravi,

Thank  you  for  response.

Here Leads__r means custom object.Why we take leads__r?
Ravi NarayananRavi Narayanan
ere Leads__r means custom object.Why we take leads__r?  --- this is relationship field betweeen accounts and leads.. is it working for u?
KeerthigeeKeerthigee
Hi Ravi,

I had created one custom object i.e leadaccount.

Which relationship will be given in lead,leadaccount,account.

Thanks.
KeerthigeeKeerthigee
Hi Ravi,

Lookup relationship is good one among 3 objects.Is it right?

Kindly support and suggest.

Ravi NarayananRavi Narayanan
leadaccount is having relationship with leads?  click ont hat lookup field.. you will see child relationship name.. u shud use that name in query
KeerthigeeKeerthigee
Hi Ravi,

I didn't get it what you said.
Ravi NarayananRavi Narayanan

Your requirement is "Displaying Total Number of leads on Account" ... am i right?  ..  i hope the code i given will work .. 

 

KeerthigeeKeerthigee
Hi Ravi,

That is good.Before going to execute that code,I would make a relationship between lead and leadaccount.i.e lookup relationship.

Is it right??

Ravi NarayananRavi Narayanan

no.. there is no need to create any lookup and all.. already you have a relationship between account and leads "Parentlookup__c"  .

 

Just copy the code and try to insert some lead and check the value of roolup count   in account.

KeerthigeeKeerthigee
Hi Ravi,

My Actual requirement is "total number of leads for a particular year associated with account to be displayed on  account object."

First,I am trying to display number of leads associated with  account.

Thanks.

Ravi NarayananRavi Narayanan
yah.. the trigger i posted above will give u number of leads for particular account
KeerthigeeKeerthigee
Hi Ravi,

I am getting error related to leads__r.

I created one custom object i.e leadaccount__c. I didn't give any relationship between lead and leadaccount.
Ravi NarayananRavi Narayanan

Parentlookup__c  --- this field is on lead object and having relationship with accounts..., am i rite?

 

can u goto thsi lookup field and let me know the childrelationshipname given in the screen?  

KeerthigeeKeerthigee
Hi Ravi,

Thank you for response.

Here,childrelationship name is leads1.

Ravi NarayananRavi Narayanan
trigger leadcont on Lead (after insert) {

set<id> accountset=new set<id>();
for(lead l:trigger.new)
{
    if(l.parentlookup__c!=NULL)
        accountset.add(l.parentlookup__c);
}

List<account> acclist=[Select id,name,(select id,name from Leads1__r) from account where ID in :accountset];
for(Account a:acclist)
{
integer count;
    for(Lead i:a.leads1__r)
    {
      count=1;
      count++;
    }
a.rollupcounter__c =count;
}
update acclist;


try this code and mark as best answer if it works. 
KeerthigeeKeerthigee
Hi  Ravi,

Thank you for response which is helpful.

Now,How to count  number of leads for "particular period" associated with account??
KeerthigeeKeerthigee
Hi Ravi,

I didn't get update value in that field.The value  i.e 2 in that field(rollupcounter__c)  remains constant while creating number of leads.

Awaiting for your response.
Ravi NarayananRavi Narayanan
trigger leadcont on Lead (after insert) {

set<id> accountset=new set<id>();
for(lead l:trigger.new)
{
    if(l.parentlookup__c!=NULL)
        accountset.add(l.parentlookup__c);
}

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

try this code..
This was selected as the best answer
Ravi NarayananRavi Narayanan
Hit best answer if it solves your problm
KeerthigeeKeerthigee
Hi Ravi,

Thank you for immediate response which is helpful.

Now,Can you suggest for "counting number of leads for particular month or year associated with account".

Awaiting for  your response.
KeerthigeeKeerthigee
Hi Ravi,

Hope you are doing well.

I have created a custom field like sign with datatype textarea(long) in contact object.Whenever I enter sign in that field then it displays image as below the field.

Can anyone hepl me?

I think ,it is possible with visualforce with custom controller(using img tag).

Kindly support and suggest as early as possible.

Awaiting for your response