You need to sign in to do that
Don't have an account?
Deepak Pandey 13
trigger after update lookup field not updated?
trigger countingof on lead (after insert,after update,after delete)
{
set<id> conId = new set<id>();
map<id,list<lead>> countle= new map<id,list<lead>>();
if(trigger.isafter && trigger.isinsert || trigger.isUpdate )
{
for(lead c: trigger.new)
{
if(c.owner_type__c!=null)
{
conId.add(c.owner_type__c);
}
}
}
if(trigger.isafter && trigger.isDelete)
{
for(lead c:trigger.old)
{
if(c.owner_type__c!=null)
{
conId.add(c.owner_type__c);
}
}
}
list<lead> leadlist= [select id ,owner_type__c , no_of_lead__c from lead where owner_type__c In :conId];
for(lead a :leadlist)
{
countle.put(a.owner_type__c, leadlist);
}
list<lead> lst=new list<lead>();
list<lead> countlist = [select id, no_of_lead__c,owner_type__c from lead where owner_type__c in :conId];
for(lead a : countlist)
{
list<lead> lstt=countle.get(a.owner_type__c);
a.no_of_lead__c= lstt.size();
system.debug('@@@a' +a);
if(a.owner_type__c!=null)
{
lst.add(a);
}
update lst;
}
}
Error:Apex trigger countingof caused an unexpected exception, contact your administrator: countingof: execution of AfterUpdate caused by: System.DmlException: Update failed.
{
set<id> conId = new set<id>();
map<id,list<lead>> countle= new map<id,list<lead>>();
if(trigger.isafter && trigger.isinsert || trigger.isUpdate )
{
for(lead c: trigger.new)
{
if(c.owner_type__c!=null)
{
conId.add(c.owner_type__c);
}
}
}
if(trigger.isafter && trigger.isDelete)
{
for(lead c:trigger.old)
{
if(c.owner_type__c!=null)
{
conId.add(c.owner_type__c);
}
}
}
list<lead> leadlist= [select id ,owner_type__c , no_of_lead__c from lead where owner_type__c In :conId];
for(lead a :leadlist)
{
countle.put(a.owner_type__c, leadlist);
}
list<lead> lst=new list<lead>();
list<lead> countlist = [select id, no_of_lead__c,owner_type__c from lead where owner_type__c in :conId];
for(lead a : countlist)
{
list<lead> lstt=countle.get(a.owner_type__c);
a.no_of_lead__c= lstt.size();
system.debug('@@@a' +a);
if(a.owner_type__c!=null)
{
lst.add(a);
}
update lst;
}
}
Error:Apex trigger countingof caused an unexpected exception, contact your administrator: countingof: execution of AfterUpdate caused by: System.DmlException: Update failed.
You are getting trapped in recursive trigger, where your trigger is updating again and again.
To overcome this situation you need to create separate static class like this :
And your trigger would be like this :
All Answers
Please try this one..............
Can you explain your requirment with bit clarity .What is owner_type__c here ? owner_type__c can not be duplicate in lead ?
You are updating no of lead in lead object it self is it your requirment ?
Please explain so that we can help you .
Thanks
Manoj
@manoj ji
1-owner_type__c is lookup of contact. 2-no_of_lead- size of lead in a contact.
after insert and after delete both are working but when i change the owner_type__c then showing error.
You are getting trapped in recursive trigger, where your trigger is updating again and again.
To overcome this situation you need to create separate static class like this :
And your trigger would be like this :