You need to sign in to do that
Don't have an account?
Alok 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();
*/
// }
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();
*/
// }
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
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
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
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
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
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
Thank You so much
{
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