+ Start a Discussion
Deepak Pandey 13Deepak 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.
Best Answer chosen by Deepak Pandey 13
JethaJetha
HI Deepak,

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 : 

 
public Class checkRecursive
{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

And your trigger would be like this : 
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);
		}
    }
	
    if(checkRecursive.runOnce())
    {
        update lst;          
    }
}

 

All Answers

JethaJetha
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;
	
}

Please try this one..............
ManojjenaManojjena
Hey Deepak ,

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
 
Deepak Pandey 13Deepak Pandey 13
@jetha sf not working.
@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.
JethaJetha
HI Deepak,

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 : 

 
public Class checkRecursive
{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

And your trigger would be like this : 
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);
		}
    }
	
    if(checkRecursive.runOnce())
    {
        update lst;          
    }
}

 
This was selected as the best answer
Deepak Pandey 13Deepak Pandey 13
thanks SF....