trigger EmployeeTrigger1 on Employee__c (after insert,after update,after delete,after undelete) { list<Organization__c> Orgvalues = new list<Organization__c>(); set ORGIds = new set(); map<id,Decimal> Sumvalues= new map<id,Decimal>(); map<id,Decimal> MinValues= new map<id,Decimal>(); map<id,Decimal> Maxvalues= new map<id,Decimal>();
trigger EmployeeTrigger1 on Employee__c (after insert,after update,after delete,after undelete)
{
list<Organization__c> Orgvalues = new list<Organization__c>();
set<ID> ORGIds = new set<ID>();
map<id,Decimal> Sumvalues= new map<id,Decimal>();
map<id,Decimal> MinValues= new map<id,Decimal>();
map<id,Decimal> Maxvalues= new map<id,Decimal>();
if(trigger.isinsert || trigger.isupdate || trigger.isundelete)
{
for(Employee__c Emprecords:trigger.new)
{
ORGIds.add(Emprecords.Organization__c);
}
}
if(trigger.isdelete || trigger.isupdate)
{
for(Employee__c Emprecords1:trigger.new)
{
ORGIds.add(Emprecords1.Organization__c);
}
}
list<AggregateResult > Outputvalues = [select Organization__c Acc,sum(Salary__c) Sumdata,min(Salary__c) MIndata,Max(Salary__c) Maxdata from Employee__c where Organization__c IN:ORGIds group by Organization__c];
for(AggregateResult Ar:Outputvalues)
{
Sumvalues.put((id)Ar.get('Acc'),(Decimal)Ar.get('Sumdata'));
MinValues.put((id)Ar.get('Acc'),(Decimal)Ar.get('MIndata'));
Maxvalues.put((id)Ar.get('Acc'),(Decimal)Ar.get('Maxdata'));
}
for(Organization__c acc1:[select id,Name,Max_Salary__c,Min_Value__c,Sum_Of_Salary__c from Organization__c where ID IN:ORGIds])
{
acc1.Sum_Of_Salary__c = Sumvalues.get(acc1.ID);
acc1.Min_Value__c = MinValues.get(acc1.ID);
acc1.Max_Salary__c = Maxvalues.get(acc1.ID);
Orgvalues.add(acc1);
}
update Orgvalues;
}
}