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
Shubham SengarShubham Sengar 

sum of fee by using trigger

How to write a trigger to sum like
Fee Paid Details = First Installment + Second Installment or Paid Full Fee
 ​where Fee Paid  Details is class object's field and remaning three like First Installment ,Second Installment , Paid Full Fee is Contact object's field..??


 
SaranSaran
HI,

Is there any relationship between class object and contact object.

If there is a relationship. Which will be acting as a parent.

Also if contact is child and class is parent. Then you have to know which child sum amount should you populate. because a single parent can have multiple child.

Let us know further details.

Thanks !
Shubham SengarShubham Sengar
Hi Saran yes there is a relationship between Class(parent) and Contact(child) object and i want to populate sum in Class object (like Fee Paid Details field)Fee Paid Details = First Installment + Second Installment or Paid Full Fee where  First Installment , Second Installment , Paid Full Fee contact object fields   
SaranSaran
Yes, In this case a class can have multiple contact's rite.

On that case you want to sum all the vales.

Eg: Class A has two contacts like,

Contact A - first instl as 200 - second inst as 400 - paid full fee as 400;
Contact B - first Instl as 400 - second inst as 300 - paid full fee as 300;

ON this case you want to update class A's fee paid detail as 2000;

or want to pick the sum of any one contact. If you want to pick and one contact on what condition you want to select the contact.

Thanks!
 
Shubham SengarShubham Sengar

hi saran
yes u r right, a class have multiple contact's.
and yes i  want to sum all the vales.
but here one thing 
Fee Paid Details =First Installment + Second Installment or if he/she paid one time full payment then add only  Paid Full Fee
SaranSaran
Hi Sengar,

Hope this below code will solve your issue.
 
trigger updateClass on contact (after insert)
{
    set<id> classId = new set<Id>();
	for(Contact con : trigger.new){
		if(trigger.isInsert)
			classId.add(con.class__c);
		else if(trigger.isUpdate){
			contact oldCon = trigger.oldMap.get(con.Id);
			if(con.First_Installment__c != oldCon.First_Installment__c ||
		 	   con.Second_Installment__c != oldCon.Second_Installment__c ||
		 	   con.Paid_Full_Fee__c != oldCon.Paid_Full_Fee__c)
		 	   		classId.add(con.class__c);
		}
	}

	map<id, decimal> feeMap = new map<id, decimal>();
	list<Class__c> classList = new list<Class__c>();

	if(classId.size() > 0){
		for(AggregateResult agr : [select class__c cls , sum(First_Installment__c) firstIns, sum(Second_Installment__c) secIns
							   from   contact 
							   where  class__c in: classId AND Paid_Full_Fee__c == null
							   group by class___c])
		{
			decimal total = (double)agr.get('firstIns') + (Double)agr.get('secIns');
			feeMap.put((Id)agr.get('cls') , total);
		}

		for(AggregateResult agr : [select class__c cls , sum(Paid_Full_Fee__c) fullPay
								   from   contact 
								   where  class__c in: classId AND Paid_Full_Fee__c != null
								   group by class___c])
		{
			feeMap.put((Id)agr.get('cls') , (double)agr.get('fullPay'));
		}
	}

	if(feeMap.size() > 0){
		for(id clsId : feeMap.keySet()){
			Class__c cls = new class__c();
			cls.id = clsId;
			cls.Fee_Paid_Detail__c = feeMap.get(clsId);
			classList.add(cls);
		}
	}

	if(classList.size() > 0)
		update classList;	
}

Let us know if you face any issues.

Thanks!!
 
Shubham SengarShubham Sengar
Hi Saran ...
thanks for ur code but i'm looking trigger for class object like
trigger updateFee on class__c (after insert) and here add all the First + Second fee or if one time  Paid Full Fee then add full fee 
suppose i have 4 classes then every class have Fee Detail field where auto populate value  like 
Fee Paid Details = First Installment + Second Installment or Paid Full Fee
class have only one field that is Fee Paid Details remaning Field is present in Contact object ..

 
SaranSaran
Hi Sengar,

It we write the trigger in the class Object then the trigger will work only when the class obj is created or udated. 

But in our case we will be creating contact after the creation of the class object. So  there is no use in writing trigger on class__c object.

Thanks!
Shubham SengarShubham Sengar
Hi Saran
ya u r right we can't write a triggng in class object  ....Thanks
But then how can add the cource fee ..
like...
Contact and Class related to each other by lookup
in contact ​​​(fee detaile ) object there is four field they r  Cource Fee(number) ,​First Installment(Number) ,Second Installment(number) and Paid Full Fee(checkbox)
so now i want to add all fee Classwise for example if i have 2 classes then i want to know how much money paid and how much remaning ,..??
as i think
Free Paid = ​First Installment + Second Installment or if click on Paid full fee then select course Fee field value
i want classwise totail Paid Fee Amount  ​