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
Alok ParidaAlok Parida 

Trigger to update parent object field based on child records.

I have 2 custom objects employee__c and company__c.In employee  Object I have a field called salry__c and in Company object Ihave a field max_salry__c .I want to populate max salary of the employees on Parent Object Field max_salry__c.Please help me on trigger for this scenario.

Here is the snippet I tried .

trigger calculateMaxSalary on Employee__c (after update, after insert) {

        /*Map<Id, Compny__c> parentcomp = new Map<Id, Compny__c>();
        List<Id> listIds = new List<Id>();

        for(Employee__c childObj : Trigger.new) {
            listIds.add(childObj.salaries__c);
        }

        parentcomp= new Map<Id, Compny__c> ([Select Id, Name,max_salary__c, (Select Id, salry__c from Emploies__r) from Compny__c where Id in :listIds]);

        for(Employee__c emp : Trigger.new) {    
            Compny__c comp = parentcomp.get(emp.salaries__c);
            system.debug('Max Salary#########'+comp.max_salary__c);
            system.debug('Salary**************'+emp.salry__c);
            //comp.max_salary__c= max(emp.salry__c);
        }

        update parentcomp.values();
        */
        
        
               
  // }
Best Answer chosen by Alok Parida
AvaneeshAvaneesh
Hii Alok

you need a little bit trick find your parent account id and put an inner query something like 

I am doing this on account as parent and contact as child 
 
select id ,name,(select id,firstname,max_salary__c from contacts ORDER BY max_salary__c  DESC  limit 1) from account where id='0016F000023D2e0'

this will give you that contact which has the highest amount .....
and I hope rest of you will understand what you need to do ...............


if still any problem then let me know I will write your trigger and if this was helpful, please mark as best answer 

Thank you
Avaneesh Singh

 


 

All Answers

Alok ParidaAlok Parida
I am not able to achieve it.
comp.max_salary__c= max(emp.salry__c);//In this line i am getting error like invalid method:max.

Please suggest me some other approach or any changes required for this.

Thanks,
Alok
AvaneeshAvaneesh
Hii Alok

you need a little bit trick find your parent account id and put an inner query something like 

I am doing this on account as parent and contact as child 
 
select id ,name,(select id,firstname,max_salary__c from contacts ORDER BY max_salary__c  DESC  limit 1) from account where id='0016F000023D2e0'

this will give you that contact which has the highest amount .....
and I hope rest of you will understand what you need to do ...............


if still any problem then let me know I will write your trigger and if this was helpful, please mark as best answer 

Thank you
Avaneesh Singh

 


 
This was selected as the best answer
Alok ParidaAlok Parida
Hi Avaneesh,
I changed my code as per ur query .But what its doing is :its updating the max salary across the emploies .Its displaying Max salry from diffrent company employee too.Pls help me.


 Map<Id, Compny__c> parentcomp = new Map<Id, Compny__c>();
        List<Id> listIds = new List<Id>();

        for(Employee__c childObj : Trigger.new) {
            listIds.add(childObj.salaries__c);
        }

 parentcomp= new Map<Id, Compny__c> ([Select Id, Name,max_salary__c, (Select Id, salry__c from Emploies__r order by salry__c desc limit 1) from Compny__c ]);

        for(Employee__c emp : Trigger.new) {    
            Compny__c comp = parentcomp.get(emp.salaries__c);
            system.debug('Max Salary#########'+comp.max_salary__c);
            system.debug('Salary**************'+emp.salry__c);
          
        }
Thanks,
Alok
Alok ParidaAlok Parida
Thanks Avaneesh .I am able to achieve it.
Thank You so much
Sreelakshmi N 1Sreelakshmi N 1
public static void maxsalupdate(list<emp2__c> emp6)
    {
        map<id,emp1__c>parentmap;
        Set<Id> setid= new Set<Id>();    
        for(emp2__c obj : emp6)
        {
            {
                setid.add(obj.Name__c);
                
            }
        }
        
         parentmap = new Map<Id, emp1__c>([select id,max_salary__c,(select id,Salary__c from emps__r order by Salary__c desc ) from emp1__c ]);
        
        for(emp2__c acc1:emp6)
        {
            emp1__c comp = parentmap.get(acc1.Name__c);
            system.debug('Max Salary#########'+comp.max_salary__c);
            
        }
        
        
       
    }

still not working