+ Start a Discussion
Supriyo Ghosh 5Supriyo Ghosh 5 

Error in Trigger

I am to copy one text field from child to parent object.but I am getting one error.Please help.

Trigger

trigger EmployeeMaster  on SP_Master__c (after insert,after update) 
{
    
    Set<id> EmployeeRecords  = new Set<ID>();
    
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
            EmployeeRecords.add(sp.Employee_Code__c);
        }
    }
    
    list<Employee_Master__c> EmpMaster = [select id,Name,SP__c,LI_License_No__c from Employee_Master__c where id IN :EmployeeRecords and SP__c = false ];
    
    for( Employee_Master__c empM : EmpMaster )
        
      
    {
        empM.SP__c = true;
        empM.LI_License_No__c=sp.License_Number__c;
        
    }
     
    if(EmpMaster.size()>0)
    {
        update EmpMaster;
    }
}

Error

Error: Compile Error: Variable does not exist: sp.License_Number__c at line 21 column 31
Best Answer chosen by Supriyo Ghosh 5
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
trigger EmployeeMaster  on SP_Master__c ( after insert, after update )
{
    Set<id> EmployeeRecords = new Set<ID>();
     
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
            EmployeeRecords.add(sp.Employee_Code__c);
        }
    }
     
    Map< Id ,Employee_Master__c> EmpMaster = new Map< Id ,Employee_Master__c> ( [select id,Name,SP__c,LI_License_No__c from Employee_Master__c where id IN :EmployeeRecords and SP__c = false ] ) ;
 
    List<Employee_Master__c> lstEmpMasterToUpdate = new List<Employee_Master__c>();
     
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
            if(EmpMaster.containsKey(sp.Employee_Code__c))
            {
				Employee_Master__c empMaster1 = EmpMaster.get(sp.Employee_Code__c); 
                empMaster1.LI_License_No__c=sp.License_Number__c;
                empMaster1.SP__c=True;
                lstEmpMasterToUpdate.add(empMaster1);
            }
        }
    }
    if(lstEmpMasterToUpdate.size() > 0 )
    {
        update lstEmpMasterToUpdate;
    }
}

in above code you forget to add below line
lstEmpMasterToUpdate.add(empMaster1);

Let us know if this will help you

All Answers

ImmuImmu
Hi Supriyo,

Try this...
 
list<Employee_Master__c> EmpMaster = [select id,Name,SP__c,LI_License_No__c from Employee_Master__c where id IN :EmployeeRecords and SP__c = false ];
    if(EmpMaster.size()>0)
{
for(SP_Master__c sp : trigger.new){
    for( Employee_Master__c empM : EmpMaster )              
    {
        empM.SP__c = true;
        empM.LI_License_No__c=sp.License_Number__c;
        EmpMaster.add(empM);
    }
 }
}
    if(EmpMaster.size()>0)
    {
        update EmpMaster;
    }
}
Supriyo Ghosh 5Supriyo Ghosh 5
@Immu
I have tried your code as below.

trigger EmployeeMaster  on SP_Master__c (after insert,after update) 
{
    
    Set<id> EmployeeRecords  = new Set<ID>();
    
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
            EmployeeRecords.add(sp.Employee_Code__c);
        }
    }
    
    list<Employee_Master__c> EmpMaster = [select id,Name,SP__c,LI_License_No__c from Employee_Master__c where id IN :EmployeeRecords and SP__c = false ];
    if(EmpMaster.size()>0)
{
for(SP_Master__c sp : trigger.new){
    for( Employee_Master__c empM : EmpMaster )              
    {
        empM.SP__c = true;
        empM.LI_License_No__c=sp.License_Number__c;
        EmpMaster.add(empM);
    }
 }
}
    if(EmpMaster.size()>0)
    {
        update EmpMaster;
    }
}

after that when I am trying to edit my SP Master record and save it is showing me 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger EmployeeMaster caused an unexpected exception, contact your administrator: EmployeeMaster: execution of AfterUpdate caused by: System.FinalException: Cannot modify a collection while it is being iterated.: Trigger.EmployeeMaster: line 18, column 1

