You need to sign in to do that
Don't have an account?
Keerthigee
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.
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.
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
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.
otherwise modify your query
for(Account Act : [SELECT Id,rollupcounter__c FROM Account WHERE Id = : ld.parentlookup__c AND rollupcounter__c!=NULL ])
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.
Thank you for response.
I too don't know.thatwhy I am asking.
Awaiting for your response.
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
No,Its not working
No errors.I didn't get update in that field(rollupcounter__c).
Thanks.
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;
}
Thank you for response.
Here Leads__r means custom object.Why we take leads__r?
I had created one custom object i.e leadaccount.
Which relationship will be given in lead,leadaccount,account.
Thanks.
Lookup relationship is good one among 3 objects.Is it right?
Kindly support and suggest.
I didn't get it what you said.
Your requirement is "Displaying Total Number of leads on Account" ... am i right? .. i hope the code i given will work ..
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??
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.
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.
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.
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?
Thank you for response.
Here,childrelationship name is leads1.
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.
Thank you for response which is helpful.
Now,How to count number of leads for "particular period" associated with account??
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.
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..
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.
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