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
ERHAN BALLI 9ERHAN BALLI 9 

Trigger update problem !

Hi I am new in salesforce. I am trying to learn about triggers and update mu student fees in my custom project. My code is not giving any errors but also it is not update the Student_Payment__c. Can someone help me about this situation my code is like that.. 


trigger StudentFeeCalculation on Student_Record__c (before insert, after insert, after update, before update) {
    
    Decimal ScholarFee = 0;
    Fees__c newFees = new Fees__c();
    Student_Record__c Std = new Student_Record__c();
    
    //Calculating the sport scholarship of students
    
            if(Std.Scholarship__c == 'Sport Scholarship')
         {           
             // For computer engineering students
             if ( Std.Department__c == 'Computer Engineering')
             {       
                     List<Fees__c> SFee = new List<Fees__c>(); 
                    SFee =  [SELECT Fees__c.Department_Fees__c FROM Fees__c WHERE Fees__c.Name = 'Computer Engineering Fees'];
                     List<Student_Record__c> StdPayment = new List<Student_Record__c>();
                    StdPayment = [SELECT Student_Record__c.Student_Payment__c FROM Student_Record__c];
                
                 //Update fees for every student    
                 for(Student_Record__c stdPay: StdPayment)
                 {
                             ScholarFee = Integer.valueOf(SFee);
                             ScholarFee = ScholarFee * (25/100);                              
                             update StdPayment;
                 }         
               }   
             }     
           }
Krishna SambarajuKrishna Sambaraju
Your trigger should be something like this. Please change the object names and field names accordingly.
trigger StudentFeeCalculation on Student_Record__c (before insert, before update)
{
	Map<string, Fees__c> dept2FeeMap = new Map<string, Fees__c>();
	List<Fees__c> feeList = [select Name, Department_Fees__c from Fees__c];
	for (Fees__c fee : feeList)
	{
		dept2FeeMap.put(fee.Name, fee.Department_Fees__c);
	}
	for (Student_Record__c sr : Trigger.new)
	{
		if (sr.Scholarship__c == 'Sport Scholarship' && sr.Department__c == 'Computer Engineering')
		{
			decimal deptFee = dept2FeeMap.get(sr.Department__c).Department_Fees__c;
			decimal scholarFee = deptFee * 0.25;
			sr.Student_Payment__c = scholarFee;
		}
	}
}

Hope this helps.
SIVASASNKARSIVASASNKAR
Hi ERHAN,

The trigger should not required to fire on after events, hope below code will help you !!.
 
trigger StudentFeeCalculation on Student_Record__c (before insert, before update) {
    
    List<Fees__c> SFee = new List<Fees__c>( [SELECT Department_Fees__c FROM Fees__c WHERE Name = 'Computer Engineering Fees']);//Query the Fees__c record where Name = Computer Engineering Fees
    
    if(SFee != null && !SFee.isEmpty())//Check the Fees record is existed or not.
	for (Student_Record__c stRec : trigger.new ) { // loop for all student records updated or inseterd
        if( Std.Scholarship__c.equalsIgnoreCase('Sport Scholarship') && Std.Department__c.equalsIgnoreCase( 'Computer Engineering') ) // check the Scholarship and department
        {           
         // For computer engineering students , Update fees for every student
         stRec.Student_Payment__c = SFee[0].Department_Fees__c * (25/100) ; // assign the payment value as 25 %.
		}   
    }            
}

Thanks,
Siva.
 
ERHAN BALLI 9ERHAN BALLI 9
Hi SIVASASNKA,

I tried your code but my payment object is still empty on the page. Is it possible about the relationship problem between my two objects ? I create fees object and make it look up relation with Student Record object which is incliding the Student_Payment__c object.
SIVASASNKARSIVASASNKAR
Hi Erhan,

Can you please provide us the objects name and relationship among them ? if possible provide us the objects fields.

Thanks
Sivasankar
Mark ShlebyMark Shleby
Can i use this or integrate with my Dream 11 MOD Apk (https://apkestate.com/dream-11-mod-apk/) website to get up-to-date data?