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
Devendra Hirulkar 3Devendra Hirulkar 3 

copy child field data to parent field using trigger have two object child(class) and parent(student)

who to copy child field data to parent field using triggeri have two object  child(class) and parent(student) 
 
student         class
----------------------------
name           name
address        student(lookup)
rollno          rollno
------------------------------
in above to obj if student name and class name will match  then it copy rollno from class and put into student 
please tell me how to get this 
thanks,
devendra
ShashankShashank (Salesforce Developers) 
This should be a good sample to start with:
 
trigger updateRollNoFromClass on class__c (before insert) {
    set<Id> studentIds = new set<Id>();
    list<student__c> studentlist = new list<student__c>();
    for(case c:trigger.new){
        studentIds.add(c.student__c);
    }
    map<Id,student__c> studentMap = new map<Id,student__c>([select name, rollno__c from student__c where Id in :studentIds]);
    for(class__c cl:trigger.new){
        if(studentMap.get(cl.student__c).name=cl.name){
            student__c s = new student__c();
            s.id = cl.student__c;
            s.rollno__c = cl.rollno__c;
            studentlist.add(s);
        }
    }
    if(studentlist.size()>0) update studentlist;
}

 
AM2014AM2014
Hi ,
You can try the below code as well..

trigger UpdateRollNo on Class__c (before insert,before update) {     

       Map<Id,Student__c> studMap = new Map<Id,Student__c>();
       Set<Id> Ids = new Set<Id>();
                   
            for(Class__c c: Trigger.new)
            {
               Ids.add(c.Student_Name__c);
            }
            
            Map<id,Student__c> studmap2 = new Map<id,Student__c>([Select Id,Name,Student_Roll_Number__c From Student__c where Id IN : Ids]);
            
            for(Class__c c1 : Trigger.new)
              {
                  Student__c st = studMap2.get(c1.Student_Name__c);
                      if(st.Student_Roll_Number__c == '')
                          {
                          st.Student_Roll_Number__c = c1.Student_Roll_No__c;
                          studMap.put(st.id,st);
                          }
               }
            if(!studmap.isEmpty())
            {
               update studMap.values();
            }   
}