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
simon chaosimon chao 

Is it possible to set children name field to a field on the parent?

I want to set a parent field to a children field, is that possible? 
I have a class__c (parent) within it, I have a student__c (children). The relationship between them is one to many. There can be many students within a class. 

Within the class__c I have a custom field, StarPupil__c, and I want to set a student name to that field. Is that possible? Do I have to query the field or can I just assign it? 

 

List<Class__c> tempClass = new List<Class>();

//First I want to check if class object have any students, if not I set the starPupil field to blank 

if( tempClass.student__r.size() == 0 )
{
     tempClass.starPupil__c = ' ';
}
// then i check to see if starpupil is equal to student name if not i set it to student name 
else
{
     if(tempClass.starPupil__c  != tempClass.student__r.studentName__c)
     {
          tempClass.starPupil__c = tempClass.student__r.studentName__c;
     }
}
The above code does not work as it seems that I cannot add the .studentName__c after student__r within the if statement inside the else statement. How should I approach this? 
v varaprasadv varaprasad
Hi Simon,

yes, it is possible but which student name you need to add, One class, contains many students right?

Check once below sample code : 
 
List<Class__c> tempClass = [Select id,starPupil__c,(Select Student_Name__c, Exam_Mark__c from Student__r ORDER BY Exam_Mark__c ASC limit 1) from class__c];

//First I want to check if class object have any students, if not I set the starPupil field to blank 
for(Class__c cls : tempClass){
if(cls.student__r.size() == 0 )
{
     cls.starPupil__c = ' ';
}
// then i check to see if starpupil is equal to student name if not i set it to student name 
else
{   
          cls.starPupil__c = tempClass.student__r.studentName__c;
}
}

update tempClass;



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
Dushyant SonwarDushyant Sonwar
Hi Simon,

Below code will help you understand and solve your problem.
Map<String , Student__c> mapOfHighestMarkStudentByClass = new Map<String , Student__c>(); // fill map of class and the highest student mark

//the below query will order lowest to highest , so in the end highest will remain in map according to class
for(Student__c 	StudentObj : [Select Id,Class__c,Exam_Mark__c from Student__c where class__c != null Order by Exam_Mark__c Asc Nulls First]){
	mapOfHighestMarkStudentByClass.put(studentObj.Class__c ,  studentObj);
}


// we will fill list of class to update with star pupil
list<Class__c> lstClassesToUpdate = new list<Class__c>();
for(String classId : mapOfHighestMarkStudentByClass.keyset()){
	Class__c classObj = new Class__c(Id = classId);
	classObj.StarPupil__c = mapOfHighestMarkStudentByClass.get(classId).Id;
	lstClassesToUpdate.add(classObj);
}

//update those classes
if(lstClassesToUpdate.size() > 0){
	update lstClassesToUpdate;
}

Hope this helps.