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
Abhinav AdminAbhinav Admin 

Requesting for the After Triggers Code for the Following Requirement

Hi all, I Have Two Objects called Student and Payment Student is the Master Object and Payment is the Child Object In those i have a common field called Fee When i enter a Fee field in the parent object its child objects also fill with the same value so can any one tell me the code for this to achieve After Triggers
Jannis BottJannis Bott
Is there a specific reason for using an after trigger? 

I think the easiest is a cross object formula to get the common field displayed on the child record. Something such as:
Student_and_Payment_Student__r.Fee__c

If you want to use a trigger try this: 
//I think you would be fine to just use a before trigger
trigger AddFeeToChildren on Student_and_Payment_Student__c (before update, before insert) {
   
    //create map with Id (later referenced as parent Id) and Fee
    Map<Id, String> studentAndPaymentMap = new Map<Id, String>();
    //creat set to store Id's for query
    Set<Id> studentAndPaymentId = new Set<Id>();
    for(Student_and_Payment_Student__c spsId : trigger.new){
        studentAndPaymentId.add(spsId.Id);
     }
    //fiil map
    for(Student_and_Payment_Student__c sps : trigger.new){
        studentAndPaymentMap.put(sps.Id, sps.Fee__c);
     }
    //get all child payments
    List<Payment__c> payment = [Select Id, ParentId, Fee__c from Payment__c where ParentId =: studentAndPaymentId];
    for(Payment__c p : payment){
        p.Fee__c = studentAndPaymentMap.get(p.ParentId);
    }
    update payment;
}

Cheers,
Jannis
Abhinav AdminAbhinav Admin
Thanks for Your Reply but it Didn't full fill my requirement. I Tried This Code Please tell is there any wrong in this............


Trigger updateFee on Students__c (After insert,After update)
{
    set<ID> Ids = new set<ID>();
    if(Trigger.isInsert || Trigger.isUpdate)
    {
        for(Students__c s : Trigger.New)
        {
            Ids.add(s.ID);
        }
    }
    System.debug(Ids);
    List<Students__c> lststdnt = [Select Id,Name,Fee__c from Students__c where Id IN : Ids];
    List<Payment__c> lstpymnt = [Select Id,Name,Total_Fee__c from Payment__c where Student_Name__c IN : Ids];
    System.debug(lststdnt);
    System.debug(lstpymnt);
    List<Payment__c> updatepymnt = new List<Payment__c>();
        for(Payment__c p : lstpymnt)
        {
            //p.Total_Fee__c = Student_Name__r.Fee__c   ;
            updatepymnt.add(p);
        }
        update updatepymnt;
}