+ Start a Discussion
Supriyo Ghosh 5Supriyo Ghosh 5 

Update custom field based on related list filed value

Hi,

I have two object.
1.Employee Master
2.Sp Master.

SP master is a related list with Employee master.So i want if SP Master one field Active__c =true then on check box should get checked in Employee master.How to do that.Please help.
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.Employee_Code__c )
        {
            EmployeeRecords.add(sp.Employee_Code__c);
        }
    }
    
	list<Employee_Master__c> EmpMaster = [select id,Name,SP__c from Employee_Master__c where id IN :EmployeeRecords and SP__c = false ];
    
    for( Employee_Master__c empM : EmpMaster )
    {
        empM.SP__c = true;
    }
     
    if(EmpMaster.size()>0)
    {
        update EmpMaster;
    }
}
If this will not help then please try to connect on my email id

Thanks
Amit Chaudhary
"amit.salesforce21@gmail.com"

All Answers

Sai Kalyan SanisettySai Kalyan Sanisetty
Hi Supriyo,

Please let me know here which one is the Parent and which one is child.

For this we need to write the Trigger like below ,

if Employee Master is parent and Sp Master is a chaild then we need to write like this 

trigger EmployeeMaster on SpMasterobject(after insert,after update)
{
    set<id> EmployeeRecords = new set<ID>();
    map<id,Employeeobject> dat = new map<id,Employeeobject>();
        
    for(SpMasterobject sp:trigger.new)
    {
        if(Active__c == true)
        {
            EmployeeRecords.add(sp.ParentEmployeeIDs);
        }
    }
    list<Employeeobject> Emp = [select id,Name,checkboxparent from Employeeobject where ID IN:EmployeeRecords];
    
    
    
    for(Employeeobject Em:Emp)
    {    
        dat.put(Em.ID,Employeeobject);
    }
    
    for(SpMasterobject sp1:trigger.new)
    {
        dat.get(sp1.EmployeeID).EmployeeCheckbox = true;    
    }
    
    if(dat !=null && dat.size()>0)
    {
        Database.update(dat.values());
    }
}

 
Amit Chaudhary 8Amit Chaudhary 8
You can do the same the process bulider or workflow also. Please check below sample of process builder to update all child Contact base on account field changes
User-added image

Introduction to the Process Builder
https://developer.salesforce.com/trailhead/en/business_process_automation/process_builder
https://developer.salesforce.com/trailhead/en/project/quickstart-process-builder

Sample code for you also
trigger EmployeeMaster on SP_Master__c( after insert,after update )
{
    Set<id> setId = new Set<ID>();
        
    for(SP_Master__c sp : trigger.new)
    {
        if( sp.Active__c == true)
        {
            EmployeeRecords.add(sp.id);
        }
    }
	
    list<Employee_Master__c> EmpMaster = [select id,Name,Active__c from Employee_Master__c where SP_Master__c IN :EmployeeRecords and Active__c = false ]; 
	// please add Field Name properly
	
	for( Employee_Master__c empM : EmpMaster )
	{
		empM.Active__c = true;
	}
	
    
    if(EmpMaster.size()>0)
    {
        update EmpMaster;
    }
}

Please let us know if this will help you.

Thanks
Amit Chaudhary
 
Supriyo Ghosh 5Supriyo Ghosh 5
Hi,

Employee master is my Parent and SP Master is my child.On which object I have to write this trigger.
Amit Chaudhary 8Amit Chaudhary 8
SP Mater. you can try workflow also
Supriyo Ghosh 5Supriyo Ghosh 5
I am unable to do with workflow rules.Both the field are check box from two diffrent object.Is it still possible with workflow rules?
Amit Chaudhary 8Amit Chaudhary 8
Yes you can do only if you have Mater Detail relation on SP Master fo Employee Master
Supriyo Ghosh 5Supriyo Ghosh 5
No it is not a master detail relation.
Amit Chaudhary 8Amit Chaudhary 8
Then please try above code on SP Master please change API name according to your org
Supriyo Ghosh 5Supriyo Ghosh 5
I have written the the trigger but getting some 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)
        {
            EmployeeRecords.add(sp.id);
        }
    }
    list<Employee_Master__c> EmpMaster = [select id,Name,SP__c from Employee_Master__c where SP_Master__c IN :EmployeeRecords and Active__c = false ];
    
     
    for( Employee_Master__c empM : EmpMaster )
    {
        empM.SP__c = true;
    }
     
    if(EmpMaster.size()>0)
    {
        update EmpMaster;
    }
}

Error


Error: Compile Error: No such column 'SP_Master__c' on entity 'Employee_Master__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 10 column 42
Supriyo Ghosh 5Supriyo Ghosh 5
Object name                                                   Field Name
Employee_Master__c                                     SP__c
SP_Master__c                                                Active__c
Supriyo Ghosh 5Supriyo Ghosh 5
I am unable to attached screen shot.

Lookup name is Employee_Code__c in SP_Master__c.
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.Employee_Code__c )
        {
            EmployeeRecords.add(sp.Employee_Code__c);
        }
    }
    
	list<Employee_Master__c> EmpMaster = [select id,Name,SP__c from Employee_Master__c where id IN :EmployeeRecords and SP__c = false ];
    
    for( Employee_Master__c empM : EmpMaster )
    {
        empM.SP__c = true;
    }
     
    if(EmpMaster.size()>0)
    {
        update EmpMaster;
    }
}
If this will not help then please try to connect on my email id

Thanks
Amit Chaudhary
"amit.salesforce21@gmail.com"
This was selected as the best answer
Supriyo Ghosh 5Supriyo Ghosh 5
Thanks a lot.Its working fine.