Please help.
Amit Chaudhary 8Amit Chaudhary 8
TRy below code. I hope that will help  you
trigger EmployeeMaster  on SP_Master__c ( after insert, after update ) 
{
    Set<id> EmployeeRecords  = new Set<ID>();
    
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
            EmployeeRecords.add(sp.Employee_Code__c);
        }
    }
    
    Map< Id ,Employee_Master__c> EmpMaster = new Map< Id ,Employee_Master__c> ( [select id,Name,SP__c,LI_License_No__c from Employee_Master__c where id IN :EmployeeRecords and SP__c = false ] ) ;

	List<Employee_Master__c> lstEmpMasterToUpdate = new List<Employee_Master__c>();
	
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
			if(EmpMaster.containsKey(sp.Employee_Code__c))
			{
				Employee_Master__c empMaster = EmpMaster.get(sp.Employee_Code__c);
				empMaster.LI_License_No__c=sp.License_Number__c;
				
				lstEmpMasterToUpdate.add(empMaster);
			}
        }
    }
	if(lstEmpMasterToUpdate.size() > 0 )
	{
		update lstEmpMasterToUpdate;
	}
}
Let us know if this will help you

Thanks
Amit Chaudhary
 
Supriyo Ghosh 5Supriyo Ghosh 5
@Amit

After using your code I am getting one error.

Error: Compile Error: Duplicate variable: empMaster (attempt to re-create the variable with type: Employee_Master__c) at line 23 column 35

Pls help
 
Supriyo Ghosh 5Supriyo Ghosh 5
@amit

I have fixed the error but the code is not working.It is not coping the vale Li_License_No__c and in SP__c.Please help.

Code

trigger EmployeeMaster  on SP_Master__c ( after insert, after update )
{
    Set<id> EmployeeRecords = new Set<ID>();
     
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
            EmployeeRecords.add(sp.Employee_Code__c);
        }
    }
     
    Map< Id ,Employee_Master__c> EmpMaster = new Map< Id ,Employee_Master__c> ( [select id,Name,SP__c,LI_License_No__c from Employee_Master__c where id IN :EmployeeRecords and SP__c = false ] ) ;
 
    List<Employee_Master__c> lstEmpMasterToUpdate = new List<Employee_Master__c>();
     
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
            if(EmpMaster.containsKey(sp.Employee_Code__c))
            {
               Employee_Master__c empMaster1 = EmpMaster.get(sp.Employee_Code__c); 
                empMaster1.LI_License_No__c=sp.License_Number__c;
                empMaster1.SP__c=True;
                 
               
            }
        }
    }
    if(lstEmpMasterToUpdate.size() > 0 )
    {
        update lstEmpMasterToUpdate;
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
trigger EmployeeMaster  on SP_Master__c ( after insert, after update )
{
    Set<id> EmployeeRecords = new Set<ID>();
     
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
            EmployeeRecords.add(sp.Employee_Code__c);
        }
    }
     
    Map< Id ,Employee_Master__c> EmpMaster = new Map< Id ,Employee_Master__c> ( [select id,Name,SP__c,LI_License_No__c from Employee_Master__c where id IN :EmployeeRecords and SP__c = false ] ) ;
 
    List<Employee_Master__c> lstEmpMasterToUpdate = new List<Employee_Master__c>();
     
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true && sp.License_Type__c == 'LI')
        {
            if(EmpMaster.containsKey(sp.Employee_Code__c))
            {
				Employee_Master__c empMaster1 = EmpMaster.get(sp.Employee_Code__c); 
                empMaster1.LI_License_No__c=sp.License_Number__c;
                empMaster1.SP__c=True;
                lstEmpMasterToUpdate.add(empMaster1);
            }
        }
    }
    if(lstEmpMasterToUpdate.size() > 0 )
    {
        update lstEmpMasterToUpdate;
    }
}

in above code you forget to add below line
lstEmpMasterToUpdate.add(empMaster1);

Let us know if this will help you
This was selected as the best answer
Supriyo Ghosh 5Supriyo Ghosh 5
Thanks a lot.