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
kkumar011985kkumar011985 

Error in trigger (Circular dependency)

Hi,

 

I have written a trigger to update user manager field based on the custom object.

There is no relationship between these two objects. Only thing is that common field is there in these 2 objects.

 

for 10 to 20 records manager field was getting update fine but when I was trying to insert 4000 records I'm getting an exception,,

 

Update failed. First exception on row 0 with id 0014000000b5gNsAAI; first error: CIRCULAR_DEPENDENCY,you cannot set hierarchy field to point to itself or a child record:[]

 

Here is the code,

 

 

trigger updateToUser on Export_Object__c (after insert)
{
    List<Export_Object__c> deleteexportobject = new List<Export_Object__c>(); 
        Set<String> eoid = new Set<String>();
        for(Export_Object__c e: Trigger.New)
        {
            if(e.adid__c!=null)
            {
                eoid.add(e.adid__c);
            }
        }
        
        List<User> user_list = [select id,adid__c,name from user where adid__c in: eoid];
        
        Transient Map<string,string> mapRecords = new Map<string,string>();
        
        List<User> user_list1 = [select id,adid__c,name from user];
        
        for(User u:User_List1)
        {
          mapRecords.put(u.Name,u.id);
        }
        
         system.debug('*****'+mapRecords);
        List<User> updatelist = new List<user>();
        
        for(Export_Object__c e: Trigger.New)
        {
            for(User u:User_List)
            {
                if(e.Manager__c!=null)
                {                            
                    try
                    {                         
                        u.managerId = mapRecords.get(e.Manager__c);
                    }
                    catch(exception ee)
                    {
                        system.debug('tusssssssssssssssss');                    
                    }
                }
                updatelist.add(u);
            }
            
        }
        update updatelist;

 

 

Please help me out where I am doing the mistake

 

THank you

 

phamleDevphamleDev

Hello,

 

From what it looks like, the nested for loop is assigning all users the same manager, which assign the manager to himself/herself. There should be another condition to make sure that the manager belongs to the user.  

 

Something like this: 

 

                if(e.Manager__c!=null && u.adid__c == e.adid__c)
                {                            
                    try
                    {                         
                        u.managerId = mapRecords.get(e.Manager__c);
                    }
                    catch(exception ee)
                    {
                        system.debug('tusssssssssssssssss');                    
                    }
                }

 

Hopefully this will solve your problem.

kkumar011985kkumar011985

Thanks for your reply.

 

I also tried like the way you mentioned

 

trigger updateToUser on Export_Object__c (after insert)
{
List<Export_Object__c> deleteexportobject = new List<Export_Object__c>();
Set<String> eoid = new Set<String>();
for(Export_Object__c e: Trigger.New)
{
if(e.adid__c!=null)
{
eoid.add(e.adid__c);
}
}

List<User> user_list = [select id,adid__c,manager.name from user where adid__c in: eoid];

Transient Map<string,string> mapRecords = new Map<string,string>();

List<User> user_list1 = [select id,adid__c,name from user];

for(User u:User_List1)
{
mapRecords.put(u.Name,u.id);
}

system.debug('*****'+mapRecords);
List<User> updatelist = new List<user>();

for(Export_Object__c e: Trigger.New)
{
for(User u:User_List)
{

if(u.adid__c == u.adid__c)

{
if(e.Manager__c!=null)
{
if(mapRecords.containsKey(e.Manager__c))
{
try
{
system.debug(e.Manager__c+'*****'+mapRecords.containsKey(e.Manager__c));

u.managerId = mapRecords.get(e.Manager__c);
}
catch(exception ee)
{
system.debug('tusssssssssssssssss'+ee);
}
}
}
updatelist.add(u);
}
}
}
update updatelist;

 

 

I am not able to find out the sollution what is happening..

 

 

THanks

phamleDevphamleDev

Try changing if(u.adid__c == u.adid__c) to if(u.adid__c == e.adid__c).

kkumar011985kkumar011985

Hi,

 

I have changed it to if(u.adid__c == e.adid__c)

 

Still the problem continues...

 

 

phamleDevphamleDev

Have you check the data to see if there is a  circular dependency?