You need to sign in to do that
Don't have an account?

Problem with updating custom field in parent object
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?
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 6.
Kindly suggest and support.
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?
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 6.
Kindly suggest and support.
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
1 point: I suggest you to move logic to new class and use it in Lead's trigger. Just for avoiding "empty update" on Account object.
2 point: if Account trigger is fired "before insert" Trigger.newMap will be null and you'll get an exception.
About Lead's trigger: try to write like this
About Account's trigger:
I think you've made a mistake in field name. Could you check API Name in your field's information?
Thank you for Immediate Response.
I had changed as you mentioned above. Now,I am getting error as "variable doesnot exist:id" on lead trigger.
API name is RollupCounter__c in account object. For this,I got an error as "variable doesn't exist:RollupCounter__c".
Kindly Suggest and Support.
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.