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 

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.
Best Answer chosen by Keerthigee
KeerthigeeKeerthigee
Hi Denis,

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

Denis VakulishinDenis Vakulishin
Hello Gita,

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
if(l.ParentLookup__c!=null)
    {
       Account acc = new Account();
       acc.Id=l.ParentLookup__c;
       m.put(l.ParentLookup__c,acc);
    }

About Account's trigger:
I think you've made a mistake in field name. Could you check API Name in your field's information?
KeerthigeeKeerthigee
Hi Denis,

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.
Denis VakulishinDenis Vakulishin
Do you have somewhere in your code class definition like this ?
public class Account {
//Code goes here
}


KeerthigeeKeerthigee
Hi Denis,

